Attach a disk to a virtual machine

From Stat DS Computing Wiki


In this article we are going to see how to attach and mount both new and existing disks to a Linux virtual machine through the Azure portal. The first few steps describe how to attach a disk to an existing VM, but note that a disk can be attached also when creating the VM itself. 

 

Find the virtual machine

  1. Go to the Azure portal to find the VM. Search for and select Virtual machines.
  2. Choose the VM from the list.
  3. In the Virtual machines page, under Settings, choose Disk

Attach a new disk

  1. On the Disks pane, under Data disks, select Create and attach a new disk.
  2. Enter a name for your managed disk. Review the default settings, and update the Storage type, Size (GiB), Encryption and Host caching as necessary. The last two are usually not needed. I would suggest to use an SSD as storage type, in order to have optimal I/O performance.
  3. When you're done, select Save at the top of the page to create the managed disk and update the VM configuration

Attach an existing disk

  1. On the Disks pane, under Data disks, select Attach existing disks.
  2. Select the drop-down menu for Disk name and select a disk from the list of available managed disks.
  3. Select Save to attach the existing managed disk and update the VM configuration.

At this point we need to connect to the VM via terminal, find the disk, partition it and mount it to the file system. In simpler words, we need to create a folder that points to the disk (or a partition of it), so that everything saved within that folder will be saved on the disk and not somewhere else.

 

Connect to the VM to mount the disk

To partition, format, and mount your new disk so your Linux VM can use it, SSH into your VM. You can use the public IP address of the VM and the default username azureuser, unless you have changed it.

Find the disk

Once connected to your VM, you need to find the disk. In this example, we're using lsblk to list the disks.

lsblk -o NAME,HCTL,SIZE,MOUNTPOINT | grep -i "sd"

The output should be 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       4G

In this example, the disk that was added was sdc. It's a LUN 0 and is 4GB.

Prepare a new empty disk

N.B. If you are using an existing disk that contains data, skip to “Mount the disk”. The following instructions will delete data on the disk.

If you're attaching a new disk, you need to partition the disk.

The parted utility can be used to partition and to format a data disk.

  • Use the latest version parted that is available for your distro.
  • If the disk size is 2 tebibytes (TiB) or larger, you must use GPT partitioning. If disk size is under 2 TiB, then you can use either MBR or GPT partitioning.

The following example uses parted on /dev/sdc, which is where the first data disk will typically be on most VMs. Replace sdc with the correct option for your disk. We're also formatting it using the XFS filesystem. Note that the /dev/sdc folder should be two steps “back” with respect to the landing folder when you SSH into Azure.

sudo parted /dev/sdc --script mklabel gpt mkpart xfspart xfs 0% 100%
sudo mkfs.xfs /dev/sdc1
sudo partprobe /dev/sdc1

Mount the disk

Create a directory to mount the file system using mkdir. The following example creates a directory at /datadrive:

sudo mkdir /datadrive

Use mount to then mount the filesystem. The following example mounts the /dev/sdc1 partition to the /datadrive mount point:

sudo mount /dev/sdc1 /datadrive

N.B. Important: To ensure that the drive is remounted automatically after a reboot, it must be added to the /etc/fstab file. It's also highly recommended that the UUID (Universally Unique Identifier) is used in /etc/fstab to refer to the drive rather than just the device name (such as, /dev/sdc1). If the OS detects a disk error during boot, using the UUID avoids the incorrect disk being mounted to a given location. Remaining data disks would then be assigned those same device IDs. To find the UUID of the new drive, use the blkid utility:

sudo blkid

The output looks similar to the following example:

/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"

N.B. Improperly editing the /etc/fstab file could result in an unbootable system. If unsure, refer to the distribution's documentation for information on how to properly edit this file. You should create a backup of the /etc/fstab file is created before editing. 

Next, open the /etc/fstab file in a text editor. 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.

Verify the disk

You can now use lsblk again to see the disk and the mountpoint.

lsblk -o NAME,HCTL,SIZE,MOUNTPOINT | grep -i "sd"

The output will look something like this:

<code data-author-content="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       4G
└─sdc1                4G /datadrive
">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       4G
└─sdc1                4G /datadrive
</code>

You can see that sdc is now mounted at /datadrive.