Managing Storage
References:
The Basics
Listing block devices
lsblk
Creating partitions
fdisk {device}
Creating filesystems
mkfs.xfs /dev/device
mkfs.ext4 /dev/device
mkfs.vfat /dev/device
Mounting and unmounting partitions
mount /dev/partition /mnt/path
umount /mnt/path # unmount a device
Finding mounts
mount # shows all mounted filesystems, including kernel mounts
findmnt # shows all mounts in tree-like structure
lsblk
Mounting partitions automatically
To mount a filesystem automatically, add entry to /etc/fstab.
- Always create a backup before editing
- Always verify correctness before rebooting (
mount -aorfindmnt --verify)
device mount-path filesystem defaults 0 0
The last two entries are legacy options. Rarely used on modern systems.
If the fstab is broken (or something else), you may not be able to access a console if there is no root password. In this case, edit the GRUB boot command by pressing
eat the boot menu. Removerhgb quietfrom the end of the line starting withlinuxand addinit=/bin/bash. The filesystem will be mounted in read-only mode. Usemount -o remount,rw /and make the necessary changes. Modified files will get an incorrect SELinux label. Usetouch /.autorelabelto ensure labeling is corrected on the next boot and thenecho s > /proc/sysrq-triggerto trigger an emergency sync before rebooting withecho b > /proc/sysrq-trigger. This procedure is extremely important for the exam.
Using UUID and labels
Block device names (eg /dev/sdb) are subject to change, so use UUIDs or labels in the fstab.
Print block device attributes
blkid
Set a label for ext filesystem
tune2fs -L
Set a label for XFS filesystem
xfs_admin -L
Set a label while creating a filesystem
mkfs.* -L
Systemd mounts
Example systemd mount: /etc/systemd/system/files.mount
[Unit]
Documentation=man:fstab(5) man:systemd-fstab-generator(8)
[Mount]
What=/dev/disk/by-uuid/{UUID}
Where=/mnt/path
Type=ext4
Options=nofail
[Install]
WantedBy=multi-user.target
Examples copied from /run/systemd/generator will be missing the Install section.
Creating a swap partition
Create swap space from block device
- Set partition type to linux-swap
- Use
mkswapto create the swap filesystem - Activate using
swapon
Knowledge check
- What type of device would /dev/sdx commonly be?
- What type of device would /dev/vdx commonly be?
- Where does lsblk get its information from?
- Which filesystem offers multi-OS support?
- Which filesystem is used for a UEFI boot partition?
- Which filesystem is the default for RHEL?
- How can you view the systemd mounts?
Answers
- SCSI, SATA, and disks connected via USB
- KVM virtual machines
- The kernel partition table: /proc/partitions
- vfat
- vfat
- XFS
- Cat the files in /run/systemd/generator
Lab Exercise
- Create a partition with a size of 1GiB. Format it with ext4, and mount it persistently on /mounts/files using its UUID
- Create a 512 MiB swap partition and mount it persistently
- Create another 1G partition of type Linux Filesystem. Format it with XFS and intentionally put an incorrect UUID number in the fstab with the default mount settings. Reboot the VM and recover.
- Clint Jordan