Sie sind auf Seite 1von 10

How to Attach a Data Disk to a

Linux Virtual Machine


By Iain FouldsUpdated: 06/07/2016
Contributors:


Edit on GitHub

Important:

Azure has two different deployment models for creating and working with
resources:Resource Manager and classic. This article covers using the classic deployment
model. Microsoft recommends that most new deployments use the Resource Manager
model. . See how to attach a data disk using the Resource Manager deployment model.

You can attach both empty disks and disks that contain data to your Azure VMs. Both
types of disks are .vhd files that reside in an Azure storage account. As with adding any
disk to a Linux machine, after you attach the disk you'll need to initialize and format it so
it's ready for use. This article details attaching both empty disks and disks already
containing data to your VMs, as well as how to then initialize and format a new disk.

Note:

It's a best practice to use one or more separate disks to store a virtual machine's data.
When you create an Azure virtual machine, it has an operating system disk and a
temporary disk. Do not use the temporary disk to store persistent data. As the name
implies, it provides temporary storage only. It offers no redundancy or backup because
it doesn't reside in Azure storage. The temporary disk is typically managed by the Azure
Linux Agent and automatically mounted to /mnt/resource (or /mnt on Ubuntu
images). On the other hand, a data disk might be named by the Linux kernel something
like/dev/sdc, and you'll need to partition, format, and mount this resource. See
the Azure Linux Agent User Guide for details.

For more details about disks, see About Disks and VHDs for Virtual Machines.
Attach an empty disk
1. Open Azure CLI and connect to your Azure subscription. Make sure you are in Azure
Service Management mode (azure config mode asm).

2. Enter azure vm disk attach-new to create and attach a new disk as shown in the
following. Replace TestVM with the name of your Linux Virtual Machine and specify the
size of the disk in GB, which is 100GB in this example:

Copy
azure vm disk attach-new TestVM 100

3. After the data disk is created and attached, it's listed in the output of azure vm disk
list <virtual-machine-name> as shown in the following:

Copy
$ azure vm disk list TestVM
info: Executing command vm disk list
+ Fetching disk images
+ Getting virtual machines
+ Getting VM disks
data: Lun Size(GB) Blob-Name OS
data: --- -------- -------------------------------- -----
data: 30 TestVM-2645b8030676c8f8.vhd Linux
data: 0 100 TestVM-76f7ee1ef0f6dddc.vhd
info: vm disk list command OK

Attach an existing disk


Attaching an existing disk requires that you have a .vhd available in a storage account.

1. Open Azure CLI and connect to your Azure subscription. Make sure you are in Azure
Service Management mode (azure config mode asm).

2. Check if the VHD you want to attach is already uploaded to your Azure subscription:

Copy
$azure vm disk list
info: Executing command vm disk list
+ Fetching disk images
data: Name OS
data: -------------------------------------------- -----
data: myTestVhd Linux
data: TestVM-ubuntuVMasm-0-201508060029150744 Linux
data: TestVM-ubuntuVMasm-0-201508060040530369
info: vm disk list command OK

3. If you don't find the disk that you want to use, you may upload a local VHD to your
subscription by using azure vm disk create or azure vm disk upload. An example
ofdisk create would be as in the following:

Copy
$azure vm disk create myTestVhd2 .\TempDisk\test.VHD -l "East US" -o Linux
info: Executing command vm disk create
+ Retrieving storage accounts
info: VHD size : 10 GB
info: Uploading 10485760.5 KB
Requested:100.0% Completed:100.0% Running: 0 Time: 25s Speed: 82 KB/s
info: Finishing computing MD5 hash, 16% is complete.
info: https://mystorageaccount.blob.core.windows.net/disks/test.VHD was
uploaded successfully
info: vm disk create command OK

You may also use azure vm disk upload to upload a VHD to a specific storage account.
Read more about the commands to manage your Azure virtual machine data disks over
here.

