Chapter 5: Drives and Filesystems
This chapter focuses on managing drives and filesystems in Linux, including mounting and unmounting drives, understanding filesystems, and using tools like fdisk
, mkfs
, and fsck
.
Understanding Drives and Filesystems
In Linux, drives and filesystems are fundamental components that allow the operating system to store and retrieve data. Each drive can have one or more partitions, and each partition can be formatted with a specific filesystem type.
Common Filesystem Types
- ext4: The most common filesystem for Linux, known for its performance and reliability.
- xfs: A high-performance filesystem often used for large files and high-capacity storage.
- btrfs: A modern filesystem with advanced features like snapshots and dynamic resizing.
- vfat: A filesystem compatible with Windows, often used for USB drives, external storage, and EFI boot partitions.
- ntfs: The Windows NT filesystem, used for compatibility with Windows systems.
- swap: A special filesystem used for swap space, which is used when the system runs out of RAM.
Personally, I use ext4
for most of my drives, xfs
for large data storage, and vfat
for USB drives and EFI partitions. I avoid btrfs
due to its complexity and potential issues with data integrity, although it has improved significantly in recent years. If you are interested in using btrfs
check out this article and video I wrote about it: https://christitus.com/btrfs-guide/. The biggest advantage of btrfs
is its ability to take snapshots, which can be useful for backups and system recovery. However, it requires more advanced knowledge to manage effectively.
Drive Management Tools
- fdisk: A command-line utility for managing disk partitions. It allows you to create, delete, and modify partitions on a disk.
- parted: A more advanced command-line utility for managing disk partitions, supporting larger disks and more complex partitioning schemes.
- lsblk: A command-line utility that lists block devices, including drives and partitions, along with their mount points and filesystem types.
- blkid: A command-line utility that displays information about block devices, including their UUIDs and filesystem types.
- df: A command-line utility that displays disk space usage for mounted filesystems.
- du: A command-line utility that estimates file and directory space usage.
- mount: A command-line utility that mounts filesystems to specific directories, allowing access to the data stored on those filesystems.
- umount: A command-line utility that unmounts filesystems, making them inaccessible until remounted.
- mkfs: A command-line utility that creates a filesystem on a partition or disk.
- fsck: A command-line utility that checks and repairs filesystems for errors.
- lsusb: A command-line utility that lists USB devices connected to the system, useful for identifying external drives and other USB peripherals.
- lspci: A command-line utility that lists PCI devices, including internal drives and controllers.
- smartctl: A command-line utility that monitors the health of hard drives and SSDs using S.M.A.R.T. (Self-Monitoring, Analysis, and Reporting Technology) data.
- hdparm: A command-line utility that allows you to configure and manage hard drives, including setting power management options and testing drive performance.
- gparted: A graphical partition editor that allows you to create, delete, and modify partitions on a disk using a user-friendly interface.
Mounting and Unmounting Drives
Mounting and unmounting drives is a common task in Linux, especially when dealing with external storage devices or network shares.
Mount commands:
# Mount a filesystem
sudo mount /dev/sdX1 /mnt/mydrive
# Unmount a filesystem
sudo umount /mnt/mydrive
Mounting Drives
To mount a drive, you need to specify the device (e.g., /dev/sda1
) and the mount point (e.g., /mnt/mydrive
). The mount point is a directory where the contents of the drive will be accessible.
You can also specify the filesystem type and mount options if needed. For example, to mount an ext4 filesystem with read-write permissions, you can use:
sudo mount -t ext4 -o rw /dev/sdX1 /mnt/mydrive
Where -t
specifies the filesystem type and -o
specifies mount options.
Unmounting Drives
To unmount a drive, you can use the umount
command followed by the mount point or device name. For example:
sudo umount /mnt/mydrive
If the drive is busy or in use, you may need to close any open files or processes using it before unmounting. You can also use the -l
option to perform a lazy unmount, which detaches the filesystem but allows it to be cleaned up later:
sudo umount -l /mnt/mydrive
Filesystem Management
Managing filesystems in Linux involves creating, formatting, and checking filesystems on drives and partitions. This section covers the essential commands and tools for filesystem management.
Creating Filesystems
WARNING: Creating a filesystem will erase and format all data on the partition. Ensure you have backups of any important data before proceeding.
To create a filesystem on a partition, you can use the mkfs
command followed by the filesystem type and the device name. For example, to create an ext4 filesystem on /dev/sdX1
, you can use:
sudo mkfs.ext4 /dev/sdX1
You can also specify other filesystem types, such as mkfs.xfs
for XFS or mkfs.fat -F 32
for FAT32. For example:
sudo mkfs.xfs /dev/sdX1
sudo mkfs.fat -F 32 /dev/sdX1
Note: When dealing with EFI System Partitions (ESP), I recommend referring to the this docuumentation: https://wiki.archlinux.org/title/EFI_system_partition for the most up-to-date information on creating and managing ESPs.
Checking Filesystems
To check a filesystem for errors, you can use the fsck
command followed by the device name. For example, to check an ext4 filesystem on /dev/sdX1
, you can use:
sudo fsck.ext4 /dev/sdX1
If you want to automatically fix errors, you can use the -y
option:
sudo fsck.ext4 -y /dev/sdX1
Viewing Filesystem Information
To view information about filesystems, you can use the df
command, which displays disk space usage for mounted filesystems. For example:
df -h
This command shows the disk space usage in a human-readable format, including the total size, used space, available space, and mount points for each filesystem.
Fstab Configuration
The /etc/fstab
file is used to define how disk partitions, network shares, and other file systems are mounted at boot time. Each line in the file represents a file system and its mount options.
Fstab structure:
- Device: usually the given name or UUID of the mounted device (sda1/sda2/etc).
- Mount Point: designates the directory where the device is/will be mounted.
- File System Type: nothing trick here, shows the type of filesystem in use. Options: lists any active mount options. If using multiple options they must be separated by commas.
- Backup Operation: (the first digit) this is a binary system where 1 = dump utility backup of a partition. 0 = no backup. This is an outdated backup method and should NOT be used.
- File System Check Order: (second digit) Here we can see three possible outcomes. 0 means that fsck will not check the filesystem. Numbers higher than this represent the check order. The root filesystem should be set to 1 and other partitions set to 2.
Stntax:
# <device> <mount point> <file system type> <options> <backup operation> <fsck order>
Example:
UUID=1234-5678 /mnt/data ext4 defaults 0 2
/dev/sdb1 /media/usb vfat defaults,nofail 0 0
Options:
defaults
: Use default mount options.nofail
: Ignore errors when mounting the filesystem.noauto
: Do not mount automatically at boot.user
: Allow non-root users to mount the filesystem.rw
: Mount the filesystem as read-write.ro
: Mount the filesystem as read-only.exec
: Allow execution of binaries on the filesystem.noexec
: Prevent execution of binaries on the filesystem.sync
: All I/O operations are done synchronously.async
: All I/O operations are done asynchronously.uid=1000
: Set the owner of the mounted filesystem to user ID 1000.gid=1000
: Set the group of the mounted filesystem to group ID 1000.umask=022
: Set the default permissions for files and directories on the mounted filesystem.dmask=027
: Set the default permissions for directories on the mounted filesystem.fmask=133
: Set the default permissions for files on the mounted filesystem.
My fstab file with NFS mounts and multiple drives:
# Static information about the filesystems.
# See fstab(5) for details.
# <file system> <dir> <type> <options> <dump> <pass>
# /dev/nvme1n1p2
UUID=1188f001-7a26-4d75-819f-202e4ef2da96 / ext4 rw,relatime 0 1
# /dev/nvme1n1p1
UUID=3337-A669 /boot vfat rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro 0 2
# /dev/nvme1n1p3
UUID=df8df26b-3bdc-427d-be86-43d6a25208b4 /home ext4 rw,relatime 0 2
# Network Drives
10.0.0.2:/volume2/Images /media/images nfs x-systemd.after=network-online.target,x-systemd.automount,x-systemd.mount-timeout=90,_netdev 0 0