- Creating Virtual Disks Using Linux Command Line
- Tools Requirements
- Create The Image and Format Partitions
- Images With Multiple Partitions
- Finish partitions
- Conclusion
- Guide to Virtual Disk Images
- 1. Overview
- 2. What Is a Virtual Disk Image
- 3. Virtual Disk Images
- 3.1. VMDK
- 3.2. VDI
- 3.3. VHD and VHDX
- 3.4. QCOW and QED
- 4. Choosing the Right VDI
- 5. Conclusion
- How to Create a Virtual Hard Disk Volume Using a File in Linux
- Step-by-Step Guide to Create a Virtual HardDisk Volume Using a File in Linux
- Step 1: Creating a New Image for Holding the Virtual Drive Volume
Creating Virtual Disks Using Linux Command Line
Linux is indeed a great system with excellent tools at our disposal. There are lots of things that can be achieved using the terminal. One such activity is creating virtual hard drives. Your Linux system should already have the tools required to do this without the need for virtual machine software.
NOTE: This tutorial only covers creating fixed-size disk images whose partitions can be mounted using Linux. Virtual machine programs like VirtualBox allow you to create dynamically expanding virtual drives that increase in size whenever necessary. If your goal is to create disks for programs such as VirtualBox, you need to use its tools.
Tools Requirements
The following commands that you will need are:
- ‘dd’ for creating the file. You can also bximage (part of the Bochs PC Emulator) if you wish.
- ‘fdisk’ for creating partitions, or which ever partition program you like.
- ‘mkfs’ for formatting the partitions.
- ‘losetup’ for setting up the loop devices for each partition.
You may use whatever tool you are comfortable with to achieve the same goal, of course.
Create The Image and Format Partitions
Creating the image is simple using ‘dd’. All this will do is write zeros to a file of a specified size. For example, let’s create a 1GB image: $ dd if=/dev/zero of=1GB_HDD.img bs=1M count=1024 This will take a little time. You may choose a smaller or larger size if you wish.
Once completed, a partition should be created using fdisk. Because there is no partition table, one will be created. This is the DOS partition table. Let’s switch to a GPT table by entering ‘g’ into the prompt to create one. Now, create a partition by entering ‘n’. Accept all of the defaults. The partition created will be in a native Linux format that can be either ext2, ext3, or ext4. Then write the changes to the image by entering ‘w’.
After the changes are written and fdisk closes, all that is required to do is format the partition running ‘mkfs.ext4’ on the image file itself to create an ext4 partition. It may ask you if you wish to proceed anyway if a GPT partition is found. If so, say yes.
If all went well, you can then proceed to setup a loop device for your image. This requires the use of ‘losetup’ (that is, loop setup). The command we wish to run will assign an available loop device (-f parameter to find one) to the partition on the image, and show the name of said loop device (–show parameter): $ sudo losetup -Pf –show 1GB_HDD.img
If successful, you should be able to access the partition by either using ‘mount’ or through your file manager.
Images With Multiple Partitions
That was how you create virtual drives with a single partition. What about images with two or more partitions? There are a few extra steps necessary, but once you know what to do, it should still be quite simple.Begin by creating a 4GB image:
$ dd if=/dev/zero of=4GB_HDD.img bs=1M count=4096
Use fdisk to create three Linux partitions with a GPT partition table. I chose the size of my partitions randomly. Feel free to choose the size of each partition yourself.
Now we need to run ‘losetup’ to gain access to each partition by assigning loop devices to each one.
$ sudo losetup -Pf --show 4GB_HDD.img
As before, we wanted to see what loop device was chosen. However, this time, the ‘-P’ parameter was useful in this case because it tells ‘losetup’ to scan the image for any partitions to create loop devices for. When the loop interfaces are created, have a look at ‘lsblk’ to see the created devices.
After that, each partition needs to be formatted before use, so run ‘mkfs’ to create them. Try running ‘mkfs.ext2’ on the first partition to create an ext2 filesystem. Then run ‘mkfs.ext4’ on the other two to create ext4 filesystems on the image. Once they’re formatted, you should be able to mount them via the command line or a file manager.
Finish partitions
If you are finished with the partitions, simply run ‘losetup’ to remove the loop device you wish.
Conclusion
To create a virtual drive with partitions on Linux is a very simple process. If you run into any trouble, do let me know in the comment section below this article. I’ll to respond as soon as possible.
Guide to Virtual Disk Images
The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.
To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.
Connect your cluster and start monitoring your K8s costs right away:
1. Overview
A virtual machine is virtualization software that we can use to run operating systems without the need to install them on bare metal. During the setup of a new operating system, the virtual machine will prompt us to select the type of virtual disk image to use.
In this article, we’ll cover the virtual disks offered by virtual machines, such as VirtualBox, that we can use to store data. We’ll provide a basic overview and the pros and cons of each virtual disk type.
Afterward, we’ll discuss choosing the correct virtual disk format for the scenarios they are best suited for.
2. What Is a Virtual Disk Image
As the name suggests, a virtual disk image (VDI) is a replica of a portion of the actual hard disk. Apart from being a sector-by-sector copy of an HDD, it also consists of the complete contents and data structure of a physical hard disk.
The virtual disk images are regular files. Virtual machines will treat an image file as a container. They will read and write data to this container — separating it from the host operating system. Our guest operating system and the user data are stored in these containers. Therefore, we can take these virtual disk images to other machines, and they will work on a compatible virtual machine.
Moreover, there are lots of virtual disk images that are offered by virtual machines, such as VMDK, VHD, VDI, QCOW, and QED. In the next section, we’ll go over these VDIs and see how they differ from one another.
3. Virtual Disk Images
3.1. VMDK
VMDK (Virtual Machine Disk) is a type of virtual disk introduced by VMWare specifically for VMWare products. As of 2011, it’s now an open format and is used by other virtual machines as well.
The VMDK disk image allows for dynamically allocated storage. We specify a maximum size for the image from the outset. The physical disk space is allocated or deallocated as we read-write data to the image, making it a very space-efficient format.
In contrast to the dynamically allocated storage, it also supports fixed allocated storage. Fixed disks allocate the specified space in the physical hard disk. It’s been shown unofficially that fixed-size disks perform slightly faster than dynamic-size disks.
Apart from this, the VMKD format has the additional capability of splitting the image into smaller files. For that reason, we can create larger disk images if the file system has a limit on the file size.
Additionally, VMDK has a storage capacity of 62 TB. Not only that, VMDK is known to perform significantly faster than all the other VDIs.
3.2. VDI
VDI stands for VirtualBox Disk Image. It’s Oracle’s default disk image format used by VirtualBox. Similar to VMDK, this image format isn’t specific to VirtualBox and can be used by other compatible virtual machines like VMWare and Microsoft VHD/VHDX.
Like VMDK, VDI is also a portable disk image format and supports both fixed and dynamic storage allocation. Unlike VMDK, VDI doesn’t support incremental backups. However, it provides high-level data redundancy, which decreases the chances of losing data.
VDI has a storage capacity of 2 TB, making it at a disadvantage against VMDK.
3.3. VHD and VHDX
VHD (Virtual Hard Disk) and VHDX (Virtual Hard Disk Extended) are disk image formats owned by Microsoft. VHD was specifically developed for the Microsoft Hyper-V hypervisor. As of 2012, it has been replaced by the modern and feature-rich VHDX.
Similar to VMDK and VDI, VHD and VHDX also reside on the physical hard disks as regular files. Besides dynamic and fixed allocated storage, VHD and VHDX also support differencing hard disk image. DHDI keeps the changes in a child image inside the VHD — allowing the possibility of undoing or merging the changes into the VHD.
Additionally, a VHD image can also be a pass-through disk image. A pass-through disk image is linked to the physical hard disk. For instance, we can mount a VHD disk on a physical partition.
Moreover, VHDX performs slightly faster than VDI and has a storage capacity of 64 TB.
3.4. QCOW and QED
QCOW stands for QEMU Copy-On-Write. It’s the default light disk image format for the QEMU. It consists of three different versions: qcow, qcow2, and qcow3. QED was supposed to be an upgraded replacement for qcow2, but it was abandoned by the QEMU team, and they advise against using this format.
QCOW supports only dynamic allocated storage. Additionally, the recent version of QCOW is much more performant than the previous versions.
4. Choosing the Right VDI
After going through these different disk image formats, we might have a better idea of choosing the proper disk image format now. In practice, we should always aim for something that’s more reliable and faster. With that in mind, VMDK sounds like a superior choice overall.
VMDK has all the features that we might need. It performs faster, provides incremental backups, and supports a huge storage capacity. Most importantly, we can use it on the most popular virtual machines.
However, if we’re using QEMU or QCOW, we might go for QCOW because it’s tightly integrated with QEMU. Similarly, we can also use VHDX if we’re on Microsoft Windows and need features like pass-through disk images.
5. Conclusion
In this article, we briefly looked at what a virtual disk image is and why we need it. Then, we discussed the different types of virtual disk images, such as VMDK, VDI, VHD, VHDX, and QCOW.
Finally, we briefly discussed choosing the right VDI for our purpose.
How to Create a Virtual Hard Disk Volume Using a File in Linux
Create a virtual hard disk volume using a file in Linux and start storing the components of your system even without bringing the physical drive into the scene. Read the following tutorial and learn how you can do it in minutes.
When we talk about Virtual Hard Disk (VHD), we’re looking at an image file format to help users in many situations. However, its prime capability is storing the elements that rest within your system’s general or physical storage. It is nothing but a container in the virtual form that acts pretty similar to how a physical hard drive operates.
If you’re new to the concept of a virtual driver, wondering what’s it for is something obvious. Let me tell you, the best answer for creating a virtual hard disk volume using a file in Linux can be a lot.
Several users choose this method to add swap space to their computers without creating a new physical hard-drive partition. Since VHDs act similar to a web-page file on the Windows OS, you can seamlessly craft a virtual hard disk for building a portable virtual solution to back up your files.
In the following article, I’ll walk you through the step-by-step on creating virtual hard drives for your Linux system.
Step-by-Step Guide to Create a Virtual HardDisk Volume Using a File in Linux
With the concept of virtual disks discussed, it is the perfect time to uncover the steps you need to follow to create a VHD volume using a file in Linux.
Step 1: Creating a New Image for Holding the Virtual Drive Volume
To create a virtual hard disk volume using a file in Linux, the first thing that you’ll need to do is build an image file and employ the same to hold the virtual volume. Now, there are several ways to conclude the job. However, I’ll help you learn using the dd command to ensure you’ve got the easiest way out.
Depending on the volume you want to allocate, alter the values around the dd command. Suppose you’re after crafting a virtual hard drive that volumes to 1 GB, launch the Terminal by using the “Ctrl+Alt+T” key and invoke the following command: