Mounting USB Drives Under Debian

I’m currently designing a backup system built around Resitc and removable USB drives. Before I can really get started though I need to properly understand how to manage the drives, in particular mounting and unmounting them. What I’m hoping I’ll be able to achieve is a situation where I can mount a drive somewhere (let’s say /mnt/backup), then replace the drive with another once it’s time to swap drives (e.g. once a week) and the backup just continues to work smoothly.

What I’ve learnt from experience is that if there’s any friction or pain points in the backup process I just won’t do it. For example, I tried to set up something like this under Windows. I bought three 1TB USB drives and tried to cycle through them. Getting Windows to mount them to the same drive letter when they were replaced turned out to be a significant challenge though so the backups stopped being taken. Yep, that’s unbelievably lazy but at least I’m honest enough with myself to know the level I need to achieve. I suspect I’ll end up with a few scripts controlling the backup process but I think that will be acceptable with proper documentation on how to run things.

Creating a Mount Point

The first thing to decide is where to mount the drive. Many Linux distributions follow the Filesystem Hierarchy Standard which provides two possible candidate locations /mnt and /media. There’s a lot of confusion over which of these should be used and when (especially for drvices that are almost permanently mounted) but I think in this case it’s fairly clear I should be mounting them under /media.

Open a command prompt and create the directory /media/backup.

~$ cd /media
/media$ sudo mkdir backup

Find the Drive

Plug the drive into the system and then use lsblk -f to list filesystem information.

$ lsblk -f
NAME        FSTYPE FSVER LABEL                   UUID                                 FSAVAIL FSUSE% MOUNTPOINTS
└─sda2      ntfs         Games                   9EFCCEA7FCCE78D3                                    
└─sdb2      ntfs         Seagate Expansion Drive 1CD2FA89D2FA6704                                    
└─sdd1      ntfs         backup-3                226AFB1D6AFAED03                                    
├─nvme1n1p1 ntfs         Recovery                4E62220B6221F87B                                    
├─nvme1n1p2 vfat   FAT32                         DC22-63C3                                           
└─nvme1n1p4 ntfs                                 6286234786231AD5                                    
├─nvme0n1p1 vfat   FAT32                         F059-0450                             505.2M     1% /boot/efi
├─nvme0n1p2 ext4   1.0                           b28e9977-f9b7-4b35-91db-fa799f17f4f4  788.4G     9% /
└─nvme0n1p3 swap   1                             e11379bb-607b-45a6-8d30-454cefe67d59                [SWAP]

The -f flag tells the utility to output information about the filesystems it finds such as the type and name, this can be very useful when identifying which disk you want to use. Looking at the output above it’s pretty obvious I want to mount backup-3 which is formatted as ntfs – this is one of the drives I was using for the failed windows backup I mentioned above, we’ll reformat it later. The really important information shown here, though, is the UUID. This is a unique name for the partition that shouldn’t ever change so you can be sure you are always working with the correct device. It also means you can use this name in /etc/fstab to refer to exactly the partition you want. Note, you can also get UUID information from the command ls -l /dev/disk/by-uuid but its output isn’t as user friendly.

Format the Disk

This step is optional and depends on the current state of the disk. Most disks come formatted either vfat or ntfs which is fine if you are working on Windows but leaves a bit to be desired if you are working under Linux. I’ll be reformatting my backup drives to ext4 for easier use under Linux. Linux has had excellent support for vfat and ntfs for years now but ext4 is still a better choice. Note that it’s possible to get an ext4 drive working under Windows but it’s not trivial, by default Windows won’t recognize the drive.

There are numerous ways to reformat and repartition a drive under Linux. The two most popular command line tools are fdisk and parted. Both will get the job done and are mature and robust. They are, however, quite complex tools to use. For simplicities sake I use KDE Partition Manager because it’s already installed on my system and it’s competent at what it does (I think the Gnome equivalent is called Disks, there’s also GParted).

Manually Mount the Disk

Eventually we’ll add an entry into /etc/fstab but I like to manually mount a device while I’m working on a new project. This helps to avoid “oops, my computer won’t boot” situations. To mount a device you need to issue a mount command. If you’re using modern desktop environment many of them will offer to mount devices though the interface, for example in the tray area of my Plasma desktop I have a widget called Disks and Devices. Since I’m preparing to use this on a server I’ll mount it manually at the command line.

Start by confirming your drive information with lsblk -f. I’ve only shown the relevant portion of the output below. Notice that the UUID has changed, this is because I’ve repartitioned the disk when converting it to ext4, the UUID is associated with the partition.

└─sdd1      ext4   1.0   backup-3                11bcd17d-06b0-4345-8b5f-cc0fcbf88f41 

The mount command to use looks like the one below, obviously change the UUID to match your partition.

$ sudo mount -t ext4 /dev/disk/by-uuid/11bcd17d-06b0-4345-8b5f-cc0fcbf88f41 /media/backup/

If it works, unmount the drive and we’ll make an entry in /etc/fstab.

Edit fstab

Entries in /etc/fstab are essentially just provide the arguments to the mount command. As we’re looking to produce a system that’s reliable over the long term we’ll care a bit more about the options we provide than when we manually mounted and left it up to the operating system. You should start by reading about the fstab file format.

An example entry for the disk we want to mount is shown below.

UUID=11bcd17d-06b0-4345-8b5f-cc0fcbf88f41 /media/backup ext4 defaults 0 0

This is super simple because we’re using ext4 which integrates well with the rest of the system. The first part specifies the UUID of the partition I want to mount. Then I tell it where to mount it and what type of partition it is – the system could guess but we know so tell it explicitly. The options can just be left at their defaults and then dump and pass are both left at 0. The dump number is a marker to indicate a filesystem should be dumped to tape for backup, I’m pretty sure it’s not used any more. The pass number can be 0,1 or 2 and determines the order when an filesystem check is run. To mount the device now you just need to use

sudo mount -a

There’s a Problem!

If you unmount the drive, unplug it and then run the mount -a command again you’ll get an error message like this.

mount: /media/backup: can't find UUID=11bcd17d-06b0-4345-8b5f-cc0fcbf88f41.

On the command line this isn’t an issue but when you’re booting your machine it is a problem because the system will hang waiting for the drive to appear. To get around this issue you can add the nofail option which will suppress warning messages. There’s a lot of debate around what the nofail option will do. I think I’m getting away with booting cleanly because I also have pass set to zero so there’s no attempt to do a filesystem check.

UUID=11bcd17d-06b0-4345-8b5f-cc0fcbf88f41 /media/backup ext4 defaults,nofail 0 0

Additional Reading