D-LINK DIR-825의 OpenWRT 부팅 파티션을 USB 메모리스틱 파티션으로 확장하는 방법(pivotroot)
컴퓨터보안/네트웍세상 / 2011. 12. 27. 17:01
1. USB 메모리스틱의 파티션 생성
usb 메모리스틱을 openwrt에 꽂은 후의 로그는 다음과 같이 인식되어야 한다.
usb 메모리스틱을 openwrt에 꽂은 후의 로그는 다음과 같이 인식되어야 한다.
root@OpenWrt:/etc# logread
...
Dec 27 05:32:27 OpenWrt kern.info kernel: [ 2178.370000] usb 1-2: new high speed USB device number 3 using ar71xx-ehci
Dec 27 05:32:27 OpenWrt kern.info kernel: [ 2178.530000] scsi1 : usb-storage 1-2:1.0
Dec 27 05:32:28 OpenWrt kern.notice kernel: [ 2179.530000] scsi 1:0:0:0: Direct-Access Generic Flash Disk 8.07 PQ: 0 ANSI: 2
Dec 27 05:32:28 OpenWrt kern.notice kernel: [ 2179.540000] sd 1:0:0:0: [sda] 1978368 512-byte logical blocks: (1.01 GB/966 MiB)
Dec 27 05:32:28 OpenWrt kern.notice kernel: [ 2179.550000] sd 1:0:0:0: [sda] Write Protect is off
Dec 27 05:32:28 OpenWrt kern.debug kernel: [ 2179.550000] sd 1:0:0:0: [sda] Mode Sense: 03 00 00 00
Dec 27 05:32:28 OpenWrt kern.err kernel: [ 2179.550000] sd 1:0:0:0: [sda] Assuming drive cache: write through
Dec 27 05:32:28 OpenWrt kern.err kernel: [ 2179.560000] sd 1:0:0:0: [sda] Assuming drive cache: write through
Dec 27 05:32:29 OpenWrt kern.info kernel: [ 2179.860000] sda:
Dec 27 05:32:29 OpenWrt kern.err kernel: [ 2179.860000] sd 1:0:0:0: [sda] Assuming drive cache: write through
Dec 27 05:32:29 OpenWrt kern.notice kernel: [ 2179.870000] sd 1:0:0:0: [sda] Attached SCSI removable disk
usb 메모리스틱은 /dev/sda 라는 디바이스명으로 인식되었다.
해당 디바이스의 파티션을 확인한다.
root@OpenWrt:/etc# fdisk -l /dev/sda
Disk /dev/sda: 1012 MB, 1012924416 bytes
32 heads, 61 sectors/track, 1013 cylinders
Units = cylinders of 1952 * 512 = 999424 bytes
Disk identifier: 0xb0bcd68e
Device Boot Start End Blocks Id System
root@OpenWrt:/etc#
디스크 확장을 위한 공간을 생성한다.
스왑공간도 생성한다.
root@OpenWrt:/etc# fdisk /dev/sda
Command (m for help): p
Disk /dev/sda: 1012 MB, 1012924416 bytes
32 heads, 61 sectors/track, 1013 cylinders
Units = cylinders of 1952 * 512 = 999424 bytes
Disk identifier: 0xb0bcd68e
Device Boot Start End Blocks Id System
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1013, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-1013, default 1013): 913
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (914-1013, default 914):
Using default value 914
Last cylinder or +size or +sizeM or +sizeK (914-1013, default 1013):
Using default value 1013
Command (m for help): p
Disk /dev/sda: 1012 MB, 1012924416 bytes
32 heads, 61 sectors/track, 1013 cylinders
Units = cylinders of 1952 * 512 = 999424 bytes
Disk identifier: 0xb0bcd68e
Device Boot Start End Blocks Id System
/dev/sda1 1 913 891057+ 83 Linux
/dev/sda2 914 1013 97600 83 Linux
Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 82
Changed system type of partition 2 to 82 (Linux swap / Solaris)
Command (m for help): p
Disk /dev/sda: 1012 MB, 1012924416 bytes
32 heads, 61 sectors/track, 1013 cylinders
Units = cylinders of 1952 * 512 = 999424 bytes
Disk identifier: 0xb0bcd68e
Device Boot Start End Blocks Id System
/dev/sda1 1 913 891057+ 83 Linux
/dev/sda2 914 1013 97600 82 Linux swap / Solaris
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
root@OpenWrt:/etc# fdisk -l /dev/sda
Disk /dev/sda: 1012 MB, 1012924416 bytes
32 heads, 61 sectors/track, 1013 cylinders
Units = cylinders of 1952 * 512 = 999424 bytes
Disk identifier: 0xb0bcd68e
Device Boot Start End Blocks Id System
/dev/sda1 1 913 891057+ 83 Linux
/dev/sda2 914 1013 97600 82 Linux swap / Solaris
root@OpenWrt:/etc#
2. 파티션에 리눅스 파일시스템 생성
스왑공간을 활성화하고, 파티션에 리눅스 파일시스템을 생성한다.
먼저 리눅스 파일시스템 생성을 위한 프로그램을 설치해야 한다.
root@OpenWrt:/etc# opkg list-installed|grep fs
kmod-fs-ext4 - 2.6.39.4-1
libext2fs - 1.41.12-2
root@OpenWrt:/etc# opkg install e2fsprogs
Installing e2fsprogs (1.41.12-2) to root...
Downloading http://downloads.openwrt.org/snapshots/trunk/ar71xx/packages/e2fsprogs_1.41.12-2_ar71xx.ipk.
Configuring e2fsprogs.
root@OpenWrt:/etc# sync
root@OpenWrt:/etc# opkg files e2fsprogs
Package e2fsprogs (1.41.12-2) is installed on root and has the following files:
/usr/sbin/mkfs.ext2
/usr/sbin/mkfs.ext3
/usr/sbin/mkfs.ext4
/usr/lib/libe2p.so.2
/lib/functions/fsck/e2fsck.sh
/usr/sbin/e2fsck
/etc/e2fsck.conf
/usr/sbin/mke2fs
/usr/lib/libe2p.so.2.3
root@OpenWrt:/etc#
리눅스 파일시스템을 생성한다.
root@OpenWrt:/etc# mkfs.ext4 /dev/sda1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
55776 inodes, 222764 blocks
11138 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=230686720
7 block groups
32768 blocks per group, 32768 fragments per group
7968 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 21 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
3. 파일시스템 마운트와 운영체제 파일 복사
usb 메모리스틱에 파일시스템이 정상적으로 생성되었는지를 확인한다.
스왑영역을 활성화하고,
/mnt/root 라는 디렉토리를 만들어서, 그곳에 새로운 파일시스템 공간을 마운트시킨다.
root@OpenWrt:/etc# swapon /dev/sda2
root@OpenWrt:/etc# mount
rootfs on / type rootfs (rw)
/dev/root on / type jffs2 (rw,noatime)
proc on /proc type proc (rw,noatime)
sysfs on /sys type sysfs (rw,noatime)
tmpfs on /tmp type tmpfs (rw,nosuid,nodev,noatime,size=30964k)
tmpfs on /dev type tmpfs (rw,noatime,size=512k,mode=755)
devpts on /dev/pts type devpts (rw,noatime,mode=600)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
none on /proc/bus/usb type usbfs (rw,relatime)
root@OpenWrt:/etc# ll /mnt
drwxr-xr-x 2 root root 0 Dec 19 21:57 .
drwxr-xr-x 17 root root 0 Jan 1 1970 ..
root@OpenWrt:/etc# mkdir -p /mnt/root/
root@OpenWrt:/etc# ll /mnt
drwxr-xr-x 3 root root 0 Dec 27 05:52 .
drwxr-xr-x 17 root root 0 Jan 1 1970 ..
drwxr-xr-x 2 root root 0 Dec 27 05:52 root
root@OpenWrt:/etc# mount -t ext4 /dev/sda1 /mnt/root
root@OpenWrt:/etc# mount
rootfs on / type rootfs (rw)
/dev/root on / type jffs2 (rw,noatime)
proc on /proc type proc (rw,noatime)
sysfs on /sys type sysfs (rw,noatime)
tmpfs on /tmp type tmpfs (rw,nosuid,nodev,noatime,size=30964k)
tmpfs on /dev type tmpfs (rw,noatime,size=512k,mode=755)
devpts on /dev/pts type devpts (rw,noatime,mode=600)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
none on /proc/bus/usb type usbfs (rw,relatime)
/dev/sda1 on /mnt/root type ext4 (rw,relatime,user_xattr,barrier=1,data=ordered)
기존의 파일시스템을 확장된 디스크 영역으로 복사한다.
root@OpenWrt:~# cd /mnt
root@OpenWrt:/mnt# ll
drwxr-xr-x 3 root root 0 Dec 27 05:52 .
drwxr-xr-x 17 root root 0 Jan 1 1970 ..
drwxr-xr-x 3 root root 4096 Dec 27 05:45 root
root@OpenWrt:/mnt# tar cvO -C / bin/ etc/ root/ lib/ sbin/ usr/ www/ var/ | tar x -C /mnt/root
bin/
bin/dd
bin/cp
bin/df
bin/ln
bin/ls
bin/mv
bin/ps
bin/rm
bin/sh
bin/vi
... 중략 ...
usr/sbin/chroot
usr/sbin/mkfs.ext2
usr/sbin/mkfs.ext3
usr/sbin/mkfs.ext4
usr/sbin/iwlist
usr/sbin/iwpriv
usr/sbin/pptpctrl
usr/sbin/mke2fs
usr/sbin/fw_printenv
usr/sbin/bcrelay
usr/sbin/openvpn
usr/sbin/wpa_supplicant
usr/share/
usr/share/udhcpc/
usr/share/udhcpc/default.script
www/
var
root@OpenWrt:/mnt# mkdir -p /mnt/root/tmp && mkdir -p /mnt/root/dev && mkdir -p /mnt/root/proc && mkdir -p /mnt
/root/jffs && mkdir -p /mnt/root/mnt/openwrt && mkdir -p /mnt/root/sys
root@OpenWrt:/mnt# ll
drwxr-xr-x 3 root root 0 Dec 27 05:52 .
drwxr-xr-x 17 root root 0 Jan 1 1970 ..
drwxr-xr-x 16 root root 4096 Dec 27 05:57 root
root@OpenWrt:/mnt# ll ./root/
drwxr-xr-x 16 root root 4096 Dec 27 05:57 .
drwxr-xr-x 3 root root 0 Dec 27 05:52 ..
drwxr-xr-x 2 root root 4096 Dec 27 05:56 bin
drwxr-xr-x 2 root root 4096 Dec 27 05:57 dev
drwxr-xr-x 14 root root 4096 Dec 27 05:56 etc
drwxr-xr-x 2 root root 4096 Dec 27 05:57 jffs
drwxr-xr-x 12 root root 4096 Dec 27 05:56 lib
drwx------ 2 root root 16384 Dec 27 05:45 lost+found
drwxr-xr-x 3 root root 4096 Dec 27 05:57 mnt
drwxr-xr-x 2 root root 4096 Dec 27 05:57 proc
drwxr-xr-x 2 root root 4096 Dec 27 05:56 root
drwxr-xr-x 2 root root 4096 Dec 27 05:56 sbin
drwxr-xr-x 2 root root 4096 Dec 27 05:57 sys
drwxr-xr-x 2 root root 4096 Dec 27 05:57 tmp
drwxr-xr-x 6 root root 4096 Dec 27 05:56 usr
lrwxrwxrwx 1 root root 4 Dec 27 05:56 var -> /tmp
drwxr-xr-x 2 root root 4096 Dec 19 21:57 www
root@OpenWrt:/mnt#
4. 부팅 스크립트 수정
다음으로, 부팅스트립트 조정하기
/etc/init.d/rcS 스크립트에 루트 파일시스템을 변경하는 내용 추가
root@OpenWrt:/etc# cd /etc/init.d
root@OpenWrt:/etc/init.d# ll
drwxr-xr-x 2 root root 0 Dec 19 21:58 .
drwxr-xr-x 14 root root 0 Dec 27 05:44 ..
-rwxr-xr-x 1 root root 3263 Nov 18 00:28 boot
-rwxr-xr-x 1 root root 385 Dec 19 22:02 cron
-rwxr-xr-x 1 root root 318 Nov 9 21:52 defconfig
-rwxr-xr-x 1 root root 10032 Dec 19 22:04 dnsmasq
-rwxr-xr-x 1 root root 318 Mar 21 2011 done
-rwxr-xr-x 1 root root 4367 Dec 19 22:05 dropbear
-rwxr-xr-x 1 root root 233 Dec 19 22:06 firewall
-rwxr-xr-x 1 root root 2004 Dec 19 22:01 fstab
-rwxr-xr-x 1 root root 1570 Mar 21 2011 led
-rwxr-xr-x 1 root root 641 Nov 5 23:38 network
-rwxr-xr-x 1 root root 4657 Dec 19 22:14 openvpn
-rwxr-xr-x 1 root root 375 Dec 20 07:12 pptpd
-rwxr-xr-x 1 root root 454 Mar 21 2011 rcS
-rwxr-xr-x 1 root root 126 Mar 21 2011 sysctl
-rwxr-xr-x 1 root root 505 Nov 9 21:52 sysntpd
-rwxr-xr-x 1 root root 878 Dec 19 22:02 telnet
-rwxr-xr-x 1 root root 673 Dec 20 09:34 uboot-envtools
-rwxr-xr-x 1 root root 99 Mar 21 2011 umount
-rwxr-xr-x 1 root root 154 Mar 21 2011 usb
-rwxr-xr-x 1 root root 244 Nov 9 21:52 watchdog
root@OpenWrt:/etc/init.d# vi rcS
#!/bin/sh
# Copyright (C) 2006 OpenWrt.org
run_scripts() {
for i in /etc/rc.d/$1*; do
[ -x $i ] && $i $2 2>&1
done | $LOGGER
}
system_config() {
config_get_bool foreground $1 foreground 0
}
LOGGER="cat"
[ -x /usr/bin/logger ] && LOGGER="logger -s -p 6 -t sysinit"
#--------------------------------------------------------------------
# Switch to pivoroot if available
#
if [ "$2" == "boot" -a -x /etc/init.d/pivotroot ] ; then
echo "[`date`]: Pivot root log created." > /tmp/pivotroot.log
/etc/init.d/pivotroot >> /tmp/pivotroot.log
fi
#--------------------------------------------------------------------
. /etc/functions.sh
config_load system
config_foreach system_config system
if [ "$1" = "S" -a "$foreground" != "1" ]; then
run_scripts "$1" "$2" &
else
run_scripts "$1" "$2"
fi
~
root@OpenWrt:/etc/init.d#
/etc/init.d/ 디렉토리 밑에 pivotroot 라는 이름으로 다음 내용을 작성하고,
root@OpenWrt:/etc/init.d# vi pivotroot
if [ -z $DRIVE ]; then
echo No partitions, skipping pivotroot
else
umount $DRIVE
e2fsck -p $DRIVE
fsstat=$?
echo e2fsck status is $fsstat
if [ $fsstat -eq 2 ]; then
echo Corrected errors on $DRIVE, need to reboot
sleep 5
reboot
elif [ $fsstat -eq 1 ]; then
echo Corrected errors on $DRIVE
elif [ $fsstat -gt 2 ]; then
echo No usable filesystem on $DRIVE
fi
echo Mounting $DRIVE
mount -t ext4 $DRIVE /mnt/openwrt
mt=$?
if [ $mt -eq 0 ]; then
if [ -x /mnt/openwrt/sbin/init -a -d /mnt/openwrt/jffs -a -d /mnt/openwrt/mnt/openwrt ]; then
echo Found OpenWRT root on $DRIVE
# Side-effect - leave /mnt/openwrt mounted
else
echo "Missing /sbin/init, /jffs, or /mnt/openwrt (mount status was $mt)"
[ -x /mnt/openwrt/sbin/init ] || echo Failed -x /sbin/init
[ -d /mnt/openwrt/mnt/openwrt ] || echo Failed -x /mnt/openwrt
[ -d /mnt/openwrt/jffs ] || echo Failed -x /jffs
umount $DRIVE
fi
else
echo mount status is $mt
fi
fi
#if everything looks ok, do the pivot root
[ -x /mnt/openwrt/sbin/init ] && {
mount -o move /proc /mnt/openwrt/proc && \
pivot_root /mnt/openwrt /mnt/openwrt/mnt/openwrt && {
mount -o move /mnt/openwrt/dev /dev
mount -o move /mnt/openwrt/tmp /tmp
mount -o move /mnt/openwrt/jffs2 /jffs2 2>&-
mount -o move /mnt/openwrt/sys /sys 2>&-
}
}
root@OpenWrt:/etc/init.d# sync
root@OpenWrt:/etc/init.d# ll
drwxr-xr-x 2 root root 0 Dec 27 06:07 .
drwxr-xr-x 14 root root 0 Dec 27 05:44 ..
-rwxr-xr-x 1 root root 3263 Nov 18 00:28 boot
-rwxr-xr-x 1 root root 385 Dec 19 22:02 cron
-rwxr-xr-x 1 root root 318 Nov 9 21:52 defconfig
-rwxr-xr-x 1 root root 10032 Dec 19 22:04 dnsmasq
-rwxr-xr-x 1 root root 318 Mar 21 2011 done
-rwxr-xr-x 1 root root 4367 Dec 19 22:05 dropbear
-rwxr-xr-x 1 root root 233 Dec 19 22:06 firewall
-rwxr-xr-x 1 root root 2004 Dec 19 22:01 fstab
-rwxr-xr-x 1 root root 1570 Mar 21 2011 led
-rwxr-xr-x 1 root root 641 Nov 5 23:38 network
-rwxr-xr-x 1 root root 4657 Dec 19 22:14 openvpn
-rw-r--r-- 1 root root 2110 Dec 27 06:14 pivotroot
-rwxr-xr-x 1 root root 375 Dec 20 07:12 pptpd
-rwxr-xr-x 1 root root 669 Dec 27 06:03 rcS
-rwxr-xr-x 1 root root 126 Mar 21 2011 sysctl
-rwxr-xr-x 1 root root 505 Nov 9 21:52 sysntpd
-rwxr-xr-x 1 root root 878 Dec 19 22:02 telnet
-rwxr-xr-x 1 root root 673 Dec 20 09:34 uboot-envtools
-rwxr-xr-x 1 root root 99 Mar 21 2011 umount
-rwxr-xr-x 1 root root 154 Mar 21 2011 usb
-rwxr-xr-x 1 root root 244 Nov 9 21:52 watchdog
pivotroot 파일에 실행권한을 부여한다.
root@OpenWrt:/etc/init.d# chmod 755 pivotroot
위의 파일시스템 변경 스크립트에서 기본 마운트 포인트가 /mnt/openwrt 로 되어있다.
기존에 마운트해서 파일을 사용했던. /mnt/root 폴더 대신에, /mnt/openwrt 로 이름을 변경한다.
root@OpenWrt:/etc/init.d# cd /mnt
root@OpenWrt:/mnt# mount
rootfs on / type rootfs (rw)
/dev/root on / type jffs2 (rw,noatime)
proc on /proc type proc (rw,noatime)
sysfs on /sys type sysfs (rw,noatime)
tmpfs on /tmp type tmpfs (rw,nosuid,nodev,noatime,size=30964k)
tmpfs on /dev type tmpfs (rw,noatime,size=512k,mode=755)
devpts on /dev/pts type devpts (rw,noatime,mode=600)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
none on /proc/bus/usb type usbfs (rw,relatime)
/dev/sda1 on /mnt/root type ext4 (rw,relatime,user_xattr,barrier=1,data=ordered)
root@OpenWrt:/mnt# umount /dev/sda1
root@OpenWrt:/mnt# mount
rootfs on / type rootfs (rw)
/dev/root on / type jffs2 (rw,noatime)
proc on /proc type proc (rw,noatime)
sysfs on /sys type sysfs (rw,noatime)
tmpfs on /tmp type tmpfs (rw,nosuid,nodev,noatime,size=30964k)
tmpfs on /dev type tmpfs (rw,noatime,size=512k,mode=755)
devpts on /dev/pts type devpts (rw,noatime,mode=600)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
none on /proc/bus/usb type usbfs (rw,relatime)
root@OpenWrt:/mnt# ll
drwxr-xr-x 3 root root 0 Dec 27 06:15 .
drwxr-xr-x 17 root root 0 Jan 1 1970 ..
drwxr-xr-x 2 root root 0 Dec 27 05:52 root
root@OpenWrt:/mnt# mv root openwrt
root@OpenWrt:/mnt# ll
drwxr-xr-x 3 root root 0 Dec 27 06:16 .
drwxr-xr-x 17 root root 0 Jan 1 1970 ..
drwxr-xr-x 2 root root 0 Dec 27 05:52 openwrt
root@OpenWrt:/mnt# sync
5. 부팅 파티션의 변경 완료 확인
공유기를 재부팅해서, 정상적으로 파일시스템이 이전되었는지를 확인한다.
root@OpenWrt:/mnt# sync
root@OpenWrt:/mnt# reboot
root@OpenWrt:/mnt#
Connection closed by foreign host.
Type `help' to learn how to use Xshell prompt.
Xshell:\>
Connecting to 192.168.43.1:8080...
Connection established.
Escape character is '^@]'.
sh: /usr/bin/X11/xauth: not found
BusyBox v1.19.3 (2011-12-19 15:01:26 MST) built-in shell (ash)
Enter 'help' for a list of built-in commands.
_______ ________ __
| |.-----.-----.-----.| | | |.----.| |_
| - || _ | -__| || | | || _|| _|
|_______|| __|_____|__|__||________||__| |____|
|__| W I R E L E S S F R E E D O M
ATTITUDE ADJUSTMENT (bleeding edge, r29570) ----------
* 1/4 oz Vodka Pour all ingredients into mixing
* 1/4 oz Gin tin with ice, strain into glass.
* 1/4 oz Amaretto
* 1/4 oz Triple sec
* 1/4 oz Peach schnapps
* 1/4 oz Sour mix
* 1 splash Cranberry juice
-----------------------------------------------------
root@OpenWrt:~# df -h
Filesystem Size Used Available Use% Mounted on
rootfs 856.5M 27.0M 786.0M 3% /
/dev/root 5.1M 4.2M 864.0K 83% /mnt/openwrt
tmpfs 30.2M 76.0K 30.2M 0% /tmp
tmpfs 512.0K 0 512.0K 0% /dev
/dev/sda1 856.5M 27.0M 786.0M 3% /
root@OpenWrt:~# cat /tmp/
.uci/ lock/ pivotroot.log run/
TZ log/ resolv.conf state/
dhcp.leases openvpn-status.log resolv.conf.auto sysinfo/
root@OpenWrt:~# cat /tmp/pivotroot.log
[Thu Jan 1 00:00:35 UTC 1970]: Pivot root log created.
/dev/sda1: clean, 840/55776 files, 10418/222764 blocks
e2fsck status is 0
Mounting /dev/sda1
Found OpenWRT root on /dev/sda1
root@OpenWrt:~#
디스크 정보와 로그에서 완벽하게 이전되었음을 확인할 수 있다.