DEV Community

Ivy Jeptoo
Ivy Jeptoo

Posted on

How to attach a data disk to a Linux VM

Hello there,
We learnt how to create a virtual machine(you can recap here) and today we are going to cover how to create a data disk and attaching it to the virtual machine.

Table of Contents

Introduction

Attach Disk

Find Disk

Prepare new disk

Mount Disk

Verify Disk.

Introduction.

  • Data disk are separate storage units attached to VMs to enhance the storage capacity and performance without affecting operating system or application.
  • We will explore the step-by-step process of using the Azure portal to create and attach a data disk to a Linux VM, and then mount it so that it can be used for storing data or running applications.

  • Select VM from the Azure portal and select one that you'll use or create a new one if need be.

Use image for reference

vm

Attach Disk.

  • In the Virtual Machine page, Under Settings choose Disk options

Attaching a new disk.

  • Select Create and attach a new disk under the Data disks pane
  • Give your managed disk a name and configure the default setting.
  • Select save at the top of page to save your new disk and update the VM configuration.

Use image for reference

new disk

Attaching an existing disk.

  • Select Attach existing disks under the Data disk.
  • From the drop down choose the desired disk you would want to work with.
  • Select save at the top of page to save your new disk and update the VM configuration.

Find Disk

Make sure you are connected to your Virtual Machine

  • Follow the steps from the image below to connect to your Virtual Machine.
    connecting

  • You are now connected to the VM and we need to find the just created disk. Run the following command on the terminal

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

N/B
lsblk - lists information about all available block devices, including disks and partitions.

-o NAME,HCTL,SIZE,MOUNTPOINT - specifies the columns of information to display in the output.We are asking lsblk to display the device name, host controller target and logical unit number (HCTL), size, and mount point (if any) for each block device.

| - a pipe symbol that redirects the output of the lsblk command to the next command in the pipeline.

grep -i "sd" - searches for lines in the output of lsblkthat contain the characters "sd" (case-insensitive).

"sd" is typically used to indicate SCSI disks in Linux.

The expected output should be:

sda     0:0:0:0      30G 
├─sda1             29.9G /
├─sda14               4M 
└─sda15             106M /boot/efi
sdb     0:0:0:1       4G 
└─sdb1                4G /mnt
sdc     1:0:0:0       4G 

Enter fullscreen mode Exit fullscreen mode

Prepare new disk

  • If you are using an existing disk that contains data, skip to mounting the disk. The following instructions will delete data on the disk.

  • If you are using a new disk you need to partition it because:

  1. You can **organize data **into logical units, making it easier to manage and locate specific files.

  2. It allows you to isolate certain data sets from others so if one partition is compromised, the other partitions remain safe.

  3. Improve performance by allowing you to separate frequently accessed files from those that are rarely accessed. Hence optimize the disk usage and reduce the time it takes to access data.

-To partition run this command, make sure to replace sdc with the correct option for your disk

sudo parted /dev/sdc --script mklabel gpt mkpart xfspart xfs 0% 100%
sudo mkfs.xfs /dev/sdc
sudo partprobe /dev/sdc
Enter fullscreen mode Exit fullscreen mode

Mount disk

  • Once the file system is created, the disk needs to be mounted to a specific directory in the file system hierarchy to be accessible to users and applications.
  • Create a directory to mount the file system using mkdir. The following example creates a directory at /datadrive:
    sudo mkdir /datadrive

  • Mount the /dev/sdc partition to the /datadrive mount point:
    sudo mount /dev/sdc /datadrive

  • We need to find the UUID of the newly attached drive:

sudo blkid

The expected output:
UUID

Verify disk

  • use lsblk command again to see the disk and the mountpoint.

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

The expected output:

output

-Well done!! You can see that sdc is now mounted at /datadrive.

These steps are crucial for ensuring a smooth and efficient computing experience, and taking the time to properly prepare and mount a new disk can save time and effort in the long run.

Top comments (0)