USB drives
(This is part of a larger series on finding your footing on Arch Linux.)
Goal: Read from and write to external USB drives, including those with the NTFS file system.
References:
- Wiki: USB storage devices
- Wiki: NTFS-3G
- https://wiki.archlinux.org/title/Udisks
- The
man
pages forlsblk
,mount
,umount
,udisksctl
, andudisks
Requirements (depending on the file systems you want to interact with):
dosfstools
mtools
ntfs-3g
These packages allow read/write interaction with the file systems commonly used on USB media.
Manual everything
Well there’s a decent amount going on here. You should roughly grok what a block device is, and what partitions are. You should know what a file system is. You have to mount the USB drive’s filesystem to your computer, so the USB drive’s files are accessible on your system.
-
For future reference, run
lsblk
and remember the output without a USDB connected -
Physically connect USB drive to computer via a USB port
- Run
lsblk
and identify the drive’s block device and data partition:$ lsblk
It takes a little experience to interpret the output. The USB drive’s block device might be
sdb
. Identify the data partition based on the known disk size—the data partition should take up the majority of the full disk size. - Create a mount directory inside
/mnt
to hold the drive’s files. You can name it whatever you want; perhaps use the manufacturer of the drive:sudo mkdir /mnt/seagate
You only need to do this step once.
- Mount the drive’s data partition to the mount directory
sudo mount /dev/sdb2 /mount/seagate
Optionally use
lsblk
to check that the USB drive is mounted to the just-created mount directory. -
Interact with the files on the USB drive from the
/mnt/seagate
directory, reading and writing as needed. You’ll need root privileges to write. - To eject the drive
umount /mnt/seagate # by specifying mount point (preferred) umount /dev/sdb2 # by specifying device # Power off echo 1 > /sys/block/DISK_NAME/device/delete
From
man umount
, specifying the mount directory is preferred, in case the physical device is mounted to multiple directories.For the power off line see https://unix.stackexchange.com/a/43450 and ArchWiki: USB storage/Device not shutting down after unmounting all partitions
Using udisksctl
Ejecting with udisks2
This package allows you to disconnect power from an e.g. USB drive, which makes for safer ejecting.
Reference:
-
man udisksctl
andman udisks
-
Install the
udisks2
package -
Mount drive with
umount
as above. - Eject with
udisksctl unmount -b /dev/sdb2
where the
-b
flag specifies that/dev/sdb2
refers to a block device. Note that a device partition, e.g.sdb2
, is used. Only the device itself, e.g.sdb
, is used below withpower-off
. - Power off with
udisksctl power-off -b /dev/sdb
Again, note that
udisksctl unmount
uses the device’s data partition (e.g./dev/sdb2
) whileudisksctl power-off
uses the device itself (e.g./dev/sdb
).
File systems
The mount
command should detect the USB drive’s file system and use the appropriate library if needed.
See e.g. https://wiki.archlinux.org/title/NTFS-3G: the mount
command should know to use the ntfs
file system after an installation of ntfs-3g
.