Adding a New Drive to Ubuntu 18.04LTS
It’s not every day that you add a new drive to your desktop or server. And whilst adding a drive to Ubuntu with a GUI is relatively simple, adding a drive from the command line is …. well … pretty simple too actually. You just have to know the commands. And because most people don’t do it often, they’re easy to forget. But fear not. The instructions for adding a new drive to Ubuntu are below. I assume that you’ve already added your drive to your hardware or attached a new virtual disk to your Virtual Machine already.
The Procure For Adding a Disk
There are a few steps to adding a new drive to Ubuntu.
- Fine the name of the new drive.
- Label the drive as mbr or gpt
- Ensure we’re working with the right units (GB or TB)
- Partition the drive.
- Create a file system on the new drive.
- Update the fstab file
As you can see there really isn’t a lot to it. So let’s get cracking.
Finding the Drive
OK, so as above the first thing we want to do is find the name of the drive we’ve added. We do this by using the list hardware command.
lshw is most likely already installed but if not you can install it by running the following :
sudo apt install lshw
Once installed we want to ask lshw to show us all the hardware with a class of disk
sudo lshw -C disk
We can see from the above, we have 3 drives – one of which is a DVD. We can see two drives of the type Virtual Disks. The size alone tells me the new disk (10TB in this case) is named /dev/sdb. However, the lack of ‘capabilities’ and GUID also tells me this is a new drive.
You need to be confident you have identified the correct drive before proceeding. The following section’s commands will wipe any data already on the drive, so if you get it wrong there will be tears before bedtime.
MBR & Fdisk Versus GPT & Parted
You have the option of two commands to partition your new drive. fdisk which is an older program and parted. fdisk can only create MBR partitions which has a limit of 2TB. parted can create both MBR and GPT but requires EFI support. As this tutorial is for Ubuntu 18.04LTS with a 10TB drive parted is the way to go.
In this example the new drive is /dev/sdb. Change the below for your particular drive name
sudo parted /dev/sdb
Type the following to create a gpt partition label
You may receive a warning that the existing drive label and all data will be destroyed. If this is a brand new drive you should not see this. If this is a drive you are reusing and happy to lose all data then proceed.
Next we want to set the units for the following command.
Then create the partion.
As the drive is new and I want to use all of my 10TB I will use the parameters 0 and 10. Change the latter parameter to the desired size.
mkpart primary 0 10
This will return with no information but create a 10TB partition
To check the output use the following command
With the partition create we can now quit parted
Getting The Drive Ready For Use
At this point we need to create our desired file system such as ext3, ext4, zfs. For our purposes we are going to create an ext4 file system
sudo mkfs.ext4 /dev/sdb1
Depending on the size of disk this can take a little time
The above may look like it’s stalled but don’t worry – it’s working hard creating our file system in the background.
OK we’re done. Before we mount the file system, let’s check what our drives currently look like
Now to mount /dev/sdb1 tso that it is usable in our system we need to make a folder for it and then ‘attach’ the file system to the folder. I am going to call my folder /data but you can call it what you want.
sudo mkdir /data
sudo mount /dev/sdb1 /data
Now let’s take another look at those disks.
We can see our new disk. As always there is an amount of overhead when creating a file system on disk. That’s why out 10TB disk has 9.1TB of usable space.
We can now start writing files to the disk. Try
Easier than you though? Great.
Mounting on Reboot
That’s it …… until you reboot your machine and the disk is gone. What! OK – you could reboot your server and check if you don’t believe me. If that was to happen you would simply need to remount your drive with
sudo mount /dev/sdb1 /data
But you don’t want to do that every time you reboot. That’s where the file system table comes in (fstab for short).
We want to edit the fstab file to ensure our disk is mounted everytime the server it rebooted.
sudo vi /etc/fstab
What we want to do is add a new line at the bottom of the file
/dev/sdb1 /data ext4 errors=remount-ro 0 2
You want to know what all that means? Oh – ok, go on then.
/dev/sdb1 is our disk. (Disk name may be different for your installation).
/data is the folder we mount to attach or ‘mount’ the drive to – called a mountpoint.
ext4 is the file system we chose for the drive.
errors=remount-ro means if any disk errors are detected the file system will be mounted as read-only
0 or the fifth parameter is called the fs_freq and is used to determine which files need to be dumpted. 0 signified no dump required.
1 or the sixth paramter is called the fs_passno and is used to determin the in file system checkers are carried out at boot time.
Here’s what the fstab file should look like after editing. I plan to do a deeper dive into fstab in a later post so stay tuned for that.
Now reboot your server and your new disk should still be present.
That’s it. Another scary Linux job done that isn’t that scary when you understand it.