4. Now you attach the desired VHD to your virtual machine:

Copy
$azure vm disk attach TestVM myTestVhd
info: Executing command vm disk attach
+ Getting virtual machines
+ Adding Data-Disk
info: vm disk attach command OK

Make sure to replace TestVM with the name of your virtual machine,
and myTestVhd with your desired VHD.

5. You can verify the disk is attached to the virtual machine with azure vm disk list
<virtual-machine-name>:

Copy
$azure vm disk list TestVM
info: Executing command vm disk list
+ Fetching disk images
+ Getting virtual machines
+ Getting VM disks
data: Lun Size(GB) Blob-Name OS
data: --- -------- -------------------------------- -----
data: 30 TestVM-2645b8030676c8f8.vhd Linux
data: 1 10 test.VHD
data: 0 100 TestVM-76f7ee1ef0f6dddc.vhd
info: vm disk list command OK
Note:

After you add a data disk, you'll need to log on to the virtual machine and initialize the
disk so the virtual machine can use the disk for storage (see the steps below for more
information on how to do this).

Initialize a new data disk in Linux


1. SSH to your VM. For further details, see How to log on to a virtual machine running
Linux.

2. Next you need to find the device identifier for the data disk to initialize. There are two
ways to do that:

a) Grep for SCSI devices in the logs, such as in the following command:

Copy
$sudo grep SCSI /var/log/messages

For recent Ubuntu distributions, you may need to use sudo grep SCSI
/var/log/syslogbecause logging to /var/log/messages might be disabled by default.

You can find the identifier of the last data disk that was added in the messages that are
displayed.

OR

b) Use the lsscsi command to find out the device id. lsscsi can be installed by
either yum install lsscsi (on Red Hat based distributions) or apt-get install
lsscsi (on Debian based distributions). You can find the disk you are looking for by
its lun or logical unit number. For example, the lun for the disks you attached can be
easily seen from azure vm disk list <virtual-machine> as:

Copy
~$ azure vm disk list TestVM
info: Executing command vm disk list
+ Fetching disk images
+ Getting virtual machines
+ Getting VM disks
data: Lun Size(GB) Blob-Name OS
data: --- -------- -------------------------------- -----
data: 30 TestVM-2645b8030676c8f8.vhd Linux
data: 0 100 TestVM-76f7ee1ef0f6dddc.vhd
info: vm disk list command OK

Compare this with the output of lsscsi for the same sample virtual machine:

Copy
ops@TestVM:~$ lsscsi
[1:0:0:0] cd/dvd Msft Virtual CD/ROM 1.0 /dev/sr0
[2:0:0:0] disk Msft Virtual Disk 1.0 /dev/sda
[3:0:1:0] disk Msft Virtual Disk 1.0 /dev/sdb
[5:0:0:0] disk Msft Virtual Disk 1.0 /dev/sdc

The last number in the tuple in each row is the lun. See man lsscsi for more information.

3. At the prompt, type the following command to create your new device:

Copy
$sudo fdisk /dev/sdc

4. When prompted, type n to create a new partition.


5. When prompted, type p to make the partition the primary partition, type 1 to make it
the first partition, and then type enter to accept the default value for the cylinder. On
some systems, it can show the default values of the first and the last sectors, instead of
the cylinder. You can choose to accept these defaults.

6. Type p to see the details about the disk that is being partitioned.

7. Type w to write the settings for the disk.


8. Now you can create the file system on the new partition. Append the partition number
to the device ID (in the following example /dev/sdc1). The following example creates an
ext4 partition on /dev/sdc1:

Copy
# sudo mkfs -t ext4 /dev/sdc1

Note:

Note that SuSE Linux Enterprise 11 systems only support read-only access for ext4 file
systems. For these systems it is recommended to format the new file system as ext3
rather than ext4.

9. Make a directory to mount the new file system, as follows:


Copy
# sudo mkdir /datadrive

