CodeTheRobot

Ubuntu Linux for Robotics · Lesson 16 of 96

Mounting USB devices

Finding the device, mounting and unmounting safely, and permanent mounts in fstab.

Estimated time · 30–45 min

Learning goals

What you will understand

  • Explain mounting usb devices in your own words
  • Use it in a small workstation example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

A USB drive appears as a block device (for example /dev/sdb1). The desktop mounts it automatically under /media/user/NAME, but on a robot without a desktop you mount it yourself: sudo mount /dev/sdb1 /mnt, and always unmount it with sudo umount /mnt before pulling it out so cached data is written.

For a drive that must be mounted at every boot add a line to /etc/fstab, identified by UUID (from lsblk -f or blkid) so it does not depend on device order. Add nofail so a missing drive does not stop the boot, and test with sudo mount -a before rebooting.

Guided lesson

Work through these steps

  1. 1.

    Open a terminal and record the current state before changing anything.

  2. 2.

    Read the mounting usb devices commands and predict what each one does.

  3. 3.

    Run the commands one at a time and compare each output with your prediction.

  4. 4.

    Write the commands that worked in a setup note you can repeat later.

Worked example

Read it before you run it

lsblk -f
# Plug in the drive and find its device name and UUID.
sudo mkdir -p /mnt/usb
# Create an empty folder to mount onto.
sudo mount /dev/sdb1 /mnt/usb
# Attach the drive's filesystem to that folder.
ls /mnt/usb
# Read its files.
sudo umount /mnt/usb
# Detach it safely before unplugging.
echo 'UUID=1234-ABCD /mnt/usb vfat defaults,nofail 0 2' | sudo tee -a /etc/fstab
# Mount it at every boot; nofail avoids boot problems if it is missing.
sudo mount -a
# Test the fstab entry now; errors here would break a reboot.

What the example is doing

  1. 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
  2. 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
  3. 3.Comments beginning with # explain safety checks and are not executed by the shell.

Watch it run · The robot's computer

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

robot computercpumemdiskdriverstoppedplannerstoppedloggerstoppedsafetystoppedTHE ROBOT'S COMPUTER

Demo for Mounting USB devices. Expected result: The drive contents are listed and the drive is then unmounted

Practice code

Now type it yourself

Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.

sudo mkdir -p /mnt/usb
# TODO: mount /dev/sdb1 on /mnt/usb, list its files, then unmount it

Expected output

The drive contents are listed and the drive is then unmounted
Stuck? Show the full solution (try it yourself first)
sudo mkdir -p /mnt/usb
sudo mount /dev/sdb1 /mnt/usb
ls /mnt/usb
sudo umount /mnt/usb

Expected result

How you know it worked

  • Each command completes without an unexplained error.
  • Your notes contain the command, the important output, and what it proves.

Practical exercise

Apply the lesson

Mount a USB drive by hand, copy a file to it, unmount it, then add an fstab entry with nofail and prove it works with mount -a. Explain why you use a UUID rather than /dev/sdb1.

Before you continue

  • You recorded the starting state
  • Every command and output is understood
  • The change can be repeated from your notes

If something goes wrong

A command says permission denied.

Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.

A command or package is not found.

Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.