Some applications need to be concerned about the available disk capacity, such as logging systems and data storage modules.
They often encounter an important question:
“What to do when the disk is full?”
To test this situation, do we really need to fill up hundreds of GB of disk space?
Clearly, this method is time-consuming and labor-intensive, and it may also affect system stability.
In fact, there is a simple wayโ“Create a virtual disk”
๐ Creation
We can directly use <span>dd</span> to create a fixed-size disk file, for example, 1GB:
dd if=/dev/zero of=virtual_disk.img bs=1M count=1024
Parameter explanation:
<span>if=/dev/zero</span>โ Read data from the zero device (all zeros)<span>of=virtual_disk.img</span>โ Output to the disk file we want to create<span>bs=1M</span>โ Write 1MB at a time<span>count=1024</span>โ Write 1024 times, which is 1GB
After creation, we can check it:
ls -lh virtual_disk.img
At this point, our disk file is actually ready ๐
๐ Formatting
To use it like a real disk, we need to format it:
sudo mkfs.ext4 virtual_disk.img
This step will turn the disk file into a mountable <span>ext4</span> file system.
๐ Mounting
Find a directory to serve as the mount point, such as <span>/mnt/vdisk</span>:
sudo mkdir -p /mnt/vdisk
sudo mount -o loop virtual_disk.img /mnt/vdisk
The key point here:
<span>-o loop</span>โ Tells the system that this is a disk file, not a physical device
After successful mounting, we can use the <span>/mnt/vdisk/</span> directory just like a real disk โจ
๐งน Unmounting and Deleting
After testing, you can unmount this disk file using <span>umount</span>:
sudo umount /mnt/vdisk
If no longer needed, simply delete the file:
rm virtual_disk.img
๐ Summary
With this small trick, we can:
โ Customize the size of the virtual disk
โ Not affect the system disk
โ Test abnormal behaviors of logging systems or databases