fbpx Creating a CentOS virtual block device
24/7/365 Support

Creating a CentOS virtual block device

In this process, 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 processes. 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. All you need to do is substitute your virtual device with “real” block device names.

To Start With: What Do You Need?

To complete this process, 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.

The Process

  1. To begin, log in as 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
  2. Now, let’s create a loop device from the file we just created:
    losetup -fP /tmp/test-image.dd
  3. Next, print the generated loop device name:
    losetup -a
  4. As this will be the first loop device created in the current system, the output will be as follows (loop0 can be a different number if you have created a loop device before):
    /dev/loop0: [0035]:3714186 (/tmp/test-image.dd)
  5. To get a list of all the block devices currently attached to the system, as well as important details, type the following:
    lsblk -io NAME,TYPE,SIZE,MOUNTPOINT,FSTYPE,MODEL
  6.  Now, let’s create a new partition table of the type gpt on our new loop device (confirm the deletion of any data):
    parted /dev/loop0 mklabel gpt
  7. Finally, create device maps from your loop device to make it more similar to real hard disk partitions:
    kpartx -a /dev/loop0

How Does It Work?

In this process, 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 processes can be checked. 

So, what did we learn from this experience?

We started this process 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 devices (/dev/zero) as our input file that returns an endless stream of bytes containing zero. We then limited the stream by defining a block size (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 block size times count data received. In our example, we used a block size 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.

Afterward, 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.

There's more…

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. Afterward, we can delete the block file if we want to: rm /tmp/test-image.dd.

 

Help Category:

What Our Clients Say