实验 6:文件系统¶
目标¶
完成此实验后,您将能够
- 分区磁盘
- 使用逻辑卷管理 (LVM) 系统
- 创建新的文件系统
- 挂载和使用文件系统
完成此实验室的估计时间:90 分钟
常用文件系统应用程序概述¶
下面是用于管理文件系统的常用应用程序的摘要。
sfdisk
¶
用于显示或操作磁盘分区表
概要
```
Usage:
sfdisk [options] <dev> [[-N] <part>]
sfdisk [options] <command>
Commands:
-A, --activate <dev> [<part> ...] list or set bootable (P)MBR partitions
-d, --dump <dev> dump partition table (usable for later input)
-J, --json <dev> dump partition table in JSON format
-g, --show-geometry [<dev> ...] list geometry of all or specified devices
-l, --list [<dev> ...] list partitions of each device
-F, --list-free [<dev> ...] list unpartitioned free areas of each device
-r, --reorder <dev> fix partitions order (by start offset)
-s, --show-size [<dev> ...] list sizes of all or specified devices
-T, --list-types print the recognized types (see -X)
-V, --verify [<dev> ...] test whether partitions seem correct
--delete <dev> [<part> ...] delete all or specified partitions
--part-label <dev> <part> [<str>] print or change partition label
--part-type <dev> <part> [<type>] print or change partition type
--part-uuid <dev> <part> [<uuid>] print or change partition uuid
--part-attrs <dev> <part> [<str>] print or change partition attributes
--disk-id <dev> [<str>] print or change disk label ID (UUID)
--relocate <oper> <dev> move partition header
```
debugfs
¶
ext2/ext3/ext4 文件系统调试器
概要
```
debugfs [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-d data_source_device] [-i] [-n] [-D] [-V] [[-w] [-z undo_file] [-c]] [device]
```
badblocks
¶
在设备上搜索坏块
概要
```
badblocks [ -svwnfBX ] [ -b block_size ] [ -c blocks_at_once ] [ -d read_delay_factor ] [ -e max_bad_blocks ] [ -i input_file ] [ -o output_file ] [ -p num_passes ] [
-t test_pattern ] device [ last_block ] [ first_block ]
```
dosfsck
¶
检查设备上的 FAT 文件系统是否有错误。
概要
```
Usage: dosfsck [OPTIONS] DEVICE
Options:
-a automatically repair the filesystem
-A toggle Atari variant of the FAT filesystem
-b make read-only boot sector check
-c N use DOS codepage N to decode short file names (default: 850)
-d PATH drop file with name PATH (can be given multiple times)
-f salvage unused chains to files
-F NUM specify FAT table NUM used for filesystem access
-l list path names
-n no-op, check non-interactively without changing
-p same as -a, for compat with other *fsck
-r interactively repair the filesystem (default)
-S disallow spaces in the middle of short file names
-t test for bad clusters
-u PATH try to undelete (non-directory) file that was named PATH (can be
given multiple times)
-U allow only uppercase characters in volume and boot label
-v verbose mode
-V perform a verification pass
--variant=TYPE handle variant TYPE of the filesystem
-w write changes to disk immediately
-y same as -a, for compat with other *fsck
--help print this message
```
mkdosfs
或 mkfs.fat
¶
用于在 Linux 中创建 MS-DOS (FAT12、FAT16 和 FAT32) 文件系统
概要
```
Usage: mkdosfs [OPTIONS] TARGET [BLOCKS]
Options:
-a Disable alignment of data structures
-A Toggle Atari variant of the filesystem
-b SECTOR Select SECTOR as location of the FAT32 backup boot sector
-c Check device for bad blocks before creating the filesystem
-C Create file TARGET then create filesystem in it
-D NUMBER Write BIOS drive number NUMBER to boot sector
-f COUNT Create COUNT file allocation tables
-F SIZE Select FAT size SIZE (12, 16 or 32)
-g GEOM Select disk geometry: heads/sectors_per_track
-h NUMBER Write hidden sectors NUMBER to boot sector
-i VOLID Set volume ID to VOLID (a 32 bit hexadecimal number)
-I Ignore and disable safety checks
-l FILENAME Read bad blocks list from FILENAME
-m FILENAME Replace default error message in boot block with contents of FILENAME
-M TYPE Set media type in boot sector to TYPE
.........
```
dumpe2fs
¶
列出所列设备上的超级块和块组信息。
概要
```
Usage: dumpe2fs [-bfghimxV] [-o superblock=<num>] [-o blocksize=<num>] device
```
fdisk
¶
查看和操作 (添加、删除和修改) 磁盘分区表
概要
```
Usage:
fdisk [options] <disk> change partition table
fdisk [options] -l [<disk>...] list partition table(s)
Display or manipulate a disk partition table.
Options:
-b, --sector-size <size> physical and logical sector size
-B, --protect-boot don't erase bootbits when creating a new label
-c, --compatibility[=<mode>] mode is 'dos' or 'nondos' (default)
-L, --color[=<when>] colorize output (auto, always or never) colors are enabled by default
-l, --list display partitions and exit
-x, --list-details like --list but with more details
-n, --noauto-pt don't create default partition table on empty devices
-o, --output <list> output columns
-t, --type <type> recognize specified partition table type only
-u, --units[=<unit>] display units: 'cylinders' or 'sectors' (default)
-s, --getsz display device size in 512-byte sectors [DEPRECATED]
--bytes print SIZE in bytes rather than in human readable format
--lock[=<mode>] use exclusive device lock (yes, no or nonblock)
-w, --wipe <mode> wipe signatures (auto, always or never)
-W, --wipe-partitions <mode> wipe signatures from new partitions (auto, always or never)
-C, --cylinders <number> specify the number of cylinders
-H, --heads <number> specify the number of heads
-S, --sectors <number> specify the number of sectors per track
```
fsck
¶
用于检查和修复 Linux 文件系统。实际上是多个文件系统特定实用程序的包装器 (例如 fsck.ext3、fsck.ext2 等)。
概要
```
Usage:
fsck [options] -- [fs-options] [<filesystem> ...]
Check and repair a Linux filesystem.
Options:
-A check all filesystems
-C [<fd>] display progress bar; file descriptor is for GUIs
-l lock the device to guarantee exclusive access
-M do not check mounted filesystems
-N do not execute, just show what would be done
-P check filesystems in parallel, including root
-R skip root filesystem; useful only with '-A'
-r [<fd>] report statistics for each device checked;
file descriptor is for GUIs
-s serialize the checking operations
-T do not show the title on startup
-t <type> specify filesystem types to be checked;
<type> is allowed to be a comma-separated list
-V explain what is being done
```
hdparm
¶
用于获取或设置硬盘参数
概要
```
hdparm [options] [device]
```
tune2fs
¶
用于调整 ext2/ext3/ext4 文件系统的可调文件系统参数。执行此操作时文件系统不得以写模式挂载。
概要
```
Usage: tune2fs [-c max_mounts_count] [-e errors_behavior] [-f] [-g group]
[-i interval[d|m|w]] [-j] [-J journal_options] [-l]
[-m reserved_blocks_percent] [-o [^]mount_options[,...]]
[-r reserved_blocks_count] [-u user] [-C mount_count]
[-L volume_label] [-M last_mounted_dir]
[-O [^]feature[,...]] [-Q quota_options]
[-E extended-option[,...]] [-T last_check_time] [-U UUID]
[-I new_inode_size] [-z undo_file] device
```
mkswap
¶
在设备上创建 Linux 交换区域
概要
```
mkswap [-c] [-vN] [-f] [-p PSZ] device [size]
```
mkfs
¶
创建 Linux 文件系统
概要
```
mkfs [ -V ] [ -t fstype ] [ fs-options ] filesys [ blocks ]
```
parted
¶
磁盘分区和分区大小调整程序。
概要
```
Parted [options] [device [command [options]]]
```
swapon
和 swapoff
¶
启用/禁用用于分页和交换的设备和文件
概要
```
swapon [-v] [-p priority] specialfile
```
mount
¶
用于挂载文件系统。
概要
```
Mount [-fnrsvw] [-o options [,...]] device | dir
```
练习 1¶
在此本地系统上执行此练习
创建分区 (fdisk
、mke2fs
、fsck
、tune2fs
)
在此练习中,您将在硬盘上创建其他分区。在初始安装期间,您留出了一些空闲空间。您将在这些空间上创建分区。
分区磁盘允许将磁盘视为一组独立的存储区域。
分区也使备份更容易,并有助于限制和约束潜在的问题区域。
硬盘空间不是无限的,而您作为管理员的职责之一就是管理可用的有限空间。例如,限制用户可以在磁盘上存储个人文件的总存储区域的一个简单方法是为用户的主目录创建一个单独的分区 (也可以使用配额)。
探索块存储设备¶
您将使用 fdisk
实用程序
-
以 root 用户身份登录,显示磁盘的当前结构。键入
[root@serverXY root]# fdisk -l Disk /dev/vda: 25 GiB, 26843545600 bytes, 52428800 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0xb3053db5 Device Boot Start End Sectors Size Id Type /dev/vda1 * 2048 52428766 52426719 25G 83 Linux
-
显示当前的磁盘使用情况统计信息。键入
[root@serverXY root]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 4.0M 0 4.0M 0% /dev tmpfs 479M 84K 479M 1% /dev/shm /dev/vda1 24G 8.5G 14G 39% / ...<SNIPPED>...
从上面的示例输出的“Used”列中,您可以看到挂载了根 (/) 目录的主分区 (/dev/vda1) 已完全 (100%) 填满。
如果您有不同大小的磁盘,或者没有遵循操作系统安装期间使用的分区方案,您的输出可能会有所不同。
创建 [假] 块设备¶
我们不希望您意外修改本地硬盘并使其无法运行,因此我们将使用一个行为和模拟实际块设备的伪设备来完成以下练习。这可以通过创建大小合理的 [稀疏] 文件并将其与伪设备关联来完成。在 Linux 系统上,这些伪设备被称为环回设备。环回设备是一种伪设备,它使得可以将常规数据文件视为块设备。
(此步骤大致相当于您在购买实际磁盘/服务器存储时必须做出的决定。例如:类型、品牌、尺寸、接口、外形尺寸等)
-
在仍然以 root 用户身份登录系统时,使用 losetup 实用程序创建一个大小为 10GB 的稀疏文件。键入
[root@serverPR root]# truncate --size 10GiB /tmp/10G-fake-disk.img
-
运行不带任何选项的
losetup
命令以显示活动的环回设备。键入[root@serverPR root]# losetup
-
再次运行
losetup
命令以查看/查找第一个未使用的环回设备。键入[root@serverPR root]# losetup -f --nooverlap /dev/loop0
在我们示例系统输出中的第一个可用或未使用的环回设备是
/dev/loop0
。 -
使用 10G-fake-disk.img 作为支持文件,通过运行以下命令将文件与可用的环回设备关联
losetup -f --nooverlap --partscan /tmp/10G-fake-disk.img
-
再次运行
losetup
命令以显示正在使用的环回设备。键入[root@serverPR root]# losetup NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE DIO LOG-SEC /dev/loop0 0 0 0 0 /tmp/10G-fake-disk.img 0 512
-
使用
sfdisk
实用程序列出新伪块设备上的任何分区。键入[root@localhost ~]# sfdisk -l /dev/loop0 Disk /dev/loop0: 10 GiB, 10737418240 bytes, 20971520 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes
-
现在使用
fdisk
列出同一设备上的任何分区。键入[root@localhost ~]# fdisk -l /dev/loop0 Disk /dev/loop0: 10 GiB, 10737418240 bytes, 20971520 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes
创建分区¶
-
您将使用
fdisk
程序创建一个新分区。fdisk
以交互方式运行,因此您将有许多问答式的提示来完成特定任务。首先将环回块设备的名称作为参数传递给
fdisk
。键入[root@localhost ~]# fdisk /dev/loop0 Welcome to fdisk (util-linux 2.*). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table. Created a new DOS disklabel with disk identifier 0xe3aa91a1. Command (m for help):
-
键入
m
在fdisk
提示符下,显示fdisk
的内置帮助系统。Command (m for help): m Help: DOS (MBR) a toggle a bootable flag b edit nested BSD disklabel c toggle the dos compatibility flag Generic d delete a partition F list free unpartitioned space l list known partition types n add a new partition p print the partition table t change a partition type v verify the partition table i print information about a partition ...<SNIP>...
-
从显示的帮助列表中,我们可以看到
n
用于添加新分区。键入n
在提示符下Command (m for help): n Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions)
-
创建主分区类型,键入
p
Command (m for help): n Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): p
-
这是块设备上的第一个主分区。将分区号设置为 1
Partition number (1-4, default 1): 1
-
通过按 Enter 接受块设备的第一个扇区的默认值
First sector (2048-20971519, default 2048):
-
通过按 Enter 接受块设备的最后一个扇区的默认值
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-20971519, default 20971519):
-
回到主
fdisk
提示符,键入p
以打印块设备的当前分区表Command (m for help): p Disk /dev/loop0: 10 GiB, 10737418240 bytes, 20971520 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0xe3aa91a1 Device Boot Start End Sectors Size Id Type /dev/loop0p1 2048 20971519 20969472 10G 83 Linux
您创建的新分区是上面
/dev/loop0p1
上的分区。您会注意到分区类型是“83”。 -
一切看起来都很好。通过键入
fdisk
的w
子命令将所有更改写入分区表Command (m for help): w
您很可能会看到关于无法重新读取分区表的警告。
w
fdisk
命令还将退出fdisk
程序并返回 shell 提示符。 -
根据您在上一步将分区表写入磁盘后可能收到的警告消息,有时需要采取额外步骤来促使内核识别新的硬盘更改。使用
partprobe
命令执行此操作[root@localhost ~]# partprobe
技巧
使用
fdisk
时,新创建分区的默认分区类型是 Linux (0x83)。您可以使用fdisk
的t
命令更改类型。例如,要将分区类型更改为 LVM (0x8e) 类型,您将执行以下操作键入
t
以更改分区类型Command (m for help): t
然后在提示符处输入 LVM 类型分区的十六进制代码 (0x8e)
Hex code or alias (type L to list all): 8e
通过键入
w
fdisk
命令将所有更改写入分区表Command (m for help): w
创建物理卷¶
为了帮助演示传统块设备管理方法与卷管理器方法等更现代方法之间的一些细微差别,我们将创建一个新的伪块设备,并尝试对其进行准备 (类似于分区) 以便与文件系统一起使用。
在接下来的步骤中,我们将创建一个由另一个常规文件支持的新环回设备。然后我们将设置该设备以用于逻辑卷管理器 (LVM) 系统。
-
以具有管理员特权的用户身份登录时,创建一个名为
10G-fake-lvm-disk.img
的 10GB 稀疏文件。键入[root@server root]# truncate --size 10GiB /tmp/10G-fake-lvm-disk.img
-
运行
losetup
命令以查看/查找第一个未使用的环回设备。键入[root@serverPR root]# losetup -f --nooverlap
我们的示例系统中的第一个可用或未使用的环回设备已增加,现在是 /dev/loop1。
-
使用 10G-fake-lvm-disk.img 作为支持文件,通过运行以下命令将文件与可用的环回设备关联
[root@server root]# losetup -f --nooverlap --partscan /tmp/10G-fake-lvm-disk.img
-
再次运行
losetup
命令以显示正在使用的环回设备。键入[root@localhost ~]# losetup
输出
NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE DIO LOG-SEC /dev/loop1 0 0 0 0 /tmp/10G-fake-lvm-disk.img 0 512 /dev/loop0 0 0 0 0 /tmp/10G-fake-disk.img 0 512
我们在输出中看到了 /dev/loop1 与 /tmp/10G-fake-lvm-disk.img 支持文件的映射。太好了。
-
使用
pvdisplay
命令查看系统上当前定义的物理卷。键入[root@localhost ~]# pvdisplay --- Physical volume --- PV Name /dev/vda3 VG Name rl PV Size 98.41 GiB / not usable 2.00 MiB ...<SNIP>...
-
将新的 /dev/loop1 块设备 (10G-fake-lvm-disk.img) 初始化为物理卷。使用
pvcreate
实用程序。键入[root@localhost ~]# pvcreate /dev/loop1 Physical volume "/dev/loop1" successfully created.
-
再次运行
pvdisplay
命令以查看任何更改。
将物理卷分配给卷组¶
在本节中,您将学习如何将 PV 设备分配给现有卷组。这会增加现有卷组的存储容量。
您将把上面准备和创建的 /dev/loop1
物理卷 (PV) 添加到现有的 rl
卷组 (VG) 中。
-
使用
vgdisplay
命令查看当前配置的卷组。键入[root@localhost ~]# vgdisplay
输出
--- Volume group --- VG Name rl System ID Format lvm2 .......... VG Size 98.41 GiB PE Size 4.00 MiB Total PE 25193 Alloc PE / Size 25193 / 98.41 GiB Free PE / Size 0 / 0 ...<SNIP>...
注意
从上面的输出中:- 卷组名称为 rl - VG 的大小为 98.41 GiB - VG 中有 0 (零) 个物理扩展块 (PE) 是空闲的,相当于 0MB 的空间。
-
将新的 PV (/dev/loop1) 分配给现有的
rl
卷组。使用vgextend
命令,键入[root@localhost ~]# vgextend rl /dev/loop1
输出
Volume group "rl" successfully extended
-
再次运行
vgdisplay
命令以查看您的更改。键入[root@localhost ~]# vgdisplay
输出
--- Volume group --- VG Name rl System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 5 ....... VG Size <108.41 GiB PE Size 4.00 MiB Total PE 27752 Alloc PE / Size 25193 / 98.41 GiB Free PE / Size 2559 / <10.00 GiB ...<SNIP>...
问题
使用您的
vgdisplay
输出,记下您系统上的更改。新的“Free PE / Size”值是多少? -
从现有
rl
卷组中删除新的 PV (/dev/loop1)。使用vgreduce
命令,键入[root@localhost ~]# vgreduce rl /dev/loop1
输出
Removed "/dev/loop0" from volume group "rl"
创建新的卷组¶
在本节中,您将创建一个全新的独立卷组,名为“scratch”。scratch VG 的空间将完全来自 /dev/loop1
伪块设备。
-
创建新的
scratch
空间。键入[root@localhost ~]# vgcreate scratch /dev/loop1 Physical volume "/dev/loop1" successfully created. Volume group "scratch" successfully created
-
运行
vgdisplay
命令以查看您的更改。键入[root@localhost ~]# vgdisplay scratch --- Volume group --- VG Name scratch System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 1 ....... VG Size <10.00 GiB PE Size 4.00 MiB Total PE 2559 Alloc PE / Size 0 / 0 Free PE / Size 2559 / <10.00 GiB VG UUID nQZPfK-bo7E-vOSR*** ...<SNIP>...
问题
查看您的
vgdisplay
输出。 “Free PE / Size”的值是多少?这些值与您将/dev/loop1
PV 添加到现有rl
卷组的上一节有何不同?
创建逻辑卷¶
由于我们向 rl
卷组 (VG) 添加了额外的可用空间,因此现在可以添加一个逻辑卷,该逻辑卷在格式化后可用于存储数据。
-
使用
lvdisplay
命令查看当前配置的逻辑卷。键入[root@localhost ~]# lvdisplay
问题
根据您的输出回答以下问题
-
定义了多少逻辑卷 (LV)?
-
LV 的名称是什么?
-
您的系统上各种 LV 用于什么?
-
-
使用
lvs
命令以类似方式显示逻辑卷,但这次过滤输出以显示特定字段。过滤以查看 lv_name (逻辑卷名)、lv_size (逻辑卷大小)、lv_path、vg_name (卷组名) 字段。键入[root@localhost ~]# lvs -o lv_name,lv_size,lv_path,vg_name LV LSize Path VG home <29.68g /dev/rl/home rl root <60.79g /dev/rl/root rl swap <7.95g /dev/rl/swap rl
注意
lv_name = 逻辑卷名,lv_size = 逻辑卷大小,lv_path = 逻辑卷路径,vg_name = 卷组名。
-
在新的
scratch
VG 上,使用lvcreate
命令创建一个名为“scratch2”的新逻辑卷。将scratch2
的大小设置为 2GB。键入[root@localhost ~]# lvcreate -L 2G --name scratch2 scratch Logical volume "scratch2" created.
-
创建第二个逻辑卷“scratch3”。这次使用
scratch
卷组中所有剩余的可用空间。键入[root@localhost ~]# lvcreate -l 100%FREE --wipesignatures y --yes --zero y --name scratch3 scratch Logical volume "scratch3" created.
-
再次使用
lvdisplay
命令查看新的 LV。
删除 LV、VG 和 PV¶
本节将介绍如何删除您在上节中分配给现有 rl
VG 的 /dev/loop1
PV。
-
删除名为 scratch2 的逻辑卷。键入
[root@localhost ~]# lvremove -f /dev/rl/scratch2 Logical volume "scratch2" successfully removed.
-
通过运行以下命令删除 scratch3 逻辑卷
[root@localhost ~]# lvremove -f /dev/rl/scratch3
-
在删除相关卷后,您可以减小
rl
VG 的大小以使其一致。键入[root@localhost ~]# vgreduce --removemissing rl
-
删除
/dev/loop1
PV 上的任何 LVM 标签。键入[root@localhost ~]# pvremove /dev/loop1 Labels on physical volume "/dev/loop1" successfully wiped.
练习 2¶
为了使操作系统能够使用先前创建的传统分区和 LVM 风格的卷,您需要对其创建文件系统。将文件系统写入设备也称为格式化磁盘。
本练习涵盖文件系统的创建以及一些常用文件系统维护工具的使用。
创建 VFAT 文件系统¶
在这里,您将使用 mke2fs
程序在新的 /dev/loop0p1 分区上创建 vFAT 文件系统。
-
使用
mkfs.vfat
实用程序在/dev/loop0p1
卷上创建 vfat 类型文件系统。键入[root@localhost ~]# mkfs.vfat /dev/loop0p1
输出
mkfs.fat 4.*
-
使用
lsblk
查询系统有关块设备的有趣信息。键入[root@localhost ~]# lsblk -f /dev/loop0
输出
NAME FSTYPE LABEL UUID MOUNTPOINT loop0 └─loop0p1 vfat 658D-4A90
创建 EXT4 文件系统¶
为了使操作系统能够使用先前创建的逻辑卷,您需要对其创建文件系统。将文件系统写入设备也称为格式化磁盘。
在这里,您将使用 mke2fs
程序在新的 scratch2 卷上创建 EXT4 文件系统。
-
使用
mkfs.ext4
实用程序在/dev/scratch/scratch2
卷上创建 EXT4 类型的文件系统。键入[root@localhost ~]# mkfs.ext4 /dev/scratch/scratch2 ...<SNIP>... Writing superblocks and filesystem accounting information: done
-
使用
lsblk
查询系统有关 scratch2 卷的有趣信息。键入[root@localhost ~]# lsblk -f /dev/scratch/scratch2 NAME FSTYPE LABEL UUID MOUNTPOINT scratch-scratch2 ext4 6689b6aa****
创建 XFS 文件系统¶
在这里,您将使用 mke2fs
程序在新的 scratch3 卷上创建 XFS 文件系统。
-
使用
mkfs.xfs
实用程序在/dev/rl/scratch3
卷上创建 XFS 类型的文件系统。键入[root@localhost ~]# mkfs.xfs /dev/scratch/scratch3 meta-data=/dev/scratch/scratch3 isize=512 agcount=4, agsize=524032 blks ...<SNIP>... Discarding blocks...Done.
-
使用
lsblk
查询系统有关 scratch2 卷的有趣信息。键入[root@localhost ~]# lsblk -f /dev/scratch/scratch3
输出
NAME FSTYPE LABEL UUID MOUNTPOINT scratch-scratch3 xfs 1d1ac306***
使用 dumpe2fs
、tune2fs
、lsblk
和 fsck
¶
这里我们将介绍一些用于维护文件系统、修复文件系统问题、调试文件系统问题等的常用文件系统实用程序。
-
查找 scratch2 卷上当前“最大挂载次数”的值。键入
[root@localhost ~]# dumpe2fs /dev/scratch/scratch2 | grep -i "maximum mount count" dumpe2fs 1.4*** Maximum mount count: -1
问题
- “最大挂载次数”的目的是什么?
- 您的
root
卷 (/dev/rl/root) 的最大挂载次数是多少?
-
使用
tune2fs
命令调整/设置/dev/scratch/scratch2
卷上文件系统检查之间的“最大挂载次数”值为零。键入[root@localhost ~]# tune2fs -c 0 /dev/scratch/scratch2 tune2fs 1.*.* Setting maximal mount count to -1
-
使用
fsck
命令检查 scratch2 文件系统。键入[root@localhost ~]# fsck -Cfp /dev/scratch/scratch2 fsck from util-linux 2.* /dev/mapper/scratch-scratch2: 11/131072 files (0.0% non-contiguous), 26156/524288 blocks
-
使用
tune2fs
程序为新的 EXT4 卷创建卷标签。键入[root@localhost root]# tune2fs -L scratch2 /dev/scratch/scratch2
-
使用
lsblk
查看有关/dev/scratch/scratch2
的信息。键入[root@localhost ~]# lsblk -o name,size,label /dev/scratch/scratch2 NAME SIZE LABEL scratch-scratch2 2G scratch2
-
检查 scratch3 卷上的 XFS 文件系统。键入
[root@localhost ~]# fsck -Cfp /dev/scratch/scratch3 fsck from util-linux 2.* /usr/sbin/fsck.xfs: XFS file system.
练习 3¶
之前的练习介绍了如何准备一个块/存储设备供系统使用。在经历了分区、格式化等所有过程之后,使文件系统可供用户存储数据的最后一步称为挂载。
本练习将介绍如何 mount
和 umount
我们在上一个练习中创建的文件系统。
mount
命令¶
mount
命令用于将设备上创建的文件系统附加到文件层次结构。
挂载 VFAT 文件系统¶
-
以具有管理员特权的用户身份登录系统。
-
创建一个名为
/mnt/10gb-scratch1-partition
的文件夹。此文件夹将用作 scratch1 文件系统的挂载点。键入[root@localhost ~]# mkdir /mnt/10gb-scratch1-partition
-
挂载分区。键入
[root@localhost ~]# mount /dev/loop0p1 /mnt/10gb-scratch1-partition
-
使用
mount
命令显示系统上的所有 VFAT 文件系统。使用 grep 过滤输出以查找单词scratch
。键入[root@localhost ~]# mount -t vfat | grep scratch
-
使用
df
命令查看服务器上文件系统磁盘空间使用情况的报告。键入[root@localhost ~]# df -ht vfat | grep scratch
-
使用
--bind
选项与mount
命令一起,使/mnt/10gb-scratch1-partition
文件系统也出现在更简单或更友好的名称/路径下,例如/mnt/scratch1
。键入[root@localhost ~]# mount --bind /mnt/10gb-scratch1-partition /mnt/scratch1
-
再次使用
df
命令查看绑定挂载的效果。
挂载 EXT4 文件系统¶
-
创建一个名为
/mnt/2gb-scratch2-volume
的文件夹。此文件夹将用作 scratch2 卷的挂载点。键入[root@localhost ~]# mkdir /mnt/2gb-scratch2-volume
-
挂载分区。键入
[root@localhost ~]# mount /dev/scratch/scratch2 /mnt/2gb-scratch2-volume
-
使用
mount
命令显示系统上的所有 EXT4 文件系统。键入[root@localhost ~]# mount -t ext4
-
确保挂载点具有正确的权限,允许所有系统用户写入挂载的卷,方法是运行
[root@localhost ~]# chmod 777 /mnt/2gb-scratch2-volume
-
使用
df
命令查看服务器上文件系统磁盘空间使用情况的报告。
挂载 XFS 文件系统¶
-
创建一个名为
/mnt/8gb-scratch3-volume
的文件夹。这将是 scratch3 文件系统的挂载点。键入[root@localhost ~]# mkdir /mnt/8gb-scratch3-volume
-
挂载分区。键入
[root@localhost ~]# mount /dev/scratch/scratch3 /mnt/8gb-scratch3-volume
-
使用
mount
命令显示系统上的所有 XFS 文件系统。键入[root@localhost ~]# mount -t xfs | grep scratch
-
使用
df
命令查看服务器上文件系统磁盘空间使用情况的报告。
使文件系统挂载持久化¶
-
使用
cat
命令查看/etc/fstab
文件的当前内容。 -
在进行任何更改之前,备份
/etc/fstab
文件。键入[root@localhost ~]# cp /etc/fstab /etc/fstab.copy
-
使用文本编辑器,小心地将以下新条目附加到
/etc/fstab
文件中,用于我们之前创建的 3 个文件系统。新条目是
/dev/loop0p1 /mnt/10gb-scratch1-partition auto defaults,nofail 0 0 /dev/scratch/scratch2 /mnt/2gb-scratch2-volume ext4 defaults,nofail 0 0 /dev/scratch/scratch3 /mnt/2gb-scratch3-volume xfs defaults,nofail 0 0
我们将使用下面的 BASH heredoc 方法创建条目。仔细键入
[root@localhost ~]# cat >> /etc/fstab << EOF /dev/loop0p1 /mnt/10gb-scratch1-partition auto defaults,nofail 0 0 /dev/scratch/scratch2 /mnt/2gb-scratch2-volume ext4 defaults,nofail 0 0 /dev/scratch/scratch3 /mnt/8gb-scratch3-volume xfs defaults,nofail 0 0 EOF
-
使用实际磁盘或存储设备,之前的步骤足以使系统自动正确挂载所有新文件系统并应用任何特殊的挂载选项。
但是,由于我们在本次实验中使用了特殊的伪块设备 (环回设备),因此我们必须完成一个额外的关键任务,以确保系统重启后能自动重新创建正确的环回设备。
为此,我们将创建一个自定义 systemd 服务单元来帮助完成此任务。
使用您熟悉的任何文本编辑器创建
/etc/systemd/system/loopdevices.service
文件。在此文件中输入以下文本。
[Unit] Description=Activate loop devices DefaultDependencies=no After=systemd-udev-settle.service Before=lvm2-activation.service Wants=systemd-udev-settle.service [Service] ExecStart=losetup -P /dev/loop0 /tmp/10G-fake-disk.img ExecStart=losetup -P /dev/loop1 /tmp/10G-fake-lvm-disk.img Type=oneshot [Install] WantedBy=local-fs.target
确保保存对文件的更改。
-
使用
systemctl
命令启用新的 loopdevice 服务。键入[root@localhost ~]# systemctl enable loopdevices.service
-
尝试启动服务以确保它成功启动。键入
[root@localhost ~]# systemctl start loopdevices.service
如果它没有报错启动,您可以继续进行下一步,在那里您将进行实际测试,即重启系统。
-
重启系统并确保一切正常,并且新的文件系统已自动挂载。
练习 4¶
前言
出于某种原因,名为“unreasonable”的用户决定在与他人共享的系统上创建一个极其巨大的文件!!
该文件占用了本地硬盘上的大量空间。
作为管理员,您可以找到并删除该文件,然后继续进行一天的工作,并希望这只是一次性的事件,或者,您可以找到并删除该文件以释放磁盘空间,并制定计划以防止再次发生。我们将在后续练习中尝试后一种解决方案。
在此期间 -
Unreasonable 用户再次作案!
创建大文件¶
(请在您的伙伴系统上执行此练习)
Unreasonable 用户意外注意到服务器上新增了 scratch 文件系统!“这太棒了!”他对自己说。
然后他继续用任意大小的文件填满该卷。
-
以
unreasonable
用户身份登录系统。 -
检查系统以查看是否有任何可以滥用的新文件系统。键入
[unreasonable@localhost ~]$ df -h
-
立即继续用垃圾填满可用的共享文件系统。键入
[unreasonable@localhost ~]$ dd if=/dev/zero \ of=/mnt/2gb-scratch2-volume/LARGE-USELESS-FILE.tar bs=10240
输出
dd: error writing '/mnt/2gb-scratch2-volume/LARGE-USELESS-FILE.tar': No space left on device 187129+0 records in 187128+0 records out 1916194816 bytes (1.9 GB, 1.8 GiB) copied, 4.99021 s, 384 MB/s
-
启动
dd
进程后,去散步,当命令完成或因无法继续而报错时返回。或者去找管理员抱怨系统磁盘空间已满。 -
探索系统中可能存在的更不合理/无意义/令人恼火的事情。您是 unreasonable user。
练习 5¶
配额¶
实施和执行磁盘配额的使用提供了一种确保系统有足够磁盘空间以及用户保持在其分配磁盘空间内的方法。在实施配额之前,您需要
- 决定要在哪些分区或卷上实施磁盘配额。
- 决定在哪个级别强制执行配额 - 即按用户、按组或两者都按。
- 决定您的软限制和硬限制将是多少。
- 决定宽限期将是多少 (即,是否会有任何宽限期)。
硬限制¶
硬限制定义了用户或组可以使用的磁盘空间的绝对最大量。一旦达到此限制,就无法再使用磁盘空间。
软限制¶
软限制定义了可以使用的磁盘空间的最大量。但是,与硬限制不同,软限制可以在一定时间内被超过。这段时间称为宽限期。
宽限期¶
宽限期是指可以超过软限制的时间。宽限期可以用秒、分钟、小时、天、周或月来表示,从而为系统管理员提供了极大的自由度来确定用户有多少时间可以将磁盘使用量降至其软限制以下。
这些是实施配额的高级步骤。
- 安装配额软件
- 修改“/etc/fstab”文件
- 重新挂载文件系统
- 运行 quotacheck
- 分配配额
您将使用的命令是
quotacheck
:
用于检查和修复配额文件的实用程序。
quotacheck [-gucbfinvdmMR] [-F <quota-format>] filesystem|-a
-u, --user check user files
-g, --group check group files
-c, --create-files create new quota files
-b, --backup create backups of old quota files
-f, --force force check even if quotas are enabled
-i, --interactive interactive mode
-n, --use-first-dquot use the first copy of duplicated structure
-v, --verbose print more information
-d, --debug print even more messages
-m, --no-remount do not remount filesystem read-only
-M, --try-remount try remounting filesystem read-only,
continue even if it fails
-R, --exclude-root exclude root when checking all filesystems
-F, --format=formatname check quota files of specific format
-a, --all check all filesystems
edquota
:
编辑用户配额的工具
SYNOPSIS
edquota [ -p protoname ] [ -u | -g | -P ] [ -rm ] [ -F format-name ] [ -f filesystem ] username | groupname | projectname...
edquota [ -u | -g | -P ] [ -F format-name ] [ -f filesystem ] -t
edquota [ -u | -g | -P ] [ -F format-name ] [ -f filesystem ] -T username | groupname | projectname...
repquota
:
报告配额的实用程序。
Usage:
repquota [-vugsi] [-c|C] [-t|n] [-F quotaformat] [-O (default | xml | csv)] (-a | mntpoint)
-v, --verbose display also users/groups without any usage
-u, --user display information about users
-g, --group display information about groups
-P, --project display information about projects
-s, --human-readable show numbers in human friendly units (MB, GB, ...)
-t, --truncate-names truncate names to 9 characters
-p, --raw-grace print grace time in seconds since epoch
-n, --no-names do not translate uid/gid to name
-i, --no-autofs avoid autofs mountpoints
-c, --cache translate big number of ids at once
-C, --no-cache translate ids one by one
-F, --format=formatname report information for specific format
-O, --output=format format output as xml or csv
-a, --all report information for all mount points with quotas
quotaon
和 quotaoff
用于启用和禁用文件系统配额的工具
SYNOPSIS
quotaon [ -vugfp ] [ -F format-name ] filesystem...
quotaon [ -avugPfp ] [ -F format-name ]
quotaoff [ -vugPp ] [ -x state ] filesystem...
quotaoff [ -avugp ]
安装配额软件¶
-
以 root 用户身份登录时,首先检查
quota-*.rpm
包是否已安装在您的系统上。键入[root@localhost ~]# rpm -q quota quota-*
问题
您的输出是什么?
-
如果您系统中未安装 quote 包,请使用
dnf
进行安装。
设置和配置配额¶
-
您已决定在“/dev/rl/scratch2"卷上实施 EXT4 风格的配额。您还决定在用户和组级别都实施配额。
-
使用您选择的编辑器查看
/etc/fstab
文件。以下是我们在对文件进行任何更改之前文件中的相关条目。[root@localhost ~]# grep scratch2 /etc/fstab
输出
/dev/scratch/scratch2 /mnt/2gb-scratch2-volume ext4 defaults 0 0
-
备份
/etc/fstab
。 -
作为实施配额的一部分,需要在 scratch2 卷条目中添加一些新的与配额相关的挂载选项。scratch2 卷条目需要更新为文件中的新行
/dev/scratch/scratch2 /mnt/2gb-scratch2-volume ext4 defaults,usrquota,grpquota 0 0
您可以使用您喜欢的文本编辑器进行更改,或使用下一步所示的
sed
实用程序。 -
使用
sed
实用程序搜索要更改的行并在原地进行更新。键入[root@localhost ~]# sudo sed -i \ '/^\/dev\/scratch\/scratch2/ s|.*|/dev/scratch/scratch2 /mnt/2gb-scratch2-volume ext4 defaults,usrquota,grpquota 0 0|'\ /etc/fstab
-
再次使用
grep
快速查看文件,以确保在/etc/fstab
中进行了正确的更改。 -
为了使对
/etc/fstab
的更改生效,您需要执行更多操作。首先重新加载 systemd-daemon,运行[root@localhost ~]# systemctl daemon-reload
-
接下来重新挂载相关文件系统。键入
[root@localhost ~]# mount -o remount /mnt/2gb-scratch2-volume
-
通过检查
/proc/mounts
文件来验证新的挂载选项是否已应用。键入[root@localhost ~]# cat /proc/mounts | grep scratch2
输出
/dev/mapper/rl-scratch2 /mnt/2gb-scratch2-volume ext4 rw,relatime,quota,usrquota,grpquota 0 0
技巧
您还可以使用
mount
命令检查任何文件系统正在使用的挂载选项。对于前面的示例,您可以运行以下命令来查看 ext4 格式的 scratch2 卷的挂载选项输出[root@localhost ~]# mount -t ext4 | grep scratch2
/dev/mapper/scratch-scratch2 on /mnt/2gb-scratch2-volume type ext4 (rw,relatime,quota,usrquota,grpquota)
问题
写下单独
unmount
给定文件系统然后mount
回来的命令? -
您现在需要使文件系统准备好支持配额。创建配额文件并生成每个文件系统的当前磁盘使用情况表。键入
[root@localhost ~]# quotacheck -avcug
输出
.... quotacheck: Scanning /dev/mapper/scratch-scratch2 [/mnt/2gb-scratch2-volume] done ...<SNIP>... quotacheck: Old file not found. quotacheck: Old file not found.
问题
上述命令执行后,您会在“/mnt/2gb-scratch2-volume"目录下发现两个新文件。在此列出文件?
技巧
要获得配额文件系统的最新状态,您应该在文件系统关闭配额时定期运行
quotacheck -avcug
命令。 -
要启用“/etc/fstab”中指定的所有文件系统的用户和组配额,键入
[root@localhost ~]# quotaon -av
为用户分配配额¶
您决定为系统上的每个用户设置 90 MB 的软限制和 100 MB 的硬限制,宽限期为 5 分钟。
这意味着我们应用配额的所有用户都不能超过 100 MB 的硬限制,但他们有大约 5 分钟的时间可以超过 90 MB 的软限制但仍保持在硬限制范围内。
-
您将使用一个原型用户创建限制。名为“me”的用户将是您的原型用户。使用
edquota
命令创建限制。键入[root@serverXY root]# edquota -u me
以上命令将打开您的默认编辑器,内容如下
Disk quotas for user me (uid 1001): Filesystem blocks soft hard inodes soft hard /dev/mapper/scratch-scratch2 0 0 0 0 0 0
修改/编辑以上文件 (第三行) 以反映您想要的限制。将文件更改为
Disk quotas for user me (uid 1001): Filesystem blocks soft hard inodes soft hard /dev/mapper/scratch-scratch2 0 90000 100000 0 0 0
保存对文件的更改并关闭它。
-
您将使用
edquota
命令的-t
选项创建宽限期。键入[root@serverXY root]# edquota -t
此命令将打开您的默认编辑器,内容与下面显示的类似
Grace period before enforcing soft limits for users: Time units may be: days, hours, minutes, or seconds Filesystem Block grace period Inode grace period /dev/mapper/scratch-scratch2 7days 7days
编辑以上文件 (第四行) 以反映您想要的宽限期。
将文件更改为
Grace period before enforcing soft limits for users: Time units may be: days, hours, minutes, or seconds Filesystem Block grace period Inode grace period /dev/mapper/scratch-scratch1 5minutes 7days
-
接下来,将为原型用户“me”配置的设置应用于用户“ying”和“unreasonable”。键入
[root@localhost ~]# edquota -p me -u ying unreasonable
-
要获取所有已启用配额的状态报告,键入
[root@localhost ~]# repquota /mnt/2gb-scratch2-volume
输出
*** Report for user quotas on device /dev/mapper/scratch-scratch2 Block grace time: 00:05; Inode grace time: 7days Block limits File limits User used soft hard grace used soft hard grace ---------------------------------------------------------------------- root -- 20 0 0 2 0 0 unreasonable +- 1871288 90000 100000 00:04 1 0 0
问题
从上面用户
unreasonable
的“grace”列的输出中,该用户还剩多少宽限期? -
从报告中,您发现 unreasonable 用户已超出服务器上的配额限制。您找到造成问题的那个文件,帮助 unreasonable 用户“清理”它,并使其恢复合规。键入
[root@localhost ~]# rm -rf /mnt/2gb-scratch2-volume/LARGE-USELESS-FILE.tar
-
使用
su
命令临时假设unreasonable
用户的身份,并尝试作为该用户创建其他文件或目录。键入[root@localhost ~]# su - unreasonable
-
以 unreasonable 用户身份登录时,您会检查发现您在先前练习中创建的
/mnt/2gb-scratch2-volume/LARGE-USELESS-FILE.tar
文件丢失了!恼怒的您决定重新创建它。键入[unreasonable@localhost ~]$ dd if=/dev/zero of=/mnt/2gb-scratch2-volume/LARGE-USELESS-FILE.tar bs=10240
输出
...<SNIP>... dd: error writing '/mnt/2gb-scratch2-volume/LARGE-USELESS-FILE.tar': Disk quota exceeded 10001+0 records in 10000+0 records out 102400000 bytes (102 MB, 98 MiB) copied, 0.19433 s, 527 MB/s
嗯……真有趣,您咕哝着。
-
尝试在 /mnt/2gb-scratch2-volume/ 下创建一个名为 test 的文件夹。一个空文件夹不应占用或使用大量磁盘空间,因此您键入
[unreasonable@localhost ~]$ mkdir /mnt/2gb-scratch2-volume/test mkdir: cannot create directory ‘/mnt/2gb-scratch2-volume/test’: Disk quota exceeded
-
检查 LARGE-USELESS-FILE.tar 文件的大小。键入
[unreasonable@localhost ~]$ ls -l /mnt/2gb-scratch2-volume/LARGE-USELESS-FILE.tar -rw-rw-r-- 1 unreasonable unreasonable 102400000 Oct 5 19:37 /mnt/2gb-scratch2-volume/LARGE-USELESS-FILE.tar
问题
发生了什么?
-
由于无知而沮丧的 unreasonable 用户键入
[unreasonable@localhost ~]$ man quota
注意
“unreasonable”用户将被迫处理他创建的“LARGE-USELESS-FILE.tar”。在用户将其总文件大小降至其限额以下之前,他无法再做太多其他事情。
-
Linux 文件系统实验完成。
作者:Wale Soyinka
贡献者:Steven Spencer, Ganna Zhyrnova