Skip to content

Instantly share code, notes, and snippets.

@jhyland87
Last active May 22, 2018 21:47
Show Gist options
  • Save jhyland87/37c32c87b8f47617ddcbdd60b19921ce to your computer and use it in GitHub Desktop.
Save jhyland87/37c32c87b8f47617ddcbdd60b19921ce to your computer and use it in GitHub Desktop.
[VirtualBox] Create and mount a VHD to an existing VM

Create & Mount a HDD in VirtualBox

1. Create VHD in VirtualBox

$ vboxmanage createmedium disk --filename New-Drive.vhd --size 8192 --format VHD --variant Standard
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Medium created. UUID: c6a6475c-a78e-4843-99f8-c5cd8891c302

$ vboxmanage list hdds

UUID:           c6a6475c-a78e-4843-99f8-c5cd8891c302
Parent UUID:    base
State:          created
Type:           normal (base)
Location:       /Users/jhyland/VirtualBox VMs/Magento2 CentOS7 2/New-Drive.vhd
Storage format: VHD
Capacity:       8192 MBytes
Encryption:     disabled

2. Login to VM, list the drives via fdisk -l

[root@localhost ~]# fdisk -l

Disk /dev/sda: 8589 MB, 8589934592 bytes, 16777216 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 label type: dos
Disk identifier: 0x000b5f72

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    16777215     7339008   8e  Linux LVM

Disk /dev/sdb: 32.2 GB, 32212254720 bytes, 62914560 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 /dev/mapper/cl-root: 6652 MB, 6652166144 bytes, 12992512 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 /dev/mapper/cl-swap: 859 MB, 859832320 bytes, 1679360 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

(Notice the Disk /dev/sdb line showing the drive, but no partitions associated to it)


3. Create a partition

[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x67ef064e.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-62914559, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-62914559, default 62914559):
Using default value 62914559
Partition 1 of type Linux and of size 30 GiB is set

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

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

View the partitions on the target disk to confirm the partition was created:

[root@localhost mnt]# fdisk -l /dev/sdb

Disk /dev/sdb: 32.2 GB, 32212254720 bytes, 62914560 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 label type: dos
Disk identifier: 0x67ef064e

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    62914559    31456256   83  Linux

4. Format the partition

[root@localhost ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1966080 inodes, 7864064 blocks
393203 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2155872256
240 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,
  4096000

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

5. Mount the partition

[root@localhost ~]# mount -t ext4 /dev/sdb1 /mnt/mysql_data
[root@localhost ~]# df -h /mnt/mysql_data
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1        30G   45M   28G   1% /mnt/mysql_data

6. If all seems well, add it to the fstab:

[root@localhost ~]# grep /dev/sdb1 /etc/mtab >> /etc/fstab

To test:

[root@localhost ~]# umount /mnt/mysql_data
[root@localhost ~]# df /mnt/mysql_data
Filesystem          1K-blocks    Used Available Use% Mounted on
/dev/mapper/cl-root   6486016 3722224   2763792  58% /
[root@localhost ~]# mount -a
[root@localhost ~]# df /mnt/mysql_data
Filesystem     1K-blocks  Used Available Use% Mounted on
/dev/sdb1       30831524 45080  29197248   1% /mnt/mysql_data

Resources

  • VBoxManage - Online documentation for VBoxManage command
  • VBoxManage list - The list command gives relevant information about your system and information about VirtualBox's current settings.
  • VBoxManage showvminfo - The showvminfo command shows information about a particular virtual machine. This is the same information as VBoxManage list vms --long would show for all virtual machines.
  • VBoxManage createmedium - This command creates a new medium. The syntax is as follows:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment