Home Partitioning and Mounting a New Drive
Post
Cancel

Partitioning and Mounting a New Drive

In a virtual Linux environment, adding a new drive in addition to the OS drive has many benefits. Isolating your OS from your data is good practice.

Locating your new raw drive

1
2
3
4
5
6
7
8
9
10
$ lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0    7:0    0   62M  1 loop /snap/core20/1611
loop1    7:1    0 67.8M  1 loop /snap/lxd/22753
loop2    7:2    0   47M  1 loop /snap/snapd/16292
sda      8:0    0   64G  0 disk
├─sda1   8:1    0    1M  0 part
└─sda2   8:2    0   64G  0 part /
sdb      8:16   0  640G  0 disk
sr0     11:0    1 1024M  0 rom

We see our new drive here as “sdb” or more fully as “/dev/sdb”.

Partitioning your new drive

Now we need to partition the new drive.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ sudo gdisk /dev/sdb
Command (? for help): n
Partition number (1-128, default 1):
First sector (34-1342177246, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-1342177246, default = 1342177246) or {+-}size{KMGTP}:
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

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/sdb.
The operation has completed successfully.

If we do lsblk again we see this.

1
2
3
4
5
6
7
8
9
10
11
$ lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0    7:0    0   62M  1 loop /snap/core20/1611
loop1    7:1    0 67.8M  1 loop /snap/lxd/22753
loop2    7:2    0   47M  1 loop /snap/snapd/16292
sda      8:0    0   64G  0 disk
├─sda1   8:1    0    1M  0 part
└─sda2   8:2    0   64G  0 part /
sdb      8:16   0  640G  0 disk
└─sdb1   8:17   0  640G  0 part /mnt/data
sr0     11:0    1 1024M  0 rom

Our new partition is /dev/sdb1.

Formatting your drive

Now we format the drive.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ sudo mkfs.ext4 /dev/sdb1
mke2fs 1.45.5 (07-Jan-2020)
Discarding device blocks: done
Creating filesystem with 167771899 4k blocks and 41943040 inodes
Filesystem UUID: f7ed7176-072c-4bdd-aede-25e4a4371279
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
        102400000

Allocating group tables: done
Writing inode tables: done
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done

You can mount the drive from the device name (/dev/sdb1) or via the UUID which is specific to this drive. The latter is good form.

Getting the UUID

1
2
3
4
5
6
7
$ sudo blkid
/dev/sda2: UUID="0e24b04f-babc-4526-ac9c-1bf52f0be440" TYPE="ext4" PARTUUID="e3ee9082-6d90-45cc-89e7-9711f5303666"
/dev/loop0: TYPE="squashfs"
/dev/loop1: TYPE="squashfs"
/dev/loop2: TYPE="squashfs"
/dev/sda1: PARTUUID="fc39aa8f-9379-4008-bedf-20ea66a3f10f"
/dev/sdb1: UUID="f7ed7176-072c-4bdd-aede-25e4a4371279" TYPE="ext4" PARTLABEL="Linux filesystem" PARTUUID="751f07f5-22cf-4a6a-ba70-b582b41be77d"

The last line is the one we need. Take note of the UUID string.

Create the mount point

Now create the mount point directory.

1
sudo mkdir /mnt/data

Editing the /etc/fstab file

Now edit the /etc/fstab file and add this line using your UUID.

1
UUID=f7ed7176-072c-4bdd-aede-25e4a4371279 /mnt/data ext4 defaults,errors=remount-ro,noatime 0 1

Mounting the drive

Now mount all entries in the fstab file

1
sudo mount -a

You may need to change the permissions on the /mnt/data folder to suit your needs..

This post is licensed under CC BY 4.0 by the author.