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
Section titled “Prerequisites”- A running virtual machine
- At least one data disk attached to the VM
- SSH access to the VM
Format and mount a disk
Section titled “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
Section titled “View attached disks”View all attached disk devices using lsblk:
lsblk -o NAME,MAJ:MIN,RM,SIZE,RO,TYPE,MOUNTPOINT,LABELSample output:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS LABELvda 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 BOOTvdb 253:0 0 954M 0 diskvdc 253:32 0 954M 0 diskvdd 253:48 0 1M 0 disk cidataData 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
Section titled “Confirm disk device mappings”Verify which device name corresponds to which disk by checking the VM status:
evroc compute virtualmachine get myvmThe output shows the attached disks and their device mappings.
Format the disk
Section titled “Format the disk”Format the data disk with the ext4 filesystem:
sudo mkfs.ext4 /dev/vdbVerify the disk is formatted:
sudo blkid /dev/vdbThe output shows the UUID and filesystem type:
/dev/vdb: UUID="125e9d68-1091-42a8-98ec-53ee10754835" BLOCK_SIZE="4096" TYPE="ext4"Mount the disk
Section titled “Mount the disk”Create a mount point and mount the disk:
sudo mkdir /mnt/mydisksudo mount /dev/vdb /mnt/mydiskVerify the disk is mounted:
lsblkThe data disk now shows a mount point.
Mount disks automatically at boot
Section titled “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
Section titled “Get the disk UUID”Retrieve the UUID of your formatted disk:
sudo blkid /dev/vdbThe 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
Section titled “Add an entry to fstab”Edit the /etc/fstab file with root privileges:
sudo nano /etc/fstabNote: 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 2Replace the placeholders with your values. For example:
UUID=125e9d68-1091-42a8-98ec-53ee10754835 /mnt/mydisk ext4 defaults 0 2Save 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
Section titled “Test the fstab configuration”Test your fstab entry before rebooting:
sudo mount -aIf the command completes without errors, your fstab configuration is correct.
Alternative: Use a disk label
Section titled “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 mydisklabelThe label must be 16 characters or less for ext4 filesystems.
Verify the label was applied:
sudo blkid /dev/vdbThe 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 2Detach a disk
Section titled “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/fstabDelete 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
Section titled “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
Section titled “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: falsefs_setup: - device: /dev/vdb filesystem: ext4 label: mydisklabelTo 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
Section titled “Mount disks at first boot”Create a mount point with runcmd:
runcmd: - mkdir -p /mnt/mymountpointMount the disk automatically using the mounts module with the label from fs_setup:
mounts: - ["LABEL=mydisklabel", /mnt/mymountpoint, ext4, "defaults", "0", "2"]Next steps
Section titled “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