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

Channel: Linux & Server Guides Linux
Abstract: 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 d

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

Ref From: techguides

Related articles