天天看點

How to create a filesystem on a file

We can use loop device to achieve this requirement.

Floppy Disk Images

First, lets create a empty image.

# dd if=/dev/zero of=floppy.img bs=512 count=2880

Now, lets set it up for mounting.

# losetup /dev/loop0 floppy.img

Now lets make it EXT2 formatted.

# mkfs -t ext2 /dev/loop0

Mount!

# mount -t ext2 /dev/loop0 /mnt/myfloppy

Now any file you put into /mnt/virtual is actually being put directly into /mnt/myfloppy

Virtual Disk Images

A virtual disk image is going to have a partition table that defines some number of partitions, and each partition contains its own filesystem;

As same as above:

# dd if=/dev/zero of=floppy.img bs=512 count=2880

# losetup /dev/loop0 floppy.img

By attaching the disk image to the loopback device, we can use /dev/loop0 the same way we would us floppy.img

# fdisk /dev/loop0 

within fdisk, choose "n" -> "p" -> "1" -> enter ->enter -> "w", after that, you will create a new partition.

use "fdisk -ul /dev/loop0" to check it. 

Disk /dev/loop0: 31 MB, 31457280 bytes

255 heads, 63 sectors/track, 3 cylinders, total 61440 sectors

Units = sectors of 1 * 512 = 512 bytes

      Device Boot      Start         End      Blocks   Id  System

/dev/loop0p1              63       48194       24066   83  Linux

The output from fdisk -ul /dev/loop0 shows us that the first partition starts at block 63,

and that each block is 512 bytes. So partition 1 starts at byte 32,256

So

# losetup -o 32256 /dev/loop1 /dev/loop0

now mkfs of the partition

# mkfs -t ext2 /dev/loop1

mount the partition

# mount /dev/loop1 /mnt

Great, now we got the available partition!!!

refs:

http://wiki.osdev.org/Loopback_device

http://web2.clarkson.edu/projects/itl/honeypot/ddtutorial.txt

http://www.richud.com/wiki/Ubuntu_Create_Hard_Drive_Image

繼續閱讀