Format and mount disks
This guide shows you how to format and mount data disks attached to your virtual machine.
Data disks must be formatted with a file system and mounted before use. These steps are performed on the VM itself using SSH, either manually or through cloud-init automation.
Prerequisites
- A running virtual machine
- At least one data disk attached to the VM
- SSH access to the VM
Format and mount a disk
These examples use an Ubuntu VM with a boot disk (vda) and two data disks (vdb and vdc).
View attached disks
View all attached disk devices using lsblk:
lsblk -o NAME,MAJ:MIN,RM,SIZE,RO,TYPE,MOUNTPOINT,LABEL
Sample output:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS LABEL
vda 253:16 0 5G 0 disk
├─vda1 253:17 0 4G 0 part / cloudimg-rootfs
├─vda14 253:30 0 4M 0 part
├─vda15 253:31 0 106M 0 part /boot/efi UEFI
└─vda16 259:0 0 913M 0 part /boot BOOT
vdb 253:0 0 954M 0 disk
vdc 253:32 0 954M 0 disk
vdd 253:48 0 1M 0 disk cidata
Data disks (vdb and vdc) appear without a mount point. Disk devices appear in the same order as disks in your VM spec: vda is the first disk, vdb is the second, and so on.
The cidata device is used for VM configuration and should be ignored.
Confirm disk device mappings
Verify which device name corresponds to which disk by checking the VM status:
kubectl get virtualmachine myvm -o yaml
The attachedDisks field shows the mapping:
status:
attachedDisks:
- device: vda
name: mybootdisk
- device: vdb
name: mystoragedisk
Format the disk
Format the data disk with the ext4 filesystem:
sudo mkfs.ext4 /dev/vdb
Verify the disk is formatted:
sudo blkid /dev/vdb
The output shows the UUID and filesystem type:
/dev/vdb: UUID="125e9d68-1091-42a8-98ec-53ee10754835" BLOCK_SIZE="4096" TYPE="ext4"
Mount the disk
Create a mount point and mount the disk:
sudo mkdir /mnt/mydisk
sudo mount /dev/vdb /mnt/mydisk
Verify the disk is mounted:
lsblk
The data disk now shows a mount point.
Mount disks automatically at boot
Configure your VM to automatically mount data disks on boot by adding entries to the /etc/fstab file. You can either use a UUID-based entry, or a label-based entry.
Get the disk UUID
Retrieve the UUID of your formatted disk:
sudo blkid /dev/vdb
The output shows the UUID:
/dev/vdb: UUID="125e9d68-1091-42a8-98ec-53ee10754835" BLOCK_SIZE="4096" TYPE="ext4"
Copy the UUID value (125e9d68-1091-42a8-98ec-53ee10754835 in this example) for use in the next step.
Add an entry to fstab
Edit the /etc/fstab file with root privileges:
sudo nano /etc/fstab
Note: These examples use
nano, but any text editor works. If your VM doesn't have an editor installed, install one first withsudo apt install nano(orvim,emacs, etc.).
Add a new line at the end of the file using this format:
UUID=<your-uuid> <mount-point> <filesystem-type> defaults 0 2
Replace the placeholders with your values. For example:
UUID=125e9d68-1091-42a8-98ec-53ee10754835 /mnt/mydisk ext4 defaults 0 2
Save and close the file.
Using the UUID is strongly recommended because device names (such as /dev/vdb) may change if you reorder disks in your VM spec.
Test the fstab configuration
Test your fstab entry before rebooting:
sudo mount -a
If the command completes without errors, your fstab configuration is correct.
Alternative: Use a disk label
Instead of a UUID-based entry, you can use a label-based entry in fstab. Use either UUID or label for each disk, not both.
Add a label to your formatted disk:
sudo e2label /dev/vdb mydisklabel
The label must be 16 characters or less for ext4 filesystems.
Verify the label was applied:
sudo blkid /dev/vdb
The output now includes the label:
/dev/vdb: LABEL="mydisklabel" UUID="125e9d68-1091-42a8-98ec-53ee10754835" BLOCK_SIZE="4096" TYPE="ext4"
Add the label-based entry to /etc/fstab:
LABEL=mydisklabel /mnt/mydisk ext4 defaults 0 2
Detach a disk
Before detaching a disk from your VM, remove its entry from /etc/fstab to prevent boot errors.
Edit /etc/fstab:
sudo nano /etc/fstab
Delete or comment out the line for the disk you're removing.
For instructions on detaching disks, see Create and attach disks.
Automate disk formatting and mounting with cloud-init
Automate disk formatting and mounting by including cloud-init configuration when you create your VM. Device names must match the order disks appear in your VM spec.
Format disks with cloud-init
Use the disk_setup and fs_setup modules to format disks and apply labels.
This example formats /dev/vdb with ext4 and applies a label without creating partitions:
disk_setup:
/dev/vdb:
table_type: gpt
layout: false
overwrite: false
fs_setup:
- device: /dev/vdb
filesystem: ext4
label: mydisklabel
To create a single partition, set layout: true and update the device in fs_setup to /dev/vdb1.
Note: Labels must be 16 characters or less for ext4 filesystems.
Mount disks at first boot
Create a mount point with runcmd:
runcmd:
- mkdir -p /mnt/mymountpoint
Mount the disk automatically using the mounts module with the label from fs_setup:
mounts:
- [ "LABEL=mydisklabel", /mnt/mymountpoint, ext4, "defaults", "0", "2" ]
Next steps
- Create and attach disks to add storage to your virtual machines
- Configure security groups to control network access
- Use custom cloud-init for advanced VM configuration