Before we dive in, let's understand what a virtual disk is. A virtual disk is simply a file that acts like a physical disk drive. Think of it like creating a container file that the operating system can treat as if it were a real hard drive. This is similar to how virtual machines use virtual disk files to store their data.
Before we proceed with the hands-on part, let's understand some important concepts:
-
First, let's create a 256MB virtual disk using the dd command:
dd if=/dev/zero of=virtual.img bs=1M count=256
Let's break this command down:
dd is a utility for copying and converting files
if=/dev/zero means "input file is /dev/zero" (a special file that provides endless zeros)
of=virtual.img means "output file is virtual.img" (our new virtual disk file)
bs=1M sets the block size to 1 megabyte (how much data to copy at once)
count=256 means copy 256 blocks (resulting in a 256MB file)
This creates an empty file filled with zeros that we'll use as our virtual disk.
-
Verify the file size:
ls -lh virtual.img
You should see that virtual.img is exactly 256MB.
-
Now, let's format this virtual disk with an ext4 filesystem:
sudo mkfs.ext4 virtual.img
What's happening here? This command:
- Creates a new ext4 filesystem inside our virtual disk file
- Sets up the basic structure needed to store files and directories
- Is similar to formatting a new USB drive before first use
The ext4 filesystem is the default for many Linux distributions because it's reliable and well-tested.
-
Next, we need to create a mount point. This is the directory where the contents of our virtual disk will appear:
sudo mkdir /mnt/virtualdisk
Think of a mount point as a "window" into your virtual disk. After mounting, when you look into this directory, you're actually seeing the contents of the virtual disk.
-
Now we can mount the virtual disk:
sudo mount -o loop virtual.img /mnt/virtualdisk
Let's break down what's happening:
- The
-o loop option tells Linux to treat our file as if it were a real disk device
virtual.img is our source (the virtual disk we created)
/mnt/virtualdisk is where we want the contents to appear
This is similar to what happens automatically when you plug in a USB drive, except we're doing it manually with our virtual disk file.
-
Let's verify that the disk is mounted:
mount | grep virtualdisk
You should see a line indicating that virtual.img is mounted on /mnt/virtualdisk.
-
Now that it's mounted, we can use it like any other directory. Let's create a file:
sudo touch /mnt/virtualdisk/testfile
ls /mnt/virtualdisk
You should see testfile listed.
-
When you're done using the virtual disk, you should unmount it:
sudo umount /mnt/virtualdisk
Unmounting removes the filesystem from that directory, ensuring that the operating system finishes all pending read and write operations before detaching it. Failing to unmount properly can lead to data corruption. Although the command syntax focuses on unmounting the directory, under the hood, the operating system knows this directory corresponds to the mounted disk image.
This process of creating, formatting, and mounting a virtual disk is very similar to what happens when you plug in a new hard drive or USB stick. The main difference is that we're doing everything with a file instead of a physical device.
Mounting a filesystem means attaching it to a specified directory so that the operating system can access the data inside the filesystem. In this lab, the virtual disk image file is treated as if it were a physical disk, and mounting makes its contents available at a particular directory (e.g., /mnt/virtualdisk).