How to Partition, Format, and Mount a Disk on Ubuntu 20.04

This blog post describes how to partition and format a disk on Ubuntu 20.04. First, I explain how to quickly partition new hard drives using gdisk, then I show you how to format a hard drive using the ext4 file system, and finally how to auto mount hard drives in Linux. Additionally, I also show how to fuse together different hard drives, to make it them appear as one large hard drive which can be used e.g. for easily extendable cloud storage.

Prerequisites

  • A running Linux machine e.g. running Ubuntu 20.04
  • A new (or used) hard drive which you have recently connected to your machine

1. Remove old partitions from disk

This step is optional if you are using new drives, but if you have previously used this hard drive in say a Windows machine, you first will need to remove the old partitions on the drive. For this, get the disk identifier (e.g. /dev/sda) using

sudo fdisk -l

and open gdisk

sudo gdisk /dev/sda
GPT fdisk (gdisk) version 1.0.5

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

and delete all existing partitions

Command (? for help): d
Partition number (1-2): 1

Command (? for help): d
Using 2

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!!

Do you want to proceed? (Y/N): Y

Note that this will make any data on your drive inaccessible! Absolutely make sure to have a backup before proceeding

2. Create a GPT partition table

Next, create a new GPT partition table by typing

sudo gdisk /dev/sda
GPT fdisk (gdisk) version 1.0.5

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

create a new partition by typing n followed by the partition number 1

Command (? for help): n
Partition number (1-128, default 1): 1

and agree to all further prompts

First sector (34-11721045134, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-11721045134, default = 11721045134) or {+-}size{KMGTP}:
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300):

finally, type “w” to write the changes to the drive and confirm by typing “Y”

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!!

Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/sda.
The operation has completed successfully.

3. Format disk

Next, you need to format the previously created partition

sudo mkfs.ext4 /dev/sda1

Make sure NOT to format the entire drive /dev/sda instead of the partition /dev/sda1

4. Mount disks on Ubuntu

Get the UUID for the drive that you want to mount

sudo blkid
/dev/sda1: LABEL="cloud1" UUID="d19baf53-02e5-4f65-9dc0-7416a5ae9e24"
  TYPE="ext4" PARTLABEL="primary" PARTUUID="d37fecad-5236-4a52-be97-c11f8abeb8dd"

and open the fstab file to specify where you want your drives to be mounted upon boot. I use the editor nano for this purpose

sudo nano /etc/fstab

Add a new line in this file containing the following information:

  • UUID: The device UUID of the drive which should be mounted
  • mount-point: The directory where the contents of the drive can be accessed from
  • fs-type: The type of the file system
  • options: Various mounting options, we use defaults which should be fine in most cases. Read more in what mount options can be used here.
  • dump: Number telling the system how often the drive should be backed up
  • pass: Number indicated in which order (and if) fsck should check your device at boot
    0 to avoid checking
    1 if this is the root file system
    2 if this is any other device

Below you can see my fstab file for three cloud storage points

# Cloud storage
UUID=d19baf53-02e5-4f65-9dc0-7416a5ae9e24 /mnt/cloud1 ext4 defaults 0 0
UUID=4e757d1d-0bb9-43b8-8ee7-95cb55501f0b /mnt/cloud2 ext4 defaults 0 0
UUID=944709c6-7eaa-4996-b839-a99dd6aaee74 /mnt/cloud3 ext4 defaults 0 0

I use the mount points /mnt/cloud1 through /mnt/cloud3 to mount three drives but your location can obviously be any location that you like!

Next, create the mount points

sudo mkdir /mnt/cloud1
sudo mkdir /mnt/cloud2
sudo mkdir /mnt/cloud3

and mount the drives

sudo mount -a

And with that you should now be able to successfully access the content from the /dev/sda1 device under /mnt/cloud1.

5. Fuse hard drives using mhddfs

In this last part of this tutorial, I am going to show you how you can fuse together multiple drives allowing you to access content which is spread over multiple drives from one mount point. Note that this step is optional!

For this, we first need to install mhddfs

sudo apt update
sudo apt install mhddfs

Then, open fstab again and add a new line according to

sudo nano /etc/fstab

# Bind drives
mhddfs#/mnt/cloud1,/mnt/cloud2,/mnt/cloud3 /mnt/cloud fuse allow_other,nonempty 0 0

Instead of a device ID we simply specify that we want to use mhddfs to fuse together our previously specified mount points into a new mount point called /mnt/cloud. Fuse is used instead of a file system and allow_other is used to allow any user to read and write on the fused location. nonempty makes sure that you do not fuse into a mount point which already contains data.


