|
| 1 | +### YamlMime:HowTo |
| 2 | + |
| 3 | +metadata: |
| 4 | + title: Attach a data disk to a Linux VM |
| 5 | + description: Use the portal to attach new or existing data disk to a Linux VM. |
| 6 | + author: roygara |
| 7 | + ms.author: rogarana |
| 8 | + ms.date: 03/19/2024 |
| 9 | + ms.service: azure-disk-storage |
| 10 | + ms.topic: how-to |
| 11 | + ms.collection: linux |
| 12 | + ms.custom: |
| 13 | + - linux-related-content |
| 14 | + - ge-structured-content-pilot |
| 15 | + |
| 16 | +title: | |
| 17 | + Use the portal to attach a data disk to a Linux VM |
| 18 | +introduction: | |
| 19 | + **Applies to:** :heavy_check_mark: Linux VMs :heavy_check_mark: Flexible scale sets |
| 20 | +
|
| 21 | + This article shows you how to attach both new and existing disks to a Linux virtual machine through the Azure portal. You can also [attach a data disk to a Windows VM in the Azure portal](../windows/attach-managed-disk-portal.yml). |
| 22 | +
|
| 23 | +prerequisites: |
| 24 | + summary: | |
| 25 | + Before you attach disks to your VM, review these tips: |
| 26 | + dependencies: |
| 27 | + - The size of the virtual machine controls how many data disks you can attach. For details, see [Sizes for virtual machines](../sizes.md). |
| 28 | + |
| 29 | +procedureSection: |
| 30 | + - title: | |
| 31 | + Find the virtual machine |
| 32 | + summary: | |
| 33 | + Follow these steps: |
| 34 | + steps: |
| 35 | + - | |
| 36 | + Go to the [Azure portal](https://portal.azure.com/) to find the VM. Search for and select **Virtual machines**. |
| 37 | + - | |
| 38 | + Select the VM you'd like to attach the disk to from the list. |
| 39 | + - | |
| 40 | + In the **Virtual machines** page, under **Settings**, select **Disks**. |
| 41 | +
|
| 42 | + - title: | |
| 43 | + Attach a new disk |
| 44 | + summary: | |
| 45 | + Follow these steps: |
| 46 | + steps: |
| 47 | + - | |
| 48 | + On the **Disks** pane, under **Data disks**, select **Create and attach a new disk**. |
| 49 | + - | |
| 50 | + Enter a name for your managed disk. Review the default settings, and update the **Storage type**, **Size (GiB)**, **Encryption** and **Host caching** as necessary. |
| 51 | +
|
| 52 | + :::image type="content" source="./media/attach-disk-portal/create-new-md.png" alt-text="Screenshot of review disk settings." lightbox="./media/attach-disk-portal/create-new-md.png"::: |
| 53 | + |
| 54 | + - | |
| 55 | + When you're done, select **Save** at the top of the page to create the managed disk and update the VM configuration. |
| 56 | + |
| 57 | + - title: | |
| 58 | + Attach an existing disk |
| 59 | + summary: | |
| 60 | + Follow these steps: |
| 61 | + steps: |
| 62 | + - | |
| 63 | + On the **Disks** pane, under **Data disks**, select **Attach existing disks**. |
| 64 | + - | |
| 65 | + Select the drop-down menu for **Disk name** and select a disk from the list of available managed disks. |
| 66 | + - | |
| 67 | + Select **Save** to attach the existing managed disk and update the VM configuration: |
| 68 | +
|
| 69 | + - title: | |
| 70 | + Connect to the Linux VM to mount the new disk |
| 71 | + summary: | |
| 72 | + To partition, format, and mount your new disk so your Linux VM can use it, SSH into your VM. For more information, see [How to use SSH with Linux on Azure](mac-create-ssh-keys.md). The following example connects to a VM with the public IP address of *10.123.123.25* with the username *azureuser*: |
| 73 | + code: | |
| 74 | + ```bash |
| 75 | + |
| 76 | + ``` |
| 77 | +
|
| 78 | + - title: | |
| 79 | + Find the disk |
| 80 | + summary: | |
| 81 | + Once connected to your VM, you need to find the disk. In this example, we're using `lsblk` to list the disks. |
| 82 | + code: | |
| 83 | + ```bash |
| 84 | + lsblk -o NAME,HCTL,SIZE,MOUNTPOINT | grep -i "sd" |
| 85 | + ``` |
| 86 | +
|
| 87 | + The output is similar to the following example: |
| 88 | +
|
| 89 | + ```output |
| 90 | + sda 0:0:0:0 30G |
| 91 | + ├─sda1 29.9G / |
| 92 | + ├─sda14 4M |
| 93 | + └─sda15 106M /boot/efi |
| 94 | + sdb 1:0:1:0 14G |
| 95 | + └─sdb1 14G /mnt |
| 96 | + sdc 3:0:0:0 4G |
| 97 | + ``` |
| 98 | +
|
| 99 | + In this example, the disk that was added was `sdc`. It's a LUN 0 and is 4GB. |
| 100 | +
|
| 101 | + For a more complex example, here's what multiple data disks look like in the portal: |
| 102 | +
|
| 103 | + :::image type="content" source="./media/attach-disk-portal/find-disk.png" alt-text="Screenshot of multiple disks shown in the portal."::: |
| 104 | +
|
| 105 | + In the image, you can see that there are 3 data disks: 4 GB on LUN 0, 16GB at LUN 1, and 32G at LUN 2. |
| 106 | +
|
| 107 | + Here's what that might look like using `lsblk`: |
| 108 | +
|
| 109 | + ```output |
| 110 | + sda 0:0:0:0 30G |
| 111 | + ├─sda1 29.9G / |
| 112 | + ├─sda14 4M |
| 113 | + └─sda15 106M /boot/efi |
| 114 | + sdb 1:0:1:0 14G |
| 115 | + └─sdb1 14G /mnt |
| 116 | + sdc 3:0:0:0 4G |
| 117 | + sdd 3:0:0:1 16G |
| 118 | + sde 3:0:0:2 32G |
| 119 | + ``` |
| 120 | +
|
| 121 | + From the output of `lsblk` you can see that the 4GB disk at LUN 0 is `sdc`, the 16GB disk at LUN 1 is `sdd`, and the 32G disk at LUN 2 is `sde`. |
| 122 | + |
| 123 | + ### Prepare a new empty disk |
| 124 | +
|
| 125 | + > [!IMPORTANT] |
| 126 | + > If you are using an existing disk that contains data, skip to [mounting the disk](#mount-the-disk). |
| 127 | + > The following instructions will delete data on the disk. |
| 128 | +
|
| 129 | + If you're attaching a new disk, you need to partition the disk. |
| 130 | +
|
| 131 | + The `parted` utility can be used to partition and to format a data disk. |
| 132 | + - Use the latest version `parted` that is available for your distro. |
| 133 | + - 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. |
| 134 | +
|
| 135 | +
|
| 136 | + 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](https://xfs.wiki.kernel.org/) filesystem. |
| 137 | + |
| 138 | + ```bash |
| 139 | + sudo parted /dev/sdc --script mklabel gpt mkpart xfspart xfs 0% 100% |
| 140 | + sudo mkfs.xfs /dev/sdc1 |
| 141 | + sudo partprobe /dev/sdc1 |
| 142 | + ``` |
| 143 | +
|
| 144 | + Use the [`partprobe`](https://linux.die.net/man/8/partprobe) utility to make sure the kernel is aware of the new partition and filesystem. Failure to use `partprobe` can cause the blkid or lslbk commands to not return the UUID for the new filesystem immediately. |
| 145 | +
|
| 146 | + ### Mount the disk |
| 147 | +
|
| 148 | + Create a directory to mount the file system using `mkdir`. The following example creates a directory at `/datadrive`: |
| 149 | +
|
| 150 | + ```bash |
| 151 | + sudo mkdir /datadrive |
| 152 | + ``` |
| 153 | +
|
| 154 | + Use `mount` to then mount the filesystem. The following example mounts the */dev/sdc1* partition to the `/datadrive` mount point: |
| 155 | +
|
| 156 | + ```bash |
| 157 | + sudo mount /dev/sdc1 /datadrive |
| 158 | + ``` |
| 159 | + 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: |
| 160 | +
|
| 161 | + ```bash |
| 162 | + sudo blkid |
| 163 | + ``` |
| 164 | +
|
| 165 | + The output looks similar to the following example: |
| 166 | +
|
| 167 | + ```output |
| 168 | + /dev/sda1: LABEL="cloudimg-rootfs" UUID="11111111-1b1b-1c1c-1d1d-1e1e1e1e1e1e" TYPE="ext4" PARTUUID="1a1b1c1d-11aa-1234-1a1a1a1a1a1a" |
| 169 | + /dev/sda15: LABEL="UEFI" UUID="BCD7-96A6" TYPE="vfat" PARTUUID="1e1g1cg1h-11aa-1234-1u1u1a1a1u1u" |
| 170 | + /dev/sdb1: UUID="22222222-2b2b-2c2c-2d2d-2e2e2e2e2e2e" TYPE="ext4" TYPE="ext4" PARTUUID="1a2b3c4d-01" |
| 171 | + /dev/sda14: PARTUUID="2e2g2cg2h-11aa-1234-1u1u1a1a1u1u" |
| 172 | + /dev/sdc1: UUID="33333333-3b3b-3c3c-3d3d-3e3e3e3e3e3e" TYPE="xfs" PARTLABEL="xfspart" PARTUUID="c1c2c3c4-1234-cdef-asdf3456ghjk" |
| 173 | + ``` |
| 174 | +
|
| 175 | + > [!NOTE] |
| 176 | + > 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. |
| 177 | +
|
| 178 | + 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: |
| 179 | +
|
| 180 | + ```config |
| 181 | + UUID=33333333-3b3b-3c3c-3d3d-3e3e3e3e3e3e /datadrive xfs defaults,nofail 1 2 |
| 182 | + ``` |
| 183 | +
|
| 184 | + When you're done editing the file, save and close the editor. |
| 185 | +
|
| 186 | + > [!NOTE] |
| 187 | + > Later removing a data disk without editing fstab could cause the VM to fail to boot. Most distributions provide either the *nofail* and/or *nobootwait* fstab options. These options allow a system to boot even if the disk fails to mount at boot time. Consult your distribution's documentation for more information on these parameters. |
| 188 | + > |
| 189 | + > The *nofail* option ensures that the VM starts even if the filesystem is corrupt or the disk does not exist at boot time. Without this option, you may encounter behavior as described in [Cannot SSH to Linux VM due to FSTAB errors](/archive/blogs/linuxonazure/cannot-ssh-to-linux-vm-after-adding-data-disk-to-etcfstab-and-rebooting) |
| 190 | +
|
| 191 | +
|
| 192 | + - title: | |
| 193 | + Verify the disk |
| 194 | + summary: | |
| 195 | + You can now use `lsblk` again to see the disk and the mountpoint. |
| 196 | +
|
| 197 | + ```bash |
| 198 | + lsblk -o NAME,HCTL,SIZE,MOUNTPOINT | grep -i "sd" |
| 199 | + ``` |
| 200 | +
|
| 201 | + The output will look something like this: |
| 202 | +
|
| 203 | + ```output |
| 204 | + sda 0:0:0:0 30G |
| 205 | + ├─sda1 29.9G / |
| 206 | + ├─sda14 4M |
| 207 | + └─sda15 106M /boot/efi |
| 208 | + sdb 1:0:1:0 14G |
| 209 | + └─sdb1 14G /mnt |
| 210 | + sdc 3:0:0:0 4G |
| 211 | + └─sdc1 4G /datadrive |
| 212 | + ``` |
| 213 | +
|
| 214 | + You can see that `sdc` is now mounted at `/datadrive`. |
| 215 | +
|
| 216 | + ### TRIM/UNMAP support for Linux in Azure |
| 217 | +
|
| 218 | + Some Linux kernels support TRIM/UNMAP operations to discard unused blocks on the disk. This feature is primarily useful to inform Azure that deleted pages are no longer valid and can be discarded. This feature can save money on disks that are billed based on the amount of consumed storage, such as unmanaged standard disks and disk snapshots. |
| 219 | +
|
| 220 | + There are two ways to enable TRIM support in your Linux VM. As usual, consult your distribution for the recommended approach: |
| 221 | + steps: |
| 222 | + - | |
| 223 | + Use the `discard` mount option in */etc/fstab*, for example: |
| 224 | +
|
| 225 | + ```config |
| 226 | + UUID=33333333-3b3b-3c3c-3d3d-3e3e3e3e3e3e /datadrive xfs defaults,discard 1 2 |
| 227 | + ``` |
| 228 | + - | |
| 229 | + In some cases, the `discard` option may have performance implications. Alternatively, you can run the `fstrim` command manually from the command line, or add it to your crontab to run regularly: |
| 230 | +
|
| 231 | + **Ubuntu** |
| 232 | +
|
| 233 | + ```bash |
| 234 | + sudo apt-get install util-linux |
| 235 | + sudo fstrim /datadrive |
| 236 | + ``` |
| 237 | +
|
| 238 | + **RHEL** |
| 239 | +
|
| 240 | + ```bash |
| 241 | + sudo yum install util-linux |
| 242 | + sudo fstrim /datadrive |
| 243 | + ``` |
| 244 | +
|
| 245 | + **SUSE** |
| 246 | +
|
| 247 | + ```bash |
| 248 | + sudo zypper install util-linux |
| 249 | + sudo fstrim /datadrive |
| 250 | + ``` |
| 251 | +
|
| 252 | +relatedContent: |
| 253 | + - text: Troubleshoot Linux VM device name changes |
| 254 | + url: /troubleshoot/azure/virtual-machines/troubleshoot-device-names-problems |
| 255 | + - text: Attach a data disk using the Azure CLI |
| 256 | + url: add-disk.md |
| 257 | +#For more information, and to help troubleshoot disk issues, see [Troubleshoot Linux VM device name changes](/troubleshoot/azure/virtual-machines/troubleshoot-device-names-problems). |
| 258 | + |
| 259 | +#You can also [attach a data disk](add-disk.md) using the Azure CLI. |
0 commit comments