A file device

everything is a file

© 2010, 2011, 2012 Dennis Leeuw dleeuw at made-it dot com
License: GPLv2 or later

Index

    1. A simple setup
    2. A partioned disk
    3. Creating large disks
    4. Creating a broken disk

A simple setup

To play around with disks and test what is described you might not want to destroy your currently available disks. If you do not have spare disks to work with, you could create some disk images files. To do so first create one or more files like this:

dd if=/dev/zero of=disk-image bs=1 count=1M
This creates a file called "disk-image" of 1 Megabyte only containing zero's. Note though that you atleast need to set count=2M if you want to format the image with a journal (ext3). The remainder of the text will assume a 3M file.

Since we are now having a flat file with nothing on it we should make it more like a disk:

parted disk-image mklabel loop
If you have an image that is larger then 2 Terabyte use gpt as the disk label.

Next we will create a single partition spanning the entire "disk":

parted disk-image mkpart primary ext2 0 3M
If you want to test LVM or RAID, you need to set the following too:
parted disk-image set 1 lvm on
And use the RAID or LVM tools to configure the device.

Assuming that we, at this point, do not use RAID or LVM, we need a way to treat the created file as a "normal" block device:

losetup /dev/loop0 disk-image
This assumes that this is the first loop device we use on our system. Use:
losetup -a
To view all loop devices and where they are connected to or use:
losetup -f
To find the first free available loop device.

From this point on we can use our normal tools to deal with the file. With the mkfs tools you can create a filesystem on it:

mkfs.ext3 -b 2048 -L image1 /dev/loop0
This creates an Ext3 filesystem with a blocksize of 2048 and a volume name of "image1". To view what the superblock has to say about the created image use:
tune2fs -l /dev/loop0

To be able to access the contents, we need to mount it:

mount -t ext3 /dev/loop0 /mnt
If you do a ls of the /mnt directory you should now see a "lost+found" directory.

To remove everything we have done:

umount /mnt
losetup -d /dev/loop0
rm -f disk-image

A partioned disk

Another option is create a disk image including a disk partition, like using dd to copy a disk to a file including all partitions. To access such an image one would need to create loop-devices for every partition on the disk. To adjust the disk-image use the following procedure:
$ fdisk -l ~/firewalls/fw01.dsk

That will output something like this:

Disk ~/firewalls/fw01.dsk: 114.2 GB, 114215415808 bytes
255 heads, 63 sectors/track, 13885 cylinders, total 223076984 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xee087596

               Device Boot      Start         End      Blocks   Id  System
~/firewalls/fw01.dsk1   *        2048    17577983     8787968   83  Linux
~/firewalls/fw01.dsk2        17580030   250068991   116244481    5  Extended
~/firewalls/fw01.dsk5        17580032    23437311     2928640   83  Linux
~/firewalls/fw01.dsk6        23439360    39690239     8125440   82  Linux swap / Solaris
~/firewalls/fw01.dsk7        39692288    40470527      389120   83  Linux
~/firewalls/fw01.dsk8        40472576   250068991   104798208   83  Linux

Per device calculate the offet. For our root system that starts at 2048 we do: 2048*512 = 1048576. So the loop-device associated with the root of our system can be created like this:

losetup /dev/loop0 ~/firewalls/fw01.dsk -o $((2048 * 512))
mount /dev/loop0 /mnt

losetup /dev/loop1 /home/bofh/dleeuw/fw01.dsk -o $((17580032*512))
mount /dev/loop1 /mnt/var
losetup /dev/loop2 /home/bofh/dleeuw/fw01.dsk -o $((39692288*512))
mount /dev/loop2 /mnt/tmp
losetup /dev/loop3 /home/bofh/dleeuw/fw01.dsk -o $((40472576*512))
mount /dev/loop3 /mnt/home

Creating large disks

A zero map is defined as: start length zero

Creating a broken disk

A error map is defined as: start length error