Page 1 of 1

Native Virtual Hard Drives on Linux Rate Topic: -----

#1 Z5T1  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 03-May 14

Posted 05 May 2014 - 08:11 AM

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

Original Article: http://z5t1.com/reso...rives-on-linux/

Is This A Good Question/Topic? 1
  • +

Page 1 of 1