When working with Virtual Machines (VMs) in the Azure cloud, it’s essential to ensure your data persists even during maintenance or resizing activities. In this guide, we’ll walk through the steps to attach a persistent disk to your Linux VM, format it, and set it up for automatic remounting after reboots.
SSH into your VM
Connect to your VM using SSH. Replace <username>
and <PublicIP>
with your VM credentials.
ssh <username>@<PublicIP>
If you use a PEM file for authentication.
ssh -i <PEMFile> <username>@<PublicIP>
Find the disk
Use lsblk
to identify the disk you want to attach. In this example, sdc
is the target disk with a size of 50GB.
lsblk -o NAME,HCTL,SIZE,MOUNTPOINT | grep -i "sd"
The output is similar to the following example
sda 0:0:0:0 30G
├─sda1 29.9G /
├─sda14 4M
└─sda15 106M /boot/efi
sdb 1:0:1:0 14G
└─sdb1 14G /mnt
sdc 3:0:0:0 50G
Here, sdc is the disk we want because it has a capacity of 50GB.
Format the disk
Use parted
and mkfs.xfs
to format the disk (/dev/sdc
in this example).
sudo parted /dev/sdc --script mklabel gpt mkpart xfspart xfs 0% 100%
sudo partprobe /dev/sdc
sudo mkfs.xfs /dev/sdc1
Mount the disk
Create a directory and mount the filesystem.
sudo mkdir /datadrive
sudo mount /dev/sdc1 /datadrive
Persist the mount
To ensure automatic remounting after a reboot, add an entry to /etc/fstab
. Find the UUID using blkid
.
sudo blkid
Example Output
/dev/sda1: LABEL="cloudimg-rootfs" UUID="11111111-1b1b-1c1c-1d1d-1e1e1e1e1e1e" TYPE="ext4" PARTUUID="1a1b1c1d-11aa-1234-1a1a1a1a1a1a"
/dev/sda15: LABEL="UEFI" UUID="BCD7-96A6" TYPE="vfat" PARTUUID="1e1g1cg1h-11aa-1234-1u1u1a1a1u1u"
/dev/sdb1: UUID="22222222-2b2b-2c2c-2d2d-2e2e2e2e2e2e" TYPE="ext4" TYPE="ext4" PARTUUID="1a2b3c4d-01"
/dev/sda14: PARTUUID="2e2g2cg2h-11aa-1234-1u1u1a1a1u1u"
/dev/sdc1: UUID="33333333-3b3b-3c3c-3d3d-3e3e3e3e3e3e" TYPE="xfs" PARTLABEL="xfspart" PARTUUID="c1c2c3c4-1234-cdef-asdf3456ghjk"
Here, sdc1 we want to persist the mount.
Next, open the /etc/fstab
file in a text editor.
sudo nano /etc/fstab
Add a line to the end of the file, using the UUID value for the /dev/sdc1
device that was created in the previous steps, and the mountpoint of /datadrive
. Using the example from this article, the new line would look like the following
UUID=33333333-3b3b-3c3c-3d3d-3e3e3e3e3e3e /datadrive xfs defaults,nofail 1 2
When you’re done editing the file, save and close the editor.
Alternatively, you can run the following command to add the disk to the /etc/fstab
file
echo "UUID=33333333-3b3b-3c3c-3d3d-3e3e3e3e3e3e /datadrive xfs defaults,nofail 1 2" >> /etc/fstab
Setting Ownership
Change ownership of the /datadrive
directory to azureuser
.
sudo chown -R azureuser:azureuser /datadrive
This sets the owner and group of the /datadrive
directory and its contents to azureuser
.
By following these steps, you’ve successfully attached a persistent disk to your Linux VM in Azure, formatted it, and set up automatic remounting for data persistence across reboots. This ensures your crucial data remains intact even during VM maintenance or resizing operations.