Finally, create the new fused mount point and fuse the drives

sudo mkdir /mnt/cloud
sudo mount -a

mhddfs: directory 'mnt/cloud1' added to list
mhddfs: directory 'mnt/cloud2' added to list
mhddfs: directory 'mnt/cloud3' added to list
mhddfs: mount to: /mnt/cloud
mhddfs: move size limit 4294967296 bytes

And that’s it! With these steps you should be able to first partition, then format, and finally mount a new disk on Ubuntu 20.04

10 thoughts on “How to Partition, Format, and Mount a Disk on Ubuntu 20.04”

  1. Thanks, Roman !

    This was exactly what I needed, worked like a charm. This disk came with a Windows NTFS partition which makes it unusable for snapshots, for example. As others stated above, if you want to use a disk with Linux it’s best to format it for Linux.

    Chuck

    Reply
  2. Or I can attach the drive to a Windows Pro operating system, and the whole shebang is done for me without any of these nightmare errors in a few VERY CLEAR steps. Then mount the drive on Linux (Ubuntu) without any issues whatsoever. That entire list of instructions is absurd.

    YES, I’m a Linux Newbie… But I’m not wrong.

    Reply
    • Without specifying which nightmare errors you saw, it’s hard to say how to improve the instructions. This guide does assume one has experience in installing the used tools in Linux. Only needed if they are not found in the Ubuntu flavour one uses. I used this guide to add a new SATA SSD in my Ubuntu server box without having to first install the drive to my Win 10 Pro machine.

      It is true that Linux requires time and patience when one attempts something new for the first time. Also I find it mandatory to document the steps for later reference.

      Reply
    • Gregg — the above are ALL very clear steps. This guide makes VERY clear from the outset that it is explaining how to use an Ubuntu command line to carry out various disk manipulations. Perhaps a little gratitude would be in order instead of slagging off someone’s help. If this guide isn’t giving you the help you need why not either write your own or go find one that does the job you need?

      Gregg I suggest you do your research. You will find that manipulating drives under Windows OS for use with Linux often creates problems down the line. Having had negative experiences (e.g., iSCSI mounts not working correctly) when using old Windows OS disks I now only work with disks partitioned and formatted with Linux (for Linux).
      (Oh, yes, I’m not in anyway an experienced Linux bod – and so appreciate these kind of guides that keep me on the straight and narrow)

      Good luck!

      Reply
  3. First, this is a good write up so thank you.

    I’m not very savvy in the server admin area but I can get some things done normally. I’m following this tutorial to add a single external drive for better disk I/O performance. Where I am getting stuck is at the end of step 4 `sudo mount -a`. The result of that command for me is `mount: /mnt/games: mount(2) system call failed: Structure needs cleaning.` I’ve tried using `sudo fsck.ext4 -y /dev/sda1` but that hasn’t yielded any progress.

    Reply
  4. I have 3 disks mounted in mnt/cloud directory (cloud1, cloud2, cloud3), only for nexcloud data.
    how can i replace one of the full hard drives with a bigger one ? can you help or make a tutorial.
    I want replace cloud1 hdd for one bigger.

    Thanks for all your videos and tutorials

    Reply
  5. Hi Roman, I have 3 disk. I have mount two disk /mnt/cloud1 and /mnt/cloud2. OS disk is mounted as /. I’m testing docker, I want persistent volume to be on /mnt/cloud.
    I test using dd file create on /mnt/cloud folder. On cockpit I’m seeing storage used under ‘/’ mount. What I’m doing wrong?
    I was done ZFS, I have only 3 X200GB drives, so I’m going without zfs. Any advise. Thanks in advance.

    Reply
  6. I’m new to linux, by about 3 weeks now. I just tried following the directions. When it was completed, the drive was not able to be written to. Everything was grayed out, in the permissions. It said the owner of the drive is root.
    Can I add the, “allow_other”, in the fstab, to give others access for read and write? If not, how would I go with getting the drive functional so I can start moving files to it?
    Thank you

    Reply
    • Hi! Are you fusing different drives together using mhddfs or do you simply want to access a single drive? I assume that only yourself needs access to the drive? You can easily change ownership of the drive using ‘sudo chown -R username:username /mnt/

      Reply

Leave a Comment

Dear reader

Ads allow me to dedicate a significant amount of time into the creation of valuable content both on YouTube as well as on this website.

Please support my work by disabling your ad blocker or whitelisting this site!

Alternatively, you can remove all ads on this site by becoming a supporter for as little as 2$/month. Thereby, you will directly support my content on YouTube and on this blog!