Monday, July 11, 2016

Mounting hard drives

In VirtualBox I'm constantly adding and removing virtual hard drives since the way you typically work with hard drives in VirtualBox is different compared to a physical computer. Separate projects usually gets it's own hard drive and hard drives are easily cloned when things a properly separated into individual VDI files.

But I never remember the exact steps to add a new hard drive :-)

First, create the new hard drive:

After that the buttons to press are in order (make sure the virtual machine is shutdown before creating the new hard drive):
  • "Create new disk".
  • Select "VDI" and then "Next".
  • Select "Dynamically allocated" and press "Next".
  • Write a suitable name of the new drive. Since the drive is dynamically allocated you may select a larger size than you plan to use. About 30GB is what I use in most cases for simple projects. Press "Create".
  • The new drive is added.
Start the virtual machine, it's time to create the partition table, the file system and mount the new drive.

Do "ls -l /dev/sd*" to find the name of the new hard drive:
In my example it's /dev/sdf since that is the only one without partitions. Next, we use fdisk to create the partition table. Be careful now, you may wipe out your hard drives if you do this incorrectly!

$ sudo fdisk /dev/sdf

 Use the "p" command to check that you are working on the correct drive. Use "n" to create a new partition. Go with the default values all the way:

If everything is correct, use the "w" command to write the changes and exit fdisk.

Now there is a new drive (or actually, partition) available:

Next step is to create a proper file system on this drive:

$ sudo mkfs.ext4 /dev/sdf1

Ok, now we have a proper partition. The final step is to get this partition properly mounted and thus visible and possible to use.

One way of accomplishing this, is as follows:

  • Create a new folder in /mnt, e.g.
$ cd /mnt
$ sudo mkdir git

  • Determine the UUID of the new partition by using lsblk:
$ sudo lsblk -f


  • From the UUID for your partition, you add a new entry into /etc/fstab:
$ sudo nano /etc/fstab

Add the line to the end with

UUID="<your UUID for your partition>" /mnt/git ext4 errors=remount-ro 0 1

Now you are almost done. The next time you reboot the new drive will get added. But you can mount the new partition already at this point by running:

$ sudo mount /mnt/git

Easy? Yes, but it's also easy to forget a step. Especially the "lsblk -f" is something I have to Google every time. Instead of lsblk it's also possible to use the "sudo blkid" command to get the required information.

No comments:

Post a Comment