mount disk Image
[ 2011/07/19 15:14 | by selboo ]
如果你今天面对的是disk image而不是一般的partition image,当使用mount -o loop是将无法mount成功。这很容易理解,因为你必须知道disk image中partition的位置之后才能mount起來,怎么做呢?
需求: mount test.img 并更改第一个partition中的某个文件
我们先看看如果直接用mount -o loop会如何:
lawrence@lawrence-x24:~/Desktop$ sudo mount -o loop test.img /mnt/test/
mount: you must specify the filesystem type
lawrence@lawrence-x24:~/Desktop$ sudo mount -o loop -t ext3 test.img /mnt/test/
mount: wrong fs type, bad opti[[separator]separator]on, bad superblock on /dev/loop0,
missing codepage or helper program, or other error
In some cases useful info is found in syslog – try
dmesg | tail or so
mount: you must specify the filesystem type
lawrence@lawrence-x24:~/Desktop$ sudo mount -o loop -t ext3 test.img /mnt/test/
mount: wrong fs type, bad opti[[separator]separator]on, bad superblock on /dev/loop0,
missing codepage or helper program, or other error
In some cases useful info is found in syslog – try
dmesg | tail or so
恩,看起来是不work……
所以我必须先知道test.img的磁轨与磁区大小才能算出第一个partition的位置:
lawrence@lawrence-x24:~/Desktop$ fdisk -l test.img
You must set cylinders.
You can do this from the extra functions menu.
Disk test.img: 0 MB, 0 bytes
16 heads, 63 sectors/track, 0 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Disk identifier: 0×00000000
所用设备 Boot Start End Blocks Id System
test.img1 1 32 16096+ 83 Linux
You must set cylinders.
You can do this from the extra functions menu.
Disk test.img: 0 MB, 0 bytes
16 heads, 63 sectors/track, 0 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Disk identifier: 0×00000000
所用设备 Boot Start End Blocks Id System
test.img1 1 32 16096+ 83 Linux
得知每个 track 共 63 sectors, 每个 sector 是 512bytes,故得知第一个partition是在63*512=32256。
接下来利用losetup将第一个partition先挂到/dev/loop0,并验证:
lawrence@lawrence-x24:~/Desktop$ sudo losetup –offset 32256 /dev/loop0 test.img
lawrence@lawrence-x24:~/Desktop$ sudo losetup /dev/loop0
/dev/loop0: [0803]:865823 (test.img), offset 32256
lawrence@lawrence-x24:~/Desktop$ sudo losetup /dev/loop0
/dev/loop0: [0803]:865823 (test.img), offset 32256
好了,将它mount起來
lawrence@lawrence-x24:~/Desktop$ sudo mount /dev/loop0 /mnt/test/
lawrence@lawrence-x24:~/Desktop$ df -h | grep test
/dev/loop0 16M 7.9M 6.6M 55% /mnt/test
lawrence@lawrence-x24:~/Desktop$ df -h | grep test
/dev/loop0 16M 7.9M 6.6M 55% /mnt/test
修改完后,同样将其umount掉并detach /dev/loop0
lawrence@lawrence-x24:/mnt/test$ cd
lawrence@lawrence-x24:~$ sudo umount /mnt/test
lawrence@lawrence-x24:~$ df -h | grep test
lawrence@lawrence-x24:~$ sudo losetup -d /dev/loop0
lawrence@lawrence-x24:~$ sudo losetup /dev/loop0
loop: can’t get info on device /dev/loop0:
lawrence@lawrence-x24:~$ sudo umount /mnt/test
lawrence@lawrence-x24:~$ df -h | grep test
lawrence@lawrence-x24:~$ sudo losetup -d /dev/loop0
lawrence@lawrence-x24:~$ sudo losetup /dev/loop0
loop: can’t get info on device /dev/loop0:
沒有此一设备或位置
以上希望对大家有帮助。
PS. Redhat中如果要位移,是使用losetup -o 32256而不是losetup –offset 32256
补充一下如果要mount第二个partition可以这样:
1. fdisk -lu test.img
得知第二个partition开始的sector, so offset=sector * sector size (512bytes)
fdisk -u When listing partition tables, give sizes in sectors instead of cylinders.
PS. 更精简的mount方法:
mount -o loop,offset=32256 test.img /mnt/test
============
例子:
liubin@liubin-laptop:$ fdisk -lu redhat6.img
You must set cylinders.
You can do this from the extra functions menu.
Disk redhat6.img: 0 MB, 0 bytes
16 heads, 63 sectors/track, 0 cylinders, total 0 sectors
Units = sectors of 1 * 512 = 512 bytes
Disk identifier: 0×00000000
Device Boot Start End Blocks Id System
redhat6.img1 * 63 33263 16600+ 83 Linux
redhat6.img2 33264 1032191 499464 5 Extended
redhat6.img5 33327 99791 33232+ 82 Linux swap / Solaris
redhat6.img6 99855 1032191 466168+ 83 Linux
You must set cylinders.
You can do this from the extra functions menu.
Disk redhat6.img: 0 MB, 0 bytes
16 heads, 63 sectors/track, 0 cylinders, total 0 sectors
Units = sectors of 1 * 512 = 512 bytes
Disk identifier: 0×00000000
Device Boot Start End Blocks Id System
redhat6.img1 * 63 33263 16600+ 83 Linux
redhat6.img2 33264 1032191 499464 5 Extended
redhat6.img5 33327 99791 33232+ 82 Linux swap / Solaris
redhat6.img6 99855 1032191 466168+ 83 Linux
可以看出redhat6.img有4个分区,其中第一个分区redhat6.img1为主分区,是加载linux时用的。另外有扩展分区,交换分区,和linux使用的ext2文件系统分区。
要挂载主分区redhat6.img1时,首先计算第一个partition的偏移字节:63*512=32256
然后使用命令:
liubin@liubin-laptop:$ sudo mount -t ext2 -o loop,offset=32256 redhat6.img /mnt
挂载主分区。
要挂载redhat6.img6时,首先计算partition的偏移字节: 99855 *512=51125760
然后使用命令:
liubin@liubin-laptop:$ sudo mount -t ext2 -o loop,offset=51125760 redhat6.img /mnt
挂载
最后编辑: selboo 编辑于2011/08/21 01:37