In this recipe, we will create a virtual block device that we will use to simulate real devices and partitions so that we can test-drive concepts and commands used in all later recipes in this chapter. Working with real disks and partitions often involves the risk of losing important data or even having to re-install your complete system. A virtual block device is ideal to learn the techniques and try things out before switching to "production mode". Later, if you have gained enough experience and feel safe, you can easily replace it with "real" hardware devices, partitions, and logical volumes (which is a part of LVM; see the later recipe). All you need to do is substitute your virtual device with "real" block device names.
To complete this recipe, you will require a minimal installation of the CentOS 7 operating system with root access. To create a virtual block device, you should have at least one gigabyte of free hard disk space that we will use temporarily to create and make. You can delete this reserved space later (or it will be automatically deleted on reboot). It's just for testing.
root
and create an empty file with the exact size of 1 gigabyte:dd if=/dev/zero of=/tmp/test-image.dd bs=1M count=1000
losetup -fP /tmp/test-image.dd
losetup -a
loop0
can be a different number if you have created a loop device before):/dev/loop0: [0035]:3714186 (/tmp/test-image.dd)
lsblk -io NAME,TYPE,SIZE,MOUNTPOINT,FSTYPE,MODEL
gpt
on our new loop device (confirm the deletion of any data):parted /dev/loop0 mklabel gpt
kpartx -a /dev/loop0
In this recipe, we have learned how to create a virtual block device that acts as a starting point for testing out how to create partitions, logical volumes, and filesystems in later recipes in this chapter.
So, what did we learn from this experience?
We started this recipe by creating a new empty file, which was one gigabyte in size, in the /tmp
directory using the dd
utility. dd
is used to make exact copies of files (which is sometimes called cloning) and expects two parameters: an input file (the if
parameter) and an output file (the of
parameter). We used the zero
device (/dev/zero
) as our input file that returns an endless stream of bytes containing zero. We then limited the stream by defining a blocksize (bs
) and count
parameter. The bs
defines the amount of data in bytes read at a time, while the count
parameter counts how many repetitions of bs
will be allowed. So, these arguments can be read as stop the copying process when we reach a blocksize times count data received. In our example, we used a blocksize of 1
Megabyte times 1000 = 1 Gigabyte. This zero byte data was written to our output file (of
) called /tmp/test-image.dd
.
After we created this empty file, we created a temporary loop device with it. A loop device is just a pseudo-device that makes it possible to use a file as a block device. Often, such a file is a CD ISO image, and using it as a loop device will make it accessible as if it were a normal hardware drive. Any device that allows reading or writing data in blocks can be called a block device; in order to get a list of all available block devices in your system, we used the lsblk
command, and as you can see, this includes our loop device as well. Standard loop device names start with the number zero, as in /dev/loop0
.
Afterwards, we created a new
partition table on our loop device using the parted
command. A partition table is a table maintained on a disk by the operating system describing the partitions on it, and it must be created before we can create them. We used the partition table type gpt
, but you can also use the old msdos type here instead.
Normally, when creating a partition table on a virtual block device, we cannot access individual partitions or make filesystems for different partitions on it, because the partitions cannot be addressed individually. Here we used the kpartx
command to create device mappings from partition tables, which allows us later to access single partitions for creating filesystems using the notation, /dev/loop0p1
, for partition 1 on loop device 0 and /dev/loop0p2
for partition 2 on loop device 0.
Congratulations, you have now created a brand new virtual block device with a standard partition table, which can be used and accessed as if it were a normal disk device.
If we want to remove a virtual block device, we first have to unmount it from the filesystem if it is currently mounted (for example, umount /dev/loo0p1
). Next, we need to detach the virtual block device file from the loop device using the -d
parameter like so: losetup -d /dev/loop0
. Afterwards, we can delete the block file if we want to: rm /tmp/test-image.dd
.