Ubuntu VM Add Extra Hard Disk Partition

In this tutorial, we will discuss how to add additional hard disk to a Ubuntu VM Environment running in Virtual Box with Windows Host. You may often find the need to add extra hard disk if the application you are trying to install into the VM Guest (i.e. Ubuntu) requires more space, than what is available. In my situation, I had to install Oracle 12C database which required extra space – so I ended up adding one. Your scenario may be different, but the steps you have to follow are exactly the same as given below. So, here is a step by step guide that explains how to do this. (Assumed you are a newbie to Ubuntu, just follow and you should be done)

Step-1: Power off Ubuntu VM, Add Extra Hard Disk


First shutdown your VM , and navigate to settings and click on Storage. Click on Controller, and then select “Add Hard Disk”. Follow the screenshot below for guidance.

Ubuntu VM Add Hard Disk - Virtual Box
Ubuntu VM Add Hard Disk - Virtual Box
In the prompt that follows, perform the following selection;


  • Select Create New Disk
  • Select Hard Drive File Type to VDI (Virtual Box Disk Image)
  • Set Storage on Physical Hard Drive to “Dynamically Allocated”
  • In “File Location and Size” – Specify Name , and set the size you want for the new disk.
  • Hit Create and you will see a new disk added.


At the end of the above step, we have added a disk, but that is not assigned to our Ubuntu OS yet. We will set this connection in the next steps.

Step-2: Find the New Disk in Ubuntu Terminal Window


In the terminal window, type the following command and locate the new disk you have added.

Fdisk-l

A sample output is provided below,

Disk /dev/sdb: 12.9 GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 cylinders, total 25165824 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/sdb doesn't contain a valid partition table Our new physical disk is detected in /dev/sdb (It may be different for you depending on the number of existing disk you have) and we are now ready to create a partition and write table to disk and exit.

Step -3: Use fdisk and create partition


Fdisk (fixed disk) is a utility through which you can achieve disk partitioning. Start with

sudo fdisk sdb

You will get an output similar to the following

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x01549cd0.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): 

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Select the action as “n” – meaning add a new partition.

Ubuntu will ask you to select a partition type – select “e” meaning extended. Accept defaults for the rest and finally provide command “w” – which will write the partition table to disk and exit. See below for a sample output;

Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): e
Partition number (1-4, default 1): 1
First sector (2048-25165823, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-25165823, default 25165823): 
Using default value 25165823

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

It is now time to format the disk we have added.

Step-4: Format the new disk


Use mkfs (make file system) command to format the newly added disk as ext4, as shown below:

ubuntu@ubuntu-VirtualBox:/dev$ sudo mkfs -t ext4 /dev/sdb

Ignore the warning that is thrown after the command, select y and press enter.

mke2fs 1.42.5 (29-Jul-2012)
/dev/sdb is entire device, not just one partition!
Proceed anyway? (y,n) y
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
786432 inodes, 3145728 blocks
157286 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=3221225472
96 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

Step-5: Create a folder to mount the partition 


Once your disk is formatted, you should create a logical folder in Ubuntu and mount the disk to the folder. Through this way, you can install apps into the destination. So, go ahead and use mkdir command to create a directory called app.

ubuntu@ubuntu-VirtualBox:/dev$ sudo mkdir /app
ubuntu@ubuntu-VirtualBox:/dev$

Step-6: Mount the newly created partition 


In this step, you will mount the newly created partition to the folder you created earlier. You can do this easily using mount command as shown below:

ubuntu@ubuntu-VirtualBox:/dev$ sudo mount -t ext4 /dev/sdb /app
ubuntu@ubuntu-VirtualBox:/dev$

Once this is done, you can use the df command to check if the mount is successful

ubuntu@ubuntu-VirtualBox:/dev$ sudo df
Filesystem     1K-blocks      Used Available Use% Mounted on
/dev/sda1       19879400   2597304  16272280  14% /
udev              246436         4    246432   1% /dev
tmpfs             101672       780    100892   1% /run
none                5120         0      5120   0% /run/lock
none              254172       152    254020   1% /run/shm
none              102400        52    102348   1% /run/user
Oracle12C      312568828 123106996 189461832  40% /media/sf_Oracle12C
/dev/sdb        12385456    161776  11594536   2% /app

As you can see from the output, /dev/sdb (physical disk) is now pointing to /app folder path, and you can start installing apps into it. Finally you have to edit fstab (File System Table) located in /etc/fstab and add the new disk. That way, during reboots Ubuntu will automatically recognize the new disk and mount it for you. This is shown below:

ubuntu@ubuntu-VirtualBox:/dev$ more /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
#                
# / was on /dev/sda1 during installation
UUID=2b989882-68c1-4f20-a47e-c735f13159e5 /               ext4    errors=remount
-ro 0       1
# swap was on /dev/sda5 during installation

/dev/sdb /app ext4 defaults 0 0
UUID=a415e079-1db8-4fcf-aaa5-374abd3e957f none            swap    sw            
  0       0

You can use any editor of your choice to edit the file –I used vi editor. Once this is done, reboot the virtual machine and you are done. You have successfully added a new disk to your Ubuntu machine and now can go ahead and install any big apps into it.

No comments:

Post a Comment