Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Adding A Disk To Linux

The following tutorial will show you how to add a disk in Linux, stepping you through the creation of a partition table, before adding a partition and adding a filesystem to it. Then set it up to automatically mount on boot.

Steps

sudo apt install parted
sudo parted /dev/sd[x]

Use the print command to output information of the disk (and check you got the correct one)

print
Error: /dev/sdd: unrecognised disk label
Model: ATA WDC WD80EFBX-68A (scsi)                                        
Disk /dev/sdd: 8002GB
Sector size (logical/physical): 512B/4096B
Partition Table: unknown
Disk Flags:

The first thing I want to do is create a GPT partition table.

mktable gpt

If the disk does not have a partition table, this will proceed without any output. If the disk already has a partition table (which it most likely will, unless its a fresh disk you just bought, then you will get the following output:

Warning: The existing disk label on /dev/sdd will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No?

Just type in Yes to confirm.

Now if I print, I will see the gpt partition table:

(parted) print                                                            
Model: ATA WDC WD80EFBX-68A (scsi)
Disk /dev/sdd: 8002GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

Now I am going to create the first partition with fdisk, because its easy to create a 100% partition automatically.

Start by quitting out of parted:

quit

Then run fdisk on the drive:

sudo fdisk /dev/sd[x]

Print out the drive to make sure your editing the correct one:

p
Disk /dev/sdd: 7.3 TiB, 8001563222016 bytes, 15628053168 sectors
Disk model: WDC WD80EFBX-68A
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: B8BCB3F0-D386-4BF9-90DC-D25FE18025C0

Create a new partition with:

n

At the prompts, just keep pressing enter to use the defaults and create a partition that fills the drive:

Partition number (1-128, default 1): 
First sector (34-15628053134, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-15628053134, default 15628053134): 

Created a new partition 1 of type 'Linux filesystem' and of size 7.3 TiB.

If you need to make the partition bootable (e.g. its not just a data storage disk) then set the bootable flag with:

a

Write your changes:

w

Create the filesystem

sudo mkfs -t ext4 /dev/sdd1

Automatically Mount (Add to fstab)

Create a directory of where you want the drive to be mounted. E.g.

sudo mkdir /mnt/hdd1

Run the following command to get the UUID of the filesystem you just created:

sudo blkid
/dev/sda1: UUID="ae36618b-af0c-420b-ac6b-5da4dbf12d32" TYPE="ext4" PARTUUID="511b3de5-01"
/dev/sda5: UUID="2434627b-ff45-4c82-82d3-f07e3afa63d4" TYPE="swap" PARTUUID="511b3de5-05"
/dev/sdb: UUID="xLEzyA-ipa5-ec3a-wfyB-2fTo-Efx0-rNnY15" TYPE="LVM2_member"
/dev/sdc: UUID="YdmHoI-gikF-lstq-Hjl3-831c-5yM2-TuCHM9" TYPE="LVM2_member"
/dev/mapper/vg1-lvm_raid1_rimage_0: UUID="df167969-d147-42e3-9379-bfc3b305e24b" TYPE="ext4"
/dev/mapper/vg1-lvm_raid1_rimage_1: UUID="df167969-d147-42e3-9379-bfc3b305e24b" TYPE="ext4"
/dev/mapper/vg1-lvm_raid1: UUID="df167969-d147-42e3-9379-bfc3b305e24b" TYPE="ext4"
/dev/sdd1: UUID="a623e4b0-2b13-4c15-ba9a-01f4b76da4f2" TYPE="ext4" PARTUUID="d05678b8-59f0-4c44-95a4-4a244d70b77a"

Copy the UUID beside your partition name (e.g. /dev/sdd1) and add it to your fstab like so:

sudo editor /etc/fstab
# Mount hdd1
UUID="a623e4b0-2b13-4c15-ba9a-01f4b76da4f2"     /mnt/hdd1       ext4    defaults        0       2

You don't have to add the comments, but I find they make life easier. Obviously if you used a different filesystem, or mount point, adjust accordingly.

Now mount:

sudo mount -a

You probably want to set yourself as the owner so that you can now write to the filesystem

sudo chown $USER:$USER /mnt/hdd1

References

Last updated: 16th June 2021
First published: 15th June 2021

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch