实验 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
¶
检查 DEVICE 上的 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
)
在本练习中,您将在硬盘上创建其他分区。在初始安装过程中,您留了一些可用空间。您将在此空间上创建分区。
对磁盘进行分区允许将磁盘视为一组独立的存储区域。
分区还可以使备份更容易,并有助于限制和隔离潜在的问题区域。
硬盘空间不是无限的,作为管理员,您的一项职责就是管理有限的可用空间。例如,限制用户存储个人文件的磁盘上的总存储区域的一种简单方法是为用户的 home 目录创建一个单独的分区(也可以使用配额)。
探索块存储设备¶
您将使用 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):
通过在
fdisk
提示符下键入m
来显示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
程序在新的scrtach2卷上创建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)的最大挂载次数是多少?
在
/dev/scratch/scratch2
卷上的文件系统检查之间,将最大挂载次数值调整/设置为零。使用tune2fs
命令。键入[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
使用
mount
命令的--bind
选项,使/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”的用户决定在一个与其他用户共享的系统上创建一个极其大的文件!!
该文件占用了本地硬盘上的大量空间。
作为管理员,您可以找到并删除有问题的文件并继续您的工作,希望这只是一个偶然事件,或者您可以找到并删除文件以释放磁盘空间并制定一个计划来防止再次发生。我们将在后面的练习中尝试后一种解决方案。
在此期间 -
不合理的用户再次出现!
创建大型文件¶
(从您的合作伙伴系统执行此练习)
不合理的用户意外地注意到,在服务器上隔夜创建了新的**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
进程后,出去散步,等到命令完成或因无法继续执行而出错时再回来。或者去找管理员抱怨系统上的磁盘空间已满。探索可以在系统上执行的其他不合理/无意义/令人讨厌的操作。您是**不合理的用户**。
练习 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卷条目需要更新到此处的newLine
/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
的宽限期列下,该用户还有多少宽限期?从报告中,您注意到 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