Exercise: Creating and Partitioning a Virtual Disk
Using Loop and NBD Devices
1 Purpose
This exercise provides hands-on practice with two fundamental storage management tasks: creating a virtual disk and partitioning it. You will learn how to create a file that acts as a virtual hard drive and then use the powerful gdisk utility to create a modern GUID Partition Table (GPT), preparing the disk for a filesystem.
Most importantly, this guide will demonstrate the two primary methods for mounting virtual disk images and explain why you must choose the right tool for the job.
2 What You’ll Accomplish
By the end of this exercise, you will be able to:
- Create both
rawandqcow2virtual disk image files usingqemu-img. - Attach a
rawdisk image to the system as a loop device usinglosetup. - Attach a
qcow2disk image to the system as a network block device usingqemu-nbd. - Use
gdiskto create a GPT partition table and add a new partition. - Verify the creation of the new partition using
lsblk.
This exercise maps to the following program and course learning outcomes:
- Program Learning Outcomes (PLOs):
- 3. Apply terminology and numeric or system concepts: You will apply the concepts of partitioning and GPT by creating them on a new device.
- Course Learning Outcomes (CLOs):
- 4. Configure hardware and basic network components: Creating and partitioning storage is a foundational system configuration skill.
This exercise develops the following skills, which align with the O*NET SOC Code 15-1232.00 for Computer User Support Specialists.
| Learning Objective | O*NET KSAs | Technologies Used |
|---|---|---|
| Create and manage a virtual disk image. | Knowledge: Computers & Electronics Skills: Troubleshooting |
qemu-img, losetup, qemu-nbd |
| Partition a disk using a command-line utility. | Knowledge: Computers & Electronics Abilities: Information Ordering, Manual Dexterity |
gdisk, GPT |
3 Core Concepts: Loop vs. NBD
Before starting, it is critical to understand the two tools we use to mount disk images. Using the wrong one will lead to errors.
| Concept | Description |
|---|---|
Loop Device (losetup) |
A pseudo-device that makes a regular file accessible as a block device. It is designed to work with raw disk images that have a simple, sequential byte-for-byte structure, just like a physical hard drive. It works best when the file size is an exact multiple of the sector size (\(512\) bytes). It cannot be used with sparse or complex file formats like QCOW2. |
Network Block Device (NBD) (qemu-nbd) |
A protocol and kernel module that exposes a block device over a network or, in this case, converts a complex file format (like QCOW2) into a block device locally. The qemu-nbd tool understands the internal structure of QCOW2 files (which contain metadata and sparse data) and presents only the virtual disk contents to the kernel, bypassing the alignment issues of the container file itself. |
For this exercise, we will proceed with the raw and losetup method, as it is the most direct way to work with a file as if it were a simple disk. The qcow2 method is included at the end as an alternative.
4 Prerequisites
This exercise requires a running Fedora 42 virtual machine with internet access and a user account with sudo privileges.
5 Step-by-Step Guide: Raw Image with Loop Device
Open a terminal window in your Fedora VM to begin.
5.1 Step 1: Install qemu-img
The qemu-img utility is a powerful tool for creating and managing virtual disk images. It is part of the QEMU toolset.
Run the following command to install the package:
sudo dnf install -y qemu-img
5.2 Step 2: Create a Preallocated Raw Virtual Disk
You will create a 1-gigabyte file that will act as your virtual hard disk. We will use the raw format, which is a simple byte-for-byte representation of a disk.
Use
qemu-img createto make the file.-f raw: Specifies the raw format.-o preallocation=falloc: This is a critical option. It tellsqemu-imgto immediately allocate the full 1 GiB of space, creating a non-sparse file. This makes it compatible withlosetup.
qemu-img create -f raw -o preallocation=falloc my_raw_disk.img 1GYou can verify the file was created with
ls -lhandqemu-img info. Notice that with a raw, preallocated image, thedisk sizeandvirtual sizeare identical.ls -lh my_raw_disk.img qemu-img info my_raw_disk.img
While qemu-img is perfect for creating virtual machine disks, Linux provides other general-purpose tools for creating large files, both preallocated and sparse.
fallocate (Recommended)
This is the fastest and most modern tool for preallocating space. It instructs the filesystem to reserve the blocks without writing any data.
# Preallocate a 1 GiB file
fallocate -l 1G my_preallocated_file.imgdd (Classic Method)
The dd command can preallocate space by physically writing blocks of zeros from the special /dev/zero device. It is slower but works on virtually any filesystem.
# Create a 1 GiB file by writing 1024 one-megabyte blocks
dd if=/dev/zero of=my_dd_file.img bs=1M count=1024truncate (For Sparse Files)
If you need to create a large sparse file instantly (where the space is not actually allocated yet), truncate is the right tool.
# Create a 1 GiB sparse file
truncate -s 1G my_sparse_file.img5.3 Step 3: Attach the Image as a Loop Device
To make the system see your raw image file as a real block device, you will attach it using losetup.
Run the following command. The
-fPflags telllosetupto find the next available loop device and to scan for partitions (P) inside the image after it is attached.sudo losetup -fP my_raw_disk.imgNow, find the name of the new loop device. Run
losetup -ato see all active loop devices, or uselsblk.losetup -a | grep my_raw_disk.img lsblk
In your student notebook, write down the name of your new loop device (e.g., /dev/loop0). You will need it for the next steps.
5.4 Step 4: Partition the Virtual Disk with gdisk
Now you will use gdisk, a text-based utility for editing GPT partition tables, to partition your new virtual disk.
Launch
gdiskon your loop device. Use the device name you found in the previous step.sudo gdisk /dev/loop0gdiskis interactive. You will enter a series of single-letter commands.- First,
gdiskwill notice there is no partition table and create a new GPT. - At the
Command (? for help):prompt, typenand press Enter to create a new partition. - Press Enter to accept the default partition number (1).
- Press Enter again to accept the default first sector.
- Press Enter a third time to accept the default last sector (this will use the whole disk).
- When asked for a filesystem type, the default is
Linux filesystem. You can press Enter to accept this. - You are now back at the main command prompt. Type
pand press Enter to print the new partition table and verify your work. You should see one partition listed. - Finally, type
wand press Enter to write the changes to the disk. You will be asked to confirm. TypeYand press Enter.
- First,
Verify the result. Run
lsblkagain. You should now see your loop device with a partition underneath it (e.g.,loop0p1).lsblk
Take a screenshot of your final lsblk output showing the loop device with its new partition and add it to your notebook.
5.5 Step 5: Clean Up by Detaching the Loop Device
It is critical to release the loop device when you are finished with it.
Use the
losetup -dcommand with your device name.sudo losetup -d /dev/loop0
6 Alternative Method: QCOW2 Image with NBD
What if you need to use a sparse qcow2 file? As explained, you cannot use losetup. Instead, you must use qemu-nbd and a Network Block Device.
6.1 Step 1: Create the Sparse QCOW2 Image
Notice that the initial disk size is very small, even though the virtual size is \(1 \text{ GiB}\). This is a sparse file.
qemu-img create -f qcow2 my_sparse_disk.qcow2 1G
qemu-img info my_sparse_disk.qcow26.2 Step 2: Prepare the NBD Module
We must ensure the NBD kernel module is loaded so the host system can handle the connection.
# This command loads the module if it isn't already.
sudo modprobe nbd6.3 Step 3: Connect to an NBD Device
We use qemu-nbd to connect the qcow2 file to an NBD device (e.g., /dev/nbd0). The tool handles the qcow2 format complexity and presents a clean block device interface.
# Connect the sparse image to /dev/nbd0
sudo qemu-nbd --connect=/dev/nbd0 my_sparse_disk.qcow2At this point, you could run sudo gdisk /dev/nbd0 and follow the same partitioning steps as before.
6.4 Step 4: Disconnect the NBD Device
Always disconnect the NBD device cleanly after use.
# Disconnect the image from /dev/nbd0
sudo qemu-nbd --disconnect /dev/nbd07 Reflect and Review
Now that you have completed this exercise, reflect on your experience in your Microsoft Teams Student Notebook:
- 3 new commands you used in this exercise.
- 2 different methods for mounting a virtual disk image.
- 1 reason why
losetupcannot be used with a standardqcow2file.
Answer these questions in your notebook to solidify your understanding:
- Why is
losetupthe correct tool for arawdisk image but the wrong tool for aqcow2disk image? - What command would you use to attach a
qcow2file namedmy_vm.qcow2to the block device/dev/nbd1? - In
gdisk, what command do you use to save your changes and exit? - What is the purpose of the
-Pflag in thelosetup -fPcommand?