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
Know MBR and GPT
A partition table describes the parts of a disk. There are two kinds.
| MBR (msdos) | GPT | |
|---|---|---|
| Max. disk size | 2 TiB | 8 ZiB (practically unlimited) |
| Partitions | 4 primary (or 3 + 1 extended with logical ones) | 128 by default |
| Backup of the table | No | Yes, at the end of the disk |
| Boot | BIOS | UEFI (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.
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 names | Disk type |
|---|---|
/dev/sda, /dev/sdb | SATA, SAS, USB, SCSI disks |
/dev/vda, /dev/vdb | KVM virtual disks (virtio) |
/dev/nvme0n1 (partitions nvme0n1p1) | NVMe SSDs |
Understand size units
Disk sellers use powers of 1000 (GB), Linux tools often use powers of 1024 (GiB). The difference grows with the size.
| Unit | Bytes |
|---|---|
| KB / KiB | 1000 / 1024 |
| MB / MiB | 1000² / 1024² |
| GB / GiB | 1000³ (1 000 000 000) / 1024³ (1 073 741 824) |
| TB / TiB | 1000⁴ / 1024⁴ |
Notitie
fdisk takes +1G as 1 GiB. parted takes 1GB as 1000³ bytes and 1GiB as 1024³.
Create an MBR partition with fdisk
fdisk is a menu tool. Changes are only written when you press w.
- Open the disk and create an MBR table (only on an empty disk).
fdisk /dev/vdb Command (m for help): o - 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 - Show the table, then write it.
Command (m for help): p Command (m for help): w - Make sure the kernel sees the new partition.
| fdisk key | Does |
|---|---|
m | Help |
p | Print the table |
n | New partition |
d | Delete a partition |
t | Change the type (like 82 swap, 8e LVM on MBR) |
o / g | New empty MBR / GPT table |
w | Write and quit |
q | Quit without saving |
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 5Let op
An extended partition holds no data itself. Use all its space for logical partitions.
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): wGPT type aliases you can type: linux, swap, lvm, raid, uefi.
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).
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.
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| Option | Changes |
|---|---|
-L NAME | Label |
-o OPTS | Default mount options |
-m N | Reserved blocks for root (default 5 %) |
-c N / -i TIME | Check after N mounts / after a time |
-U UUID | New UUID |
Change XFS file system settings
XFS has xfs_admin for label and UUID (only when unmounted) and xfs_info to show the settings.
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.
Add a swap file
Without free disk space for a partition, use a file as swap.
Mount file systems by hand
Mounting connects a file system to a folder (the mount point). umount disconnects it.
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.
Mount at boot with /etc/fstab
Every line in /etc/fstab is a file system that is mounted at boot.
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| Field | Meaning |
|---|---|
| 1 | What: UUID=, LABEL=, or a device |
| 2 | Where: the mount point (none for swap) |
| 3 | File system type |
| 4 | Mount options: defaults, ro, noexec, nosuid, nodev, _netdev, nofail |
| 5 | dump (backup flag): 0 |
| 6 | fsck order: 0 = never; 1 = root; 2 = others (XFS: 0) |
- Add the line, then reload systemd and test before you reboot.
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).
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.
[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.targetOnthoud
/data → data.mount; /srv/web → srv-web.mount. Check the name with systemd-escape -p --suffix=mount /srv/web.
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).
lsblkshows disks and partitions;fdiskandgdiskchange them interactively;partedworks with commands too.- Make a file system with
mkfs.xfs(default on RHEL),mkfs.ext4ormkfs.vfat. - XFS can grow but not shrink; ext4 can grow and shrink.
- Swap: make it with
mkswap, turn it on withswapon; a swap file must be made withddorfallocateand get permission 600. - Mount at boot with a line in
/etc/fstab: device (best a UUID fromblkid), mount point, type, options, dump, fsck order. - Test fstab with
mount -a(andfindmnt --verify) before you reboot: a wrong line can stop the boot. - systemd can also mount with
.mountunits.
Learn the key terms
The words of this chapter in simple English.
| Term | Meaning |
|---|---|
| Partition | A part of a disk with its own start and end. |
| MBR | Master Boot Record: old partition table. |
| GPT | GUID Partition Table: modern partition table. |
| Extended / logical partition | On MBR, a container partition that holds more partitions. |
| File system | The structure that stores files on a partition (XFS, ext4, vfat). |
| Mount point | The folder where a file system appears. |
| UUID | A unique ID of a file system, safe to use in fstab. |
| Label | A name you give a file system. |
| Swap | Disk space used as extra memory. |
| fstab | The file with the mounts that happen at boot. |
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?
blkid /dev/vdb1 (or lsblk -f).
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?
mkdir -p /data, then mount -a and findmnt /data.
6How do you add a 1 GiB swap file?
dd if=/dev/zero of=/swapfile bs=1M count=1024, chmod 600 /swapfile, mkswap /swapfile, swapon /swapfile, and an fstab line /swapfile none swap defaults 0 0.
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?
partprobe (or udevadm settle).