everything is a file
© 2010, 2011, 2012 Dennis Leeuw dleeuw at made-it dot com
License: GPLv2 or later
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=1MThis 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 loopIf 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 3MIf you want to test LVM or RAID, you need to set the following too:
parted disk-image set 1 lvm onAnd 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-imageThis assumes that this is the first loop device we use on our system. Use:
losetup -aTo view all loop devices and where they are connected to or use:
losetup -fTo 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/loop0This 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 /mntIf 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
$ 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
A zero map is defined as: start length zero
A error map is defined as: start length error