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 mannerConfirm 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/sdbCreate 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 fatlabelAdd 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/sdbPartition the device (GUID Partition Table)...
parted
select /dev/sdb
mklabel gpt
mkpart
*** TO BE COMPLETED ***
- 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.
Bibliography & References
https://www.oracle.com/technical-resources/articles/it-infrastructure/admin-manage-vbox-cli.htmlhttps://www.linuxtechi.com/create-disk-partitions-parted-command-linux/https://www.2daygeek.com/create-lvm-storage-logical-volume-manager-in-linux/https://www.2daygeek.com/how-to-manage-disk-partitions-using-parted-command/https://www.gnu.org/software/parted/manual/html_node/mklabel.htmlhttps://en.wikipedia.org/wiki/GUID_Partition_Tablehttps://unix.stackexchange.com/questions/289389/what-are-the-differences-between-the-various-partition-tables(1) https://sleeplessbeastie.eu/2021/02/26/how-to-fix-device-excluded-by-a-filter/https://serverfault.com/questions/429693/recommended-filesystem-for-mysql-xfs-vs-ext4https://www.cyberciti.biz/faq/see-filesystems-in-linux/
https://docs.oracle.com/en/virtualization/virtualbox/6.0/user/vboxmanage-showvminfo.html