Thursday, April 19, 2012

Creating empty files and using them as disks

If you ever needed some disks to test anything (eg. mdadm) then you can create your own harddisk in an existing filesystem. First create a 1GB file filled with zeros:

# dd if=/dev/zero of=/local/disk/vda bs=1 count=0 seek=1073741824
0+0 records in
0+0 records out
0 bytes (0 B) copied, 1.3252e-05 s, 0.0 kB/s

Set up the file as a loop device:

# losetup /dev/loop0 /local/disk/vda

Check that the loop device is available:

# losetup -a
/dev/loop0: [0803]:7605015 (/local/disk/vda)

Create a filesystem on the loop device:

# mkfs /dev/loop0

And mount it:

# mount /dev/loop0 /mnt/hd/
# df -h /mnt/hd
Filesystem            Size  Used Avail Use% Mounted on
/dev/loop0           1008M  1.3M  956M   1% /mnt/hd

To detach the loop device unmount the filesystem first:

# umount /mnt/hd

Then detach the loop0 device with losetup:

# losetup -d /dev/loop0

It is not possible to create one or more partition on a loop device this way!

No comments:

Post a Comment