LoopDevice笔记

#insmod loop.o
dd if=/dev/zero of=/tmp/fsfile bs=1k count=4096
#mknod /dev/loop0 b 7 0
losetup /dev/loop0 /tmp/fsfile
mke2fs /dev/loop0 4096
mount -t ext2 /dev/loop0 /mnt
umount /dev/loop0
losetup -d /dev/loop0
#rmmod loop

mke2fs -m 0 -i 2000 /dev/loop0

mount -o loop -t ext2 /tmp/fsfile /mnt

dd if=/dev/loop0 bs=1k gzip -v > rhat2.img

fdisk -lu /dev/loop0
losetup -o 32256 /dev/loop0 image.dd
# 32256 = sector 64 * 512 bytes

# mount -o loop,offset=32256 image.dd /mnt/

注意区分访问硬盘镜像文件的不同方式:

1. 硬盘无分区,即创建时仅做了格式化
losetup /dev/loop0 hdisk.img
mke2fs /dev/loop0
mount -o loop hdisk.img /mnt/loopdevice/
在操作系统中挂载/dev/hdb

2.硬盘创建了分区
fdisk中看到的信息
Command (m for help): x

Expert command (m for help): p

Disk /dev/loop0: 255 heads, 63 sectors, 2 cylinders

Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID
1 00 1 1 0 254 63 1 63 32067 83 <– start = 63 2 00 0 0 0 0 0 0 0 0 00 3 00 0 0 0 0 0 0 0 0 00 4 00 0 0 0 0 0 0 0 0 00 第一个分区的起始偏移63*512 = 32256

之后可用mount -o loop,offset=32256 hdisk.img /mnt/loopdevice/
或 losetup -o 32256 /dev/loop0 hdisk.img挂载该分区

操作系统中挂载/dev/sdb1

注意,如果在host环境中losetup /dev/loop0 hdisk.img ,再对/dev/loop0分区可能会出现分区表错误!
fdisk的信息:Disk /dev/loop0: 255 heads, 63 sectors, 2 cylinders
其中柱面数与创建磁盘镜像时不匹配(应为40)
最好在guest操作系统中对磁盘镜像进行分区
update:
或在fdisk下输x进入expert模式,手工设置C/H/S值(cylinder/header/sector)
dd if=~/.bochs/guest.hd0 of=/tmp/hd0 bs=512 skip=63 可用于去除文件系统信息

other info
=======
Encrypted Root Filesystem HOWTO
http://lfs.linuxsir.org/main/?q=node/78

update: 2008-07-25
=======
image上安装grub
% ./grub-0.95/grub/grub –no-floppy
grub> device (hd0) c.img

(此处曾尝试使用预先losetup过的/dev/loop0,但没有成功)

grub> geometry (hd0) 3047 16 63
drive 0x80: C/H/S = 3047/16/63, The number of sectors = 3071376, c.img Partition num: 0, Filesystem type is ext2fs, partition type 0x83

grub> root (hd0,0)
Filesystem type is ext2fs, partition type 0x83

grub> setup (hd0)
Checking if “/boot/grub/stage1” exists… yes
Checking if “/boot/grub/stage2” exists… yes
Checking if “/boot/grub/e2fs_stage1_5” exists… yes
Running “embed /boot/grub/e2fs_stage1_5 (hd0)”… 22 sectors are embedded.succeeded
Running “install /boot/grub/stage1 (hd0) (hd0)1+22 p (hd0,0)/boot/grub/stage2 /boot/grub/menu.lst”… succeeded

Done.
http://kbarr.net/bochs


相关文章

Speak Your Mind

If you want a pic to show with your comment, go get a gravatar!