Provisioning a New Data Disk

Working with Linux on Azure has brought me back to the basics. This post is all about adding a new Data Disk to an existing Azure CentOS Virtual Machine. If you don’t have a Virtual Machine handy, I wrote a short walkthrough to help you get going.

1) Adding a Data Disk to a Virtual Machine

Using the Azure Resource Manager PowerShell CmdLets, we must get a reference to our provisioned Virtual Machine and add a new Data Disk.

Switch-AzureMode -Name AzureResourceManager

$resourceGroup = 'mslinuxbrisebois'

$storageAccount = Get-AzureStorageAccount -ResourceGroupName $resourceGroup `
                                          -Name 'mslinuxbrisebois'

Set-AzureSubscription -SubscriptionName (Get-AzureSubscription -Current).SubscriptionName `
                      -CurrentStorageAccountName $storageAccount.Name


$vm = Get-AzureVM -ResourceGroupName $resourceGroup `
                  -Name 'msbriseboislinux'

Add-AzureVMDataDisk -VM $vm `
                    -Name 'datadisk0' `
                    -DiskSizeInGB 100 `
                    -Lun 0 `
                    -CreateOption empty `
                    -VhdUri "https://mslinuxbrisebois.blob.core.windows.net/vhds/datadisk0.vhd" `
| Update-AzureVM -Tags @{Name = "Updated"; Value ="2015-09-01"}

 

2) Mounting, Partitioning & Formatting a Data Disk

Once the Virtual Machine is ready, use the Azure portal to find out which public port was configured as the SSH port. Usually, it will be port 22. Then use your favorite SSH client to an SSH Session. In this post, I will be using PuTTY.

Once we’re logged in, let’s prepare the Virtual Machine for the next steps by installing mdadm.

sudo yum install mdadm

To verify that our new Data Disk has been added properly, use the sfdisk command. It will list the disks as being available and ready to be mounted.

sudo sfdisk -l

Disk /dev/sdb: 9137 cylinders, 255 heads, 63 sectors/track
Units: cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0

   Device Boot Start     End   #cyls    #blocks   Id  System
/dev/sdb1   *      0+   9137-   9138-  73398272   83  Linux
/dev/sdb2          0       -       0          0    0  Empty
/dev/sdb3          0       -       0          0    0  Empty
/dev/sdb4          0       -       0          0    0  Empty

Disk /dev/sda: 3916 cylinders, 255 heads, 63 sectors/track
Units: cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0

   Device Boot Start     End   #cyls    #blocks   Id  System
/dev/sda1   *      0+   3788-   3789-  30432256   83  Linux
/dev/sda2       3788+   3916-    128-   1024000   82  Linux swap / Solaris
/dev/sda3          0       -       0          0    0  Empty
/dev/sda4          0       -       0          0    0  Empty

Disk /dev/sdc: 13054 cylinders, 255 heads, 63 sectors/track

From this listing we identified our new Data Disk as /dev/sdc. The next step is to Partition the disk using fdisk.

sudo fdisk /dev/sdc

Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x584723f7.

Command (m for help): p

Disk /dev/sdc: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x584723f7

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-209715199, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199):
Using default value 209715199
Partition 1 of type Linux and of size 100 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Next we need to create a File System on the newly created Partition.

sudo /sbin/mkfs.ext4 -L datadisk0 /dev/sdc1     

mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=datadisk0
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
6553600 inodes, 26214144 blocks
1310707 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2174746624
800 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

We can now use blkid to retrieve the UUID that we will use to Mount the File System.

sudo blkid

/dev/sr0: LABEL="rd_rdfe_stable.150604-1401" TYPE="udf"
/dev/sdb1: UUID="638cb1ca-16a3-432b-bcc5-7cc9a9e23ff0" TYPE="ext4"
/dev/sda1: UUID="427e4cf4-85d2-4b58-ac5b-a5c12d0b70dd" TYPE="ext4"
/dev/sda2: UUID="89aabb77-9b57-40cd-8469-da8c6016cd5d" TYPE="swap"
/dev/sdc1: LABEL="datadisk0" UUID="4615be34-2157-4487-ae56-a9e47145ca6b" TYPE="ext4"

We’re now ready to mount the File System to our CentOS Virtual Machine. In this example, we are mounting it to /data by editing the /etc/fstab file.

sudo mkdir /data

sudo vi /etc/fstab
[i]
UUID=4615be34-2157-4487-ae56-a9e47145ca6b /data                   ext4    defaults        0 2
[esc]
[:w]
[:q]

Be sure to review and test the newly added configurations. Keep in mind that an invalid fstab can prevent you from successfully restarting the Virtual Machine.

When something goes wrong, and the Virtual Machine is stuck in the boot cycle. Attach the OS Disk to another Linux Virtual Machine and correct the fstab. Once everything is valid, you can rehydrate the Virtual Machine.

sudo cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Tue Jul 22 19:41:26 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=427e4cf4-85d2-4b58-ac5b-a5c12d0b70dd /                       ext4    defaults        1 1
UUID=4615be34-2157-4487-ae56-a9e47145ca6b /data                   ext4    defaults        0 2

sudo mount -a

mount

proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=851084k,nr_inodes=212771,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,seclabel)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,seclabel,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (rw,nosuid,nodev,noexec,seclabel,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpuacct,cpu)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/net_cls type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/sda1 on / type ext4 (rw,relatime,seclabel,data=ordered)
selinuxfs on /sys/fs/selinux type selinuxfs (rw,relatime)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=34,pgrp=1,timeout=300,minproto=5,maxproto=5,direct)
mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
/dev/sdb1 on /mnt/resource type ext4 (rw,relatime,seclabel,data=ordered)
/dev/sdc1 on /data type ext4 (rw,relatime,seclabel,data=ordered)

Now that we’ve configured and verified and tested, it’s time to have a look.

mounted-disk

This screen capture, shows us that we’ve reached out goal. The Data Disk is now provisioned and ready to use.

2 responses to Provisioning a Data Disk on a CentOS Virtual Machine on Azure

  1. 
    Rodrigo Murillo G. March 31, 2018 at 11:19 PM

    Very useful. Thanks!!!

    Like

  2. 

    useful thanks ..loved the article

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.