VirtualBox: Add Linux Filesystem

The example on this page covers adding a new 50GB /mysql filesystem to an existing Oracle Linux 8 VirtualBox VM (called OracleLinux8)

VirtualBox

Create VirtualBox Disk

vboxmanage createhd --filename /media/${USER}/HDD1/VirtualBox/OracleLinux/mysql.vdi --size 51200

Add Disk to SATA Controller

Confirm VM name...

vboxmanage list vms

Confirm SATA Controller Name...

vboxmanage showvminfo OracleLinux8 | grep "Storage Controller Name"

This name is generally SATA unless you have created your VM in a heavily customized manner

Confirm which Ports/Devices are already used...

vboxmanage showvminfo OracleLinux8 | grep "^SATA"

Attach the HD to the SATA Controller...

vboxmanage storageattach OracleLinux8 --storagectl "SATA" --port 1 --device 0 --type hdd \

--medium /media/${USER}/HDD1/VirtualBox/OracleLinux/mysql.vdi

Start VirtualBox VM

vboxmanage startvm OracleLinux8

Create Filesystem (LVM)

All actions to be performed on the target VM from a terminal session...

Identify device...

lsblk

Our device is shown as...sdb 8:16 0 50G 0 disk .. therefore the device will be /dev/sdb

Create the Physical Volume...

pvcreate /dev/sdb

Create Volume Group...

(called myvg using just the /dev/sdb device.. note, you can span multiple devices)

vgcreate myvg /dev/sdb

Create Logical Volume...

(called mylv, size 49GB in the myvg Volume Group)

lvcreate -n mylv -L 49G myvg

If you try to create a 50GB Logical Volume, you will get this error...Volume group "myvg" has insufficient free space (12799 extents): 12800 required.

Create Filesystem...

(in this case an ext4 filesystem. Use the LV Path from the lvdisplay command. The filesystem will be the same size as the underlying Logical Volume)

mkfs.ext4 /dev/myvg/mylv

Create a mount point...

mkdir /mysql

Mount the filesystem...

mount /dev/myvg/mylv /mysql

Give the device a Label...

e2label /dev/myvg/mylv mysql

For other filesystem types use appropriate alternative commands like xfs_admin or fatlabel

Add a line to /etc/fstab if you want it to mount automatically at boot time...

LABEL=mysql /mysql ext4 defaults 0 2

You can use a device node (/dev/myvg/mylv) but using a Label is now the recommended method.

Alternatively...

fdisk -l


Check with...

pvs

pvdisplay



vgs

vgdisplay myvg



lvs

lvdisplay myvg

(note we're passing the VG not the LV)

lvdisplay /dev/myvg/mylv






mount | grep "^/dev" | column -t

df -h


Backout

unmount /mysql

lvremove mylv

vgremove myvg

pvremove /dev/sdb

Troubleshooting

If the device already has a partition table, you will get this error from pvcreate...

Device /dev/sdb excluded by a filter.

Check

(as root)

wipefs --no-act /dev/sdb

Fix

Do a dry run...

(as root)

wipefs --all --no-act /dev/sdb

Create backup and remove partition table signatures...

(as root)

wipefs --all --backup /dev/sdb

List stored signatures...

(as root)

find ~/ -maxdepth 1 -name 'wipefs-sdb-*.bak'

Backout

You cannot backout wipefs once you have overwritten any data on the device.

(i.e. there is a limited window for backout to be successful. Be careful.)

This command (1) generates a list of commands to run to put the partition table back (run as root)...

find ~/ -maxdepth 1 -name "wipefs-sdb-*.bak"| while read file

do

addr=$(echo ${file} | sed "s/.*wipefs-.*-\(.*\).bak/\1/")

echo sudo dd if=${file} of=/dev/sdb seek=$((${addr})) bs=1 conv=notrunc

done'

Create Filesystem

Use this method to add a disk without using the Linux Logical Volume Manager (LVM)...

Identify device...

lsblk

Our device is shown as...sdb 8:16 0 50G 0 disk .. therefore the device will be /dev/sdb

Partition the device (GUID Partition Table)...

parted

select /dev/sdb

print

mklabel gpt

print

mkpart

*** TO BE COMPLETED ***

Partition Table Types(As used by mklabel)
  • aix support for volumes used in IBM AIX
  • amiga support for Amiga RDB partitioning scheme;
  • bsd support for BSD disk labels
  • dvh support for SGI disk volume headers
  • gpt support for GUID partition tables
  • mac support for old (pre-GPT) Apple partition tables
  • msdos support for DOS-style MBR partition tables
  • pc98 support for PC-98 partition tables;
  • sun support for Sun’s partitioning scheme;
  • loop provides support for raw disk access.