10. Finally you can mount the drive, as follows:

Copy to clipboardCopy
# sudo mount /dev/sdc1 /datadrive

The data disk is now ready to use as /datadrive.

11. Add the new drive to /etc/fstab:

To ensure the drive is re-mounted automatically after a reboot it must be added to the
/etc/fstab file. In addition, it is highly recommended that the UUID (Universally Unique
IDentifier) is used in /etc/fstab to refer to the drive rather than just the device name (i.e.
/dev/sdc1). This avoids the incorrect disk being mounted to a given location if the OS
detects a disk error during boot and any remaining data disks then being assigned
those device IDs. To find the UUID of the new drive you can use the blkid utility:

Copy
# sudo -i blkid

The output will look similar to the following:

Copy
/dev/sda1: UUID="11111111-1b1b-1c1c-1d1d-1e1e1e1e1e1e" TYPE="ext4"
/dev/sdb1: UUID="22222222-2b2b-2c2c-2d2d-2e2e2e2e2e2e" TYPE="ext4"
/dev/sdc1: UUID="33333333-3b3b-3c3c-3d3d-3e3e3e3e3e3e" TYPE="ext4"
Note:

Improperly editing the /etc/fstab file could result in an unbootable system. If unsure,
please refer to the distribution's documentation for information on how to properly edit
this file. It is also recommended that a backup of the /etc/fstab file is created before
editing.

Next, open the /etc/fstab file in a text editor:


Copy
# sudo vi /etc/fstab

In this example we will use the UUID value for the new /dev/sdc1 device that was
created in the previous steps, and the mountpoint /datadrive. Add the following line to
the end of the/etc/fstab file:

Copy
UUID=33333333-3b3b-3c3c-3d3d-3e3e3e3e3e3e /datadrive ext4 defaults 1 2

Or, on systems based on SuSE Linux you may need to use a slightly different format:

Copy
/dev/disk/by-uuid/33333333-3b3b-3c3c-3d3d-3e3e3e3e3e3e /datadrive ext3
defaults 1 2

You can now test that the file system is mounted properly by simply unmounting and
then re-mounting the file system, i.e. using the example mount point /datadrive created
in the earlier steps:

Copy
# sudo umount /datadrive
# sudo mount /datadrive

If the mount command produces an error, check the /etc/fstab file for correct syntax. If
additional data drives or partitions are created you will need to enter them into
/etc/fstab separately as well.

You will need to make the drive writable by using this command:

Copy
# sudo chmod go+w /datadrive
Note:

Subsequently removing a data disk without editing fstab could cause the VM to fail to
boot. If this is a common occurrence, most distributions provide either
the nofailand/or nobootwait fstab options that will allow a system to boot even if the
disk fails to mount at boot time. Please consult your distribution's documentation for
more information on these parameters.

TRIM/UNMAP support for Linux in Azure


Some Linux kernels will support TRIM/UNMAP operations to discard unused blocks on
the disk. This is primarily useful in standard storage to inform Azure that deleted pages
are no longer valid and can be discarded. This can save cost if you create large files and
then delete them.

There are two ways to enable TRIM support in your Linux VM. As usual, please consult
your distribution for the recommended approach:

Use the discard mount option in /etc/fstab, for example:

Copy
UUID=33333333-3b3b-3c3c-3d3d-3e3e3e3e3e3e /datadrive ext4 defaults,discard
1 2

Alternatively, you can run the fstrim command manually from the command-line, or
add it to your crontab to run regularly:

Ubuntu

Copy
# sudo apt-get install util-linux
# sudo fstrim /datadrive

RHEL/CentOS

Copy
# sudo yum install util-linux
# sudo fstrim /datadrive

Next Steps
You can read more about using your Linux VM in the following articles:

How to log on to a virtual machine running Linux

How to detach a disk from a Linux virtual machine

Using the Azure CLI with the Classic deployment model

Das könnte Ihnen auch gefallen