Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

Hoofdstuk 14Naam in het boek: Managing Storage

Create partitions, file systems and swap, and mount them

A new disk is empty space. This chapter shows how to split it into partitions (MBR or GPT), put a file system or swap on them, change file system settings, and mount them — by hand, at boot through /etc/fstab, or with systemd mount units.

  • 19 Lessen
  • 32 Commando's
  • 1 Labs

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

Examendoelen in dit hoofdstuk

  • Partities op MBR- en GPT-schijven tonen, maken en verwijderen
  • Bestandssystemen bij het opstarten koppelen via UUID of label
  • Nieuwe partities, logische volumes en swap toevoegen zonder dataverlies
  • vfat-, ext4- en xfs-bestandssystemen maken, koppelen, ontkoppelen en gebruiken

14.1

Know MBR and GPT

A partition table describes the parts of a disk. There are two kinds.

MBR (msdos)GPT
Max. disk size2 TiB8 ZiB (practically unlimited)
Partitions4 primary (or 3 + 1 extended with logical ones)128 by default
Backup of the tableNoYes, at the end of the disk
BootBIOSUEFI (and BIOS with a BIOS boot partition)

Onthoud

New systems and disks over 2 TiB use GPT. MBR is for old BIOS systems and small disks.

14.2

See the disks

First find the right disk. A mistake here destroys data.

lsblk
NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
vda           252:0    0   20G  0 disk
├─vda1        252:1    0    1G  0 part /boot
└─vda2        252:2    0   19G  0 part
  ├─rhel-root 253:0    0   17G  0 lvm  /
  └─rhel-swap 253:1    0    2G  0 lvm  [SWAP]
vdb           252:16   0   10G  0 disk
lsblk -f
fdisk -l /dev/vdb
Device namesDisk type
/dev/sda, /dev/sdbSATA, SAS, USB, SCSI disks
/dev/vda, /dev/vdbKVM virtual disks (virtio)
/dev/nvme0n1 (partitions nvme0n1p1)NVMe SSDs

14.3

Understand size units

Disk sellers use powers of 1000 (GB), Linux tools often use powers of 1024 (GiB). The difference grows with the size.

UnitBytes
KB / KiB1000 / 1024
MB / MiB1000² / 1024²
GB / GiB1000³ (1 000 000 000) / 1024³ (1 073 741 824)
TB / TiB1000⁴ / 1024⁴

Notitie

fdisk takes +1G as 1 GiB. parted takes 1GB as 1000³ bytes and 1GiB as 1024³.

14.4

Create an MBR partition with fdisk

fdisk is a menu tool. Changes are only written when you press w.

  1. Open the disk and create an MBR table (only on an empty disk).
    fdisk /dev/vdb
    Command (m for help): o
  2. Create a primary partition of 1 GiB.
    Command (m for help): n
    Select (default p): p
    Partition number (1-4, default 1): 1
    First sector (2048-20971519, default 2048): <Enter>
    Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-20971519, default 20971519): +1G
  3. Show the table, then write it.
    Command (m for help): p
    Command (m for help): w
  4. Make sure the kernel sees the new partition.
    partprobe /dev/vdb
    lsblk /dev/vdb
fdisk keyDoes
mHelp
pPrint the table
nNew partition
dDelete a partition
tChange the type (like 82 swap, 8e LVM on MBR)
o / gNew empty MBR / GPT table
wWrite and quit
qQuit without saving

14.5

Use extended and logical partitions

MBR has only 4 slots. To get more than 4 partitions, make the 4th one extended: a container for logical partitions (numbered from 5).

Command (m for help): n
Select (default e): e
Command (m for help): n
All primary partitions are in use.
Adding logical partition 5

Let op

An extended partition holds no data itself. Use all its space for logical partitions.

14.6

Create GPT partitions with fdisk

The same fdisk, but start with g (new GPT table). There is no primary/extended question.

