I recently added a new hard-drive to one of my machines running Ubuntu. Unlike most other OS’s which pick up drives by default as part of their startup routine, Linux requires you to configure these drives. This is typically done during an OS install, but it’s often that you’ll need to retroactively adjust this configuration when there are changes to your hardware set up.
The first step is to see that the drive is actually being picked up by Ubuntu which can be done
with the lsblk
(list block devices) command.
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 465.3G 0 disk
├─sda1 8:1 0 512M 0 part /boot/efi
└─sda2 8:2 0 464.8G 0 part /
sdb 8:16 0 1.4T 0 disk
sr0 11:0 1 1024M 0 rom
In my case, you can see that sdb
exists as a disk, but there are no partitions configured or
mount points so that it is accessible via the file system.
To inspect disks further, you can use fdisk
(fixed disks command / format disk command depending
on who you talk to).
# fdisk -l
Disk /dev/sda: 465.3 GiB, 499558383616 bytes, 975699968 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: gpt
Disk identifier: EBD193D9-795C-4F4C-B7C0-06CDA7B4AFBD
Device Start End Sectors Size Type
/dev/sda1 2048 1050623 1048576 512M EFI System
/dev/sda2 1050624 975697919 974647296 464.8G Linux filesystem
Disk /dev/sdb: 1.4 TiB, 1498675150848 bytes, 2927099904 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
Again, sdb
shows up with no partitions. To proceed from here, it’s just a case of starting
fdisk
passing in the drive which needs to be changed. fdisk
then prompts for further
information on what partitions are required and creates the tables on the drive as needed.
# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.31.1).
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 0xe78d1889.
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-2927099903, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-2927099903, default 2927099903):
Created a new partition 1 of type 'Linux' and of size 1.4 TiB.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
Running lsblk
again, the sdb
disk now contains a partition named sdb1
.
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 465.3G 0 disk
├─sda1 8:1 0 512M 0 part /boot/efi
└─sda2 8:2 0 464.8G 0 part /
sdb 8:16 0 1.4T 0 disk
└─sdb1 8:17 0 1.4T 0 part
sr0 11:0 1 1024M 0 rom
The new partition is 1.4TB in size, but prior to setting up the mount point, that partition needs to be actually formatted to a proper file system type.
To see the file system type, lsblk
can be used with the -o
switch. The following command also
requests the disk name and disk partition UUID because they are required later.
# lsblk -o NAME,FSTYPE,UUID
NAME FSTYPE UUID
sda
├─sda1 vfat 1D0D-9095
└─sda2 ext4 18698c14-3637-40b4-8ad5-245538a2bdf3
sdb
└─sdb1
sr0
Using -o NAME,FSTYPE,UUID
the lsblk
command outputs the file system type and the UUID. The
UUID comes up later so make a note of it if required.
To format the partition the mkfs
(make filesystem) command can be used. In my case, I formatted
sdb1
to ext4
which is a good choice in a Ubuntu set up such as this and it likely the best
choice in most situations.
# mkfs -t ext4 /dev/sdb1
mke2fs 1.44.1 (24-Mar-2018)
Creating filesystem with 365887232 4k blocks and 91471872 inodes
Filesystem UUID: 89c08654-b0eb-4bcf-98ab-0630f8346b13
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000, 214990848
Allocating group tables: done
Writing inode tables: done
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done
Running the same lsblk
as before, sdb1
is now listed with ext4
as the file system type and a
UUID
has been allocated to the partition.
# lsblk -o NAME,FSTYPE,UUID
NAME FSTYPE UUID
sda
├─sda1 vfat 1D0D-9095
└─sda2 ext4 18698c14-3637-40b4-8ad5-245538a2bdf3
sdb
└─sdb1 ext4 89c08654-b0eb-4bcf-98ab-0630f8346b13
sr0
At this point you can mount the disk using the mount
command but this only persists up until the
machine is cycled. To make the mount permanent, the /etc/fstab
file needs to be edited in your
editor of choice.
Once in the file, it’s simply a case of adding a new entry using the UUID for the partition that
was just created and choosing a mount point. In my case, I added this drive at /data1
.
UUID=cce765b6-fe92-4d2a-a264-a887af6d95b3 / ext4 errors=remount-ro 0 1
UUID=89c08654-b0eb-4bcf-98ab-0630f8346b13 /data1 ext4 errors=remount-ro 0 2
The last two fields are dump
(controls how the dump command handles the disk which is not used in
most cases), and the last is pass
which determines the order in which disks are checked at
startup. The root drive should always be 1, with any other mounts being 2 or above in priority.
At this point, it’s just a case of cycling the machine.
# shutdown -r now
After the machine comes back online, you should be able to list the files in the new directory.
In my case, the familiar ext
file system lost+found
directory sits at the root of the partition
mount position which indicates everything is all set up correctly.
$ ls -la /data1
total 24
drwxr-xr-x 3 root root 4096 Dec 21 21:56 .
drwxr-xr-x 24 root root 4096 Dec 21 22:16 ..
drwx------ 2 root root 16384 Dec 21 21:56 lost+found
The /data1
directory can now be used with all of it’s data being stored on the new drive.