Add Mounted Disk to Ubuntu 20.04 or 22.04

Finding the new Disk

Once the new physical or virtual hard drive has been installed on the system and is visible to the operating system. Once added, the new drive should automatically be detected by the operating system. Typically, the disk drives in a system are assigned device names beginning hd, sd or xv followed by a letter to indicate the device number depending on the type of disk. For example, the first device might be /dev/xvda, the second /dev/xvdb and so on.

The following is output from a typical system with only one virtual disk drive connected:

sudo ls /dev/xvd*

/dev/xvda  /dev/xvda1  /dev/xvda2

This shows that the disk drive represented by /dev/xvda is itself divided into 2 partitions, represented by /dev/xvda1 and /dev/xvda2.

The following output is from the same system after a second hard disk drive has been installed:

sudo ls /dev/xvd*

/dev/xvda /dev/xvda1 /dev/xvda2 /dev/xvdb

As shown above, the new hard drive has been assigned to the device file /dev/xvdb. Currently the drive has no partitions shown (because we have yet to create any).

At this point we have a choice of creating partitions and file systems on the new drive and mounting them for access.

Creating Linux Partitions

The next step is to create one or more Linux partitions on the new disk drive. This is achieved using the fdisk utility which takes as a command-line argument the device to be partitioned:

sudo fdisk /dev/xvdb

Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xbea8e529.

Command (m for help):

In order to view the current partitions on the disk enter the p command:

Command (m for help): p
Disk /dev/xvdb: 75 GiB, 80530636800 bytes, 157286400 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xbea8e529

As we can see from the above fdisk output, the disk currently has no partitions because it is a previously unused disk. The next step is to create a new partition on the disk, a task which is performed by entering n (for new partition) and p (for primary partition):

Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-157286399, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-157286399, default 157286399):
Created a new partition 1 of type 'Linux' and of size 75 GiB.

Now that we have specified the partition, we need to write it to the disk using the w command:

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

If we now look at the devices again we will see that the new partition is visible as /dev/xvdb1:

sudo ls /dev/xvd*
/dev/xvda /dev/xvda1 /dev/xvda2 /dev/xvdb /dev/xvdb1

Creating a File System on a Disk Partition

We now have a new disk installed, it is visible to Ubuntu and we have configured a Linux partition on the disk. The next step is to create a Linux file system on the partition so that the operating system can use it to store files and data. The easiest way to create a file system on a partition is to use the mkfs.xfs utility:

sudo mkfs.xfs /dev/xvdb1
meta-data=/dev/xvdc1 isize=512 agcount=4, agsize=4915136 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=19660544, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=9599, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0

 Mounting a File System

Now that we have created a new file system on the Linux partition of our new disk drive we need to mount it so that it is accessible and usable. In order to do this we need to create a mount point. A mount point is simply a directory or folder into which the file system will be mounted. For the purposes of this example we will create a /backup directory to match our file system label (although it is not necessary that these values match):

sudo mkdir /backup

The file system may then be manually mounted using the mount command:

sudo mount /dev/xvdb1 /backup

Configuring Ubuntu to Automatically Mount a File System

In order to set up the system so that the new file system is automatically mounted at boot time an entry needs to be added to the /etc/fstab file. The format for an fstab entry is as follows:

<device>	<dir>	<type>	<options>	<dump>	<fsck>

These entries can be summarized as follows:<device> – The device on which the filesystem is to be mounted.

<dir> – The directory that is to act as the mount point for the filesystem.

<type> – The filesystem type (xfs, ext4 etc.)

<options> – Additional filesystem mount options, for example making the filesystem read-only or controlling whether the filesystem can be mounted by any user. Run man mount to review a full list of options. Setting this value to defaults will use the default settings for the filesystem (rw, suid, dev, exec, auto, nouser, async).

<dump> – Dictates whether the content of the filesystem is to be included in any backups performed by the dump utility. This setting is rarely used and can be disabled with a 0 value.

<fsck> – Whether the filesystem is checked by fsck after a system crash and the order in which filesystems are to be checked. For journaled filesystems such as XFS this should be set to 0 to indicate that the check is not required

The following example shows an fstab file configured to automount our /backup partition on the /dev/xvdb1 partition:

/dev/xvdb1      /backup     xfs      defaults

The /backup filesystem will now automount each time the system restarts.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.