Logo

Armand.nz

Home / About / Linkedin / Github

Format and Mount USB Drive on Ubuntu

#linux #nfs #mount |

This is a brief overview of setting up a USB drive on Ubuntu

My intention of doing so is to then use it as a NFS Share, which this article outlines: [[NFS Share on Debian]]

These instructions are based on my project: Ubuntu 22.04 Linux host with a USB 3.0 Hard Drive (Passport from WD)

Identifying the USB Drive

  1. Use lsblk command to identify the USB drive. In my example, I have found it as sdb1
armand@beelink:~$ lsblk
NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda                         8:0    0 119.2G  0 disk
├─sda1                      8:1    0     1G  0 part /boot/efi
├─sda2                      8:2    0     2G  0 part /boot
└─sda3                      8:3    0 116.2G  0 part
  └─ubuntu--vg-ubuntu--lv 253:0    0  58.1G  0 lvm  /var/lib/kubelet/pods/blah2..
                                                    /
sdb                         8:16   0   1.8T  0 disk
└─sdb1                      8:17   0   1.8T  0 part #<-- THERE IT IS!

Formatting the USB Drive

I will format the drive to ext4. Ext4, the Extended Filesystem, is a file system built specifically for Linux and will do fine for this job. Here is an article that provides a Introduction to the Linux File System

  1. Assume the USB stick is on /dev/sdb1, first make sure /dev/sdb1 is unmounted:
# unmount 
umount /dev/sdb1

# Example output:
umount: /dev/sdb1: not mounted.
  1. Quickly wipe the drive, removing all disk label and file systems signatures
sudo wipefs -a /dev/sdb1
  1. Now format a disk partition with the ext4 file system using the following command:
sudo mkfs -t ext4 /dev/sdb1
  1. Lastly, verify the file system change using the lsblk command once more:
lsblk -f


# Example output


NAME                      FSTYPE      FSVER    LABEL UUID                                   FSAVAIL FSUSE% MOUNTPOINTS
sda
├─sda1                    vfat        FAT32          2E73-113F                                   1G     1% /boot/efi
├─sda2                    ext4        1.0            ec472509-8216-4c0d-baa3-5516710b0dd9      1.7G     7% /boot
└─sda3                    LVM2_member LVM2 001       UACmtW-N9Pl-xDoF-BJzS-bRat-NbKB-ALEAh1
  └─ubuntu--vg-ubuntu--lv ext4        1.0            1b8bc555-8ce3-4456-83a1-489c7fac8c44     40.7G    23% /var/lib/kubelet/pods/992425db-3600-40a2-86f7-33311ef9738e/volume-subpaths/hubble-ui-nginx-conf/frontend/0
                                                                                                           /
sdb
└─sdb1                    ext4        1.0            c56478ca-fb3f-47dc-bff4-4a9ff4e917ed # <-- HERE IT IS!

Mount the USB Drive

  1. Create mount point
sudo mkdir /media/storage
  1. Mount the partition by using the following command:
sudo mount -t auto /dev/sdb1 /media/storage
  1. Get the PARTUUID (Note: This is unique on each host!)
sudo blkid

# Example output
/dev/sda1: LABEL="mystorage" UUID="[device uuid]" TYPE="ext4" PARTUUID="[partition uuid]"

In my example, the PARTUUID="c8109b4c-f3c1-4dd1-b58c-07761e7e6759"" looks like:

/dev/sdb1: UUID="5c8c20d8-f758-417f-a7e0-a677fa950b3c" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="c8109b4c-f3c1-4dd1-b58c-07761e7e6759""
  1. Mount the drive at boot by adding a config line in your /etc/fstab file by adding the line my external device file system is ext4). Remember that the UUID is unique on each node, and does not apply on different hosts

For each host, add the line into /etc/fstab:

PARTUUID=[partition uuid] /media/storage ext4 defaults,noatime,nodiratime 0 2

So, in my example, on this host, it is:

echo "PARTUUID=c8109b4c-f3c1-4dd1-b58c-07761e7e6759 /media/storage ext4 defaults,noatime,nodiratime 0 2" | sudo tee -a /etc/fstab > /dev/null
  1. Test if it mounted correctly with the mount -a command. There should be no errors:
sudo mount -a
comments powered byDisqus

Copyright © Armand