Native Virtual Hard Drives on Linux

Hello there!

This article explains how to use virtual hard drives on Linux with no third party software. That’s right, this method uses absolutely no third party software whatsoever. How is this possible you may ask? The answer is actually surprisingly simple.

Note: if you just want to do this and don’t care how any of this works, then you need only read the bold text and type in the printed commands.

I got the idea to write this article when asked how one would go about making a virtual hard drive in C. I was unable to find an article explaining how to do it natively, so I decided to write my own. This method that I’m about to explain is both extremely useful and gives a deeper insight to how virtual hard drives work.

To start with, there’s something that must be understood. A hard drive is not a magical device that automatically stores any file you tell it to. An physical hard drive is only capable of storing a bunch of 1s and 0s, nothing more. Everything else, such as the filesystem, is handled by software.

So if a physical hard drive only needs to be able to store a bunch of 1s and 0s, why should a virtual hard drive be any different? The answer is it’s not. All a virtual hard drive is is a file containing a bunch of 1s and 0s, exactly like a physical hard drive.

That means all we need to do to create a virtual hard drive is create a file. Any hard core Linux user should know that you can create a blank file using the dd utility. Simply run the following command to create your new virtual hard drive file:

dd if=/dev/zero of=myharddrive.fs count=<size in sectors>

For those of you who don’t know, a sector is 512 bytes, so if I wanted a virtual hard drive that was 10 MB in size, I would set count equal to 20480.

Well that’s the hard part. Now we have a virtual hard drive. Only one problem: it’s empty. You may be asking how we go about formatting and mounting this new virtual drive. Surely it must be ridiculously complicated right? Wrong.

Remember, in Unix everything is a file. Your physical hard drive is just a file, mapped to /dev/sda1 by default (unless you have multiple hard drives). The virtual hard drive we just created is also just a file, like your physical hard drive. So why should formatting your virtual hard drive be any different from formatting a physical hard drive? It shouldn’t and isn’t.

Hopefully you’re familiar with the mkfs utility. If you’re not, in short it formats a partition or a hard drive to a certain file system. So to format my new virtual drive as an ext2 filesystem, I would run:

mkfs.ext2 myharddrive.fs

Now just to mount it. This is done, you guessed it, the same way we would mount a physical drive:

mount myharddrive.fs /mnt

And that’s it! Now we have a virtual hard drive that we can use just as though it were a real hard drive.

Thank you for reading this article, I hope you found it helpful.

- Z5T1

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>