How to attach a data disk to a Linux VM
Published: 2023-04-14 11:42:19



Hello there,
We learnt how to create a virtual machine(you can recap [here](https://dev.to/jeptoo/how-to-create-ec2-instance-ubuntu-2204-on-aws-and-connect-via-ssh-using-pem-492o)) and today we are going to cover how to create a data disk and attaching it to the virtual machine.

## Table of Contents

 [Introduction](#introduction)

 [Attach Disk](#attach-disk)

 [Find Disk](#find-disk)

 [Prepare new disk](#prepare-new-disk)

 [Mount Disk](#mount-disk)

 [Verify 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/31s44t3f2oic4hvlg4ba.png)


##Attach Disk.
- In the Virtual Machine page, Under **Setting**s 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y89zmtmefly55fmz5nri.png)

###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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cjepam2klxpmvlnbimp2.png)

- 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 `lsblk `that 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 

```

##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
```

##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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l40umjj3pe7j6o6a215s.png)

##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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ngmcy8o28rus2q7n4hgb.png)

-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.