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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值