fdisk /dev/vdc
Command (m for help): g
Command (m for help): n
Partition number (1-128, default 1): <Enter>
First sector (2048-20971486, default 2048): <Enter>
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-20971486, default 20969471): +2G
Command (m for help): t
Partition type or alias (type L to list all): swap
Command (m for help): w

GPT type aliases you can type: linux, swap, lvm, raid, uefi.

Commando's in deze les

14.7

Create GPT partitions with parted

parted works in a menu or with one command line — good for scripts. It writes changes at once (no w).

parted /dev/vdc mklabel gpt
parted /dev/vdc mkpart data xfs 1MiB 1GiB
parted /dev/vdc mkpart swap linux-swap 1GiB 2GiB
parted /dev/vdc print
parted /dev/vdc unit MiB print free

Let op

mklabel on a used disk deletes the partition table at once, without a question in command-line mode.

14.8

Create file systems

A partition needs a file system before it can store files. RHEL uses XFS by default; ext4 is the other common choice; vfat is for USB sticks and the EFI partition.

mkfs.xfs /dev/vdb1
mkfs.ext4 /dev/vdb2
mkfs.vfat /dev/vdb3
mkfs -t xfs -L data /dev/vdb1
lsblk -f /dev/vdb
XFSext4
Default on RHELyesno
Grow while mountedyesyes
Shrinknoyes (only unmounted)
Check/repairxfs_repairfsck.ext4 / e2fsck

Let op

mkfs on the wrong device destroys its data. Check with lsblk -f first; mkfs.xfs refuses to overwrite a file system unless you add -f.

14.9

Change ext4 file system settings

tune2fs changes settings of ext4, dumpe2fs shows them all.

tune2fs -l /dev/vdb2 | head -20
dumpe2fs -h /dev/vdb2
tune2fs -L backup /dev/vdb2
tune2fs -o acl,user_xattr /dev/vdb2
tune2fs -m 1 /dev/vdb2
tune2fs -c 30 /dev/vdb2
OptionChanges
-L NAMELabel
-o OPTSDefault mount options
-m NReserved blocks for root (default 5 %)
-c N / -i TIMECheck after N mounts / after a time
-U UUIDNew UUID

14.10

Change XFS file system settings

XFS has xfs_admin for label and UUID (only when unmounted) and xfs_info to show the settings.

xfs_admin -L data /dev/vdb1
xfs_admin -l /dev/vdb1
xfs_info /data
xfs_admin -U generate /dev/vdb1

14.11

Add a swap partition

Swap is disk space the kernel uses when RAM is full. Make a partition with the swap type, format it with mkswap, then switch it on.

  1. Create the partition (type swap in fdisk, or linux-swap in parted), then format it.
    mkswap /dev/vdb3
  2. Switch it on and check.
    swapon /dev/vdb3
    swapon --show
    free -h
  3. Make it permanent in /etc/fstab.
    /etc/fstab (line)
    UUID=8f1b…   none   swap   defaults   0 0
    swapoff /dev/vdb3; swapon -a; swapon --show

14.12

Add a swap file

Without free disk space for a partition, use a file as swap.

dd if=/dev/zero of=/swapfile bs=1M count=1024
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo "/swapfile none swap defaults 0 0" >> /etc/fstab

Notitie

Use dd (not fallocate) for swap files on XFS to be safe; the file must not have holes.

14.13

Mount file systems by hand

Mounting connects a file system to a folder (the mount point). umount disconnects it.

mkdir /data
mount /dev/vdb1 /data
findmnt /data
umount /data
mount -o ro /dev/vdb1 /data
mount -o remount,rw /data

Let op

umount fails with "target is busy" when a process uses the folder (also your own shell). Leave the folder and check with lsof /data or fuser -vm /data.

14.14

Use UUIDs and labels

Names like /dev/vdb1 can change when you add disks. A UUID (unique ID) or a label stays the same — use them in /etc/fstab.

blkid
/dev/vdb1: LABEL="data" UUID="4e5c7a6b-3f1d-4c2e-9a5b-7d2f8e1c0a93" TYPE="xfs" PARTUUID="…"
lsblk -f
mount UUID="4e5c7a6b-3f1d-4c2e-9a5b-7d2f8e1c0a93" /data
mount LABEL=data /data

14.15

Mount at boot with /etc/fstab

Every line in /etc/fstab is a file system that is mounted at boot.

/etc/fstab
UUID=4e5c7a6b-3f1d-4c2e-9a5b-7d2f8e1c0a93  /data     xfs   defaults        0 0
LABEL=backup                               /backup   ext4  defaults,noexec 0 2
UUID=8f1b…                                 none      swap  defaults        0 0
FieldMeaning
1What: UUID=, LABEL=, or a device
2Where: the mount point (none for swap)
3File system type
4Mount options: defaults, ro, noexec, nosuid, nodev, _netdev, nofail
5dump (backup flag): 0
6fsck order: 0 = never; 1 = root; 2 = others (XFS: 0)
  1. Add the line, then reload systemd and test before you reboot.
    vim /etc/fstab
    systemctl daemon-reload
    mount -a
    findmnt --verify

Examentip

A wrong fstab line can stop the boot (emergency mode). Always run mount -a and findmnt --verify after editing. On the exam, reboot at the end to prove everything mounts.

Op het werk

nofail lets the system boot even when that disk is missing. For a USB or network disk add nofail (and _netdev for network storage).

14.16

Mount with systemd mount units

systemd creates a .mount unit for every fstab line. You can also write a mount unit yourself — the file name must match the mount point.

/etc/systemd/system/data.mount
[Unit]
Description=Data disk
 
[Mount]
What=/dev/disk/by-uuid/4e5c7a6b-3f1d-4c2e-9a5b-7d2f8e1c0a93
Where=/data
Type=xfs
Options=defaults
 
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable --now data.mount
systemctl status data.mount

Onthoud

/data → data.mount; /srv/web → srv-web.mount. Check the name with systemd-escape -p --suffix=mount /srv/web.

14.17

Remember the main points

The most important facts of this chapter, in short.

  • Disks use MBR (old, max 4 primary partitions, 2 TiB) or GPT (up to 128 partitions, very large disks).
  • lsblk shows disks and partitions; fdisk and gdisk change them interactively; parted works with commands too.
  • Make a file system with mkfs.xfs (default on RHEL), mkfs.ext4 or mkfs.vfat.
  • XFS can grow but not shrink; ext4 can grow and shrink.
  • Swap: make it with mkswap, turn it on with swapon; a swap file must be made with dd or fallocate and get permission 600.
  • Mount at boot with a line in /etc/fstab: device (best a UUID from blkid), mount point, type, options, dump, fsck order.
  • Test fstab with mount -a (and findmnt --verify) before you reboot: a wrong line can stop the boot.
  • systemd can also mount with .mount units.

14.18

Learn the key terms

The words of this chapter in simple English.

TermMeaning
PartitionA part of a disk with its own start and end.
MBRMaster Boot Record: old partition table.
GPTGUID Partition Table: modern partition table.
Extended / logical partitionOn MBR, a container partition that holds more partitions.
File systemThe structure that stores files on a partition (XFS, ext4, vfat).
Mount pointThe folder where a file system appears.
UUIDA unique ID of a file system, safe to use in fstab.
LabelA name you give a file system.
SwapDisk space used as extra memory.
fstabThe file with the mounts that happen at boot.

14.19

Check what you learned

Think about the answer first, then open it.

1Which partition table do you need for a 4 TiB disk?

GPT.

2How do you make an XFS file system on /dev/vdb1?
3How do you find the UUID of /dev/vdb1?
4Write an fstab line that mounts that XFS file system on /data.

UUID=xxxx-xxxx /data xfs defaults 0 0.

5How do you test the new fstab line without a reboot?
6How do you add a 1 GiB swap file?
7Can you make an XFS file system smaller?

No. XFS can only grow. Use ext4 if you need to shrink.

8How do you make the kernel read a changed partition table?

Oefenen

Alle commando's in dit hoofdstuk