Lesson
1.3 Objective review
Partitions
| Tool | Use |
|---|---|
lsblk |
Tree of block devices, partitions, LVM volumes and mount points. lsblk -f adds filesystem type, label and UUID |
blkid |
Prints UUID, LABEL and TYPE of each block device. Use UUIDs in /etc/fstab |
fdisk |
Interactive partitioning for MBR and GPT (fdisk -l lists) |
gdisk |
GPT focused version of fdisk |
parted |
Scriptable partitioning (parted /dev/sdb mklabel gpt mkpart data xfs 1MiB 100%) |
growpart |
Grows a partition to fill free space after it, typically after a cloud disk was enlarged (growpart /dev/sda 2) |
MBR supports 4 primary partitions (or 3 plus an extended one) and disks up to 2 TiB. GPT supports 128 partitions by default and very large disks, and is required for UEFI boot from the disk. After changing a disk that is in use, partprobe asks the kernel to re-read the table.
Filesystems
| Format | Notes |
|---|---|
| ext4 | Mature journaling filesystem. Can grow online and shrink offline (resize2fs). Checked with fsck / e2fsck |
| xfs | Default on RHEL. Excellent with large files and parallel I/O. Grows online with xfs_growfs, cannot shrink. Repaired with xfs_repair (not fsck) |
| btrfs | Copy on write with snapshots, subvolumes, checksums, compression and built in RAID. Default on openSUSE and Fedora Workstation |
| tmpfs | Lives in RAM (and swap). Contents vanish at reboot. Used for /run, often /tmp and /dev/shm |
| Utility | Purpose |
|---|---|
mkfs -t xfs /dev/sdb1 or mkfs.ext4 /dev/sdb1 |
Create a filesystem |
fsck /dev/sdb1 |
Check and repair (ext family). Unmount first |
resize2fs /dev/vg/lv |
Resize ext2/3/4 to the size of its device |
xfs_growfs /mountpoint |
Grow a mounted XFS filesystem |
xfs_repair /dev/sdb1 |
Repair an unmounted XFS filesystem |
df -h |
Free and used space per mounted filesystem. df -i shows inodes |
du -sh dir |
Space used by files in a directory tree |
fio |
Flexible I/O tester: benchmarks IOPS, throughput and latency with defined workloads |
Inodes store a file's metadata (owner, permissions, timestamps, pointers to data blocks) but not its name. ext4 creates a fixed number when the filesystem is made, so millions of tiny files can exhaust inodes while df -h still shows free space. df -i reveals it.
Logical Volume Manager (LVM)
LVM stacks three layers: physical volumes (PVs: disks or partitions) are pooled into volume groups (VGs), which are carved into logical volumes (LVs) that hold filesystems. LVs can be resized and moved between disks while in use.
| Layer | Create | Show | Change | Remove |
|---|---|---|---|---|
| PV | pvcreate /dev/sdb |
pvs, pvdisplay, pvscan |
pvresize (after the disk grew), pvmove (move extents off a disk) |
pvremove |
| VG | vgcreate data /dev/sdb /dev/sdc |
vgs, vgdisplay, vgscan |
vgextend data /dev/sdd, vgchange -a y/n (activate), vgexport / vgimport (move to another host) |
vgremove |
| LV | lvcreate -n web -L 20G data |
lvs, lvdisplay |
lvextend, lvresize, lvchange -a n |
lvremove |
LVs appear as /dev/data/web and /dev/mapper/data-web.
Growing a filesystem on LVM in one step: lvextend -r -L +10G /dev/data/web (-r runs resize2fs or xfs_growfs for you). -l +100%FREE uses all free space in the VG.
Replacing a failing disk with no downtime: vgextend data /dev/sdd, then pvmove /dev/sdb (moves its extents), vgreduce data /dev/sdb, pvremove /dev/sdb.
pvcreate /dev/sdb /dev/sdc
vgcreate data /dev/sdb /dev/sdc
lvcreate -n web -L 20G data
mkfs.xfs /dev/data/web
mkdir -p /srv/web && mount /dev/data/web /srv/web
lvextend -r -L +10G /dev/data/web # grow LV and filesystem togetherSoftware RAID (mdadm)
| Level | Minimum disks | Survives | Usable capacity |
|---|---|---|---|
| RAID 0 (striping) | 2 | no failures | all disks |
| RAID 1 (mirroring) | 2 | all but one disk | one disk |
| RAID 5 (striping with parity) | 3 | one disk | n minus 1 disks |
| RAID 6 (double parity) | 4 | two disks | n minus 2 disks |
| RAID 10 (mirrored stripes) | 4 | one per mirror | half |
- Create:
mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb1 /dev/sdc1 - Status:
cat /proc/mdstat([UU]healthy,[U_]degraded) andmdadm --detail /dev/md0 - Replace a disk:
mdadm /dev/md0 --fail /dev/sdc1 --remove /dev/sdc1, then--addthe new one - Persist:
mdadm --detail --scan >> /etc/mdadm.conf(/etc/mdadm/mdadm.confon Debian)
Mounting
mount /dev/sdb1 /data attaches a filesystem; umount /data detaches it (it fails with "target is busy" while a process uses it: find it with lsof /data or fuser -vm /data).
/etc/fstab: filesystems to mount at boot. Six fields: device (UUID= preferred), mount point, type, options, dump (0), fsck order (1 for root, 2 for others, 0 to skip)./proc/mounts: what the kernel actually has mounted right now./etc/mtabis a symlink to it on modern systems.mount -amounts everything in fstab that is not yet mounted: test fstab edits with it before rebooting.findmntshows the mount tree.- autofs mounts on demand when a path is accessed and unmounts after a timeout. The master map
/etc/auto.masterpoints a directory at a map file, for example/mnt/nfs /etc/auto.nfs.
UUID=7c1e... /data xfs defaults,noatime,nofail 0 2
| Option | Effect |
|---|---|
ro / rw |
Read only / read write |
remount |
Change options of a mounted filesystem: mount -o remount,rw / |
noatime |
Do not update access times on read (less I/O). nodiratime does the same for directories only |
noexec |
Do not allow executing binaries from this filesystem (good for /tmp) |
nosuid |
Ignore setuid and setgid bits |
nodev |
Do not interpret device files |
nofail |
Do not fail the boot if the device is missing (removable or network disks) |
_netdev |
The device needs the network; wait for it |
Network mounts
- NFS: the server lists shares in
/etc/exports(/srv/share 192.168.1.0/24(rw,sync,root_squash)) and applies them withexportfs -ra. Clients mount withmount -t nfs server:/srv/share /mnt/share.showmount -e serverlists exports. - SMB (Samba): Windows compatible shares. Clients mount with
mount -t cifs //server/share /mnt/share -o credentials=/root/.smbcred(cifs-utils). Keep passwords in a credentials file with mode 600, not in fstab.
A frequent exam trap: you can grow XFS but never shrink it. To make an XFS volume smaller, back it up, recreate it at the new size and restore.