实验室 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
完成特定任务。
首先将循环块设备的名称作为参数传递给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”的全新独立卷组。该暂存卷组将完全从 /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
输出。“可用 PE / 大小”的值是什么?这些值与前面将/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
使用带有--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
使用文本编辑器,小心地将以下新条目添加到我们之前创建的 3 个文件系统的/etc/fstab
文件中。新条目如下:
/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@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 [-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
修改/编辑上述文件(第 3 行)以反映您想要的限制。将文件更改为: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
编辑上述文件(第 4 行)以反映您想要的宽限期。
将文件更改为: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
宽限期列的输出中,用户还剩下多少宽限期?
从报告中,您注意到不合理的用户已超出其在服务器上的配额限制。您搜索有问题的文件并帮助不合理的用户“清理”并使其恢复合规。输入:[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@localhost ~]$ man quota
笔记
“不讲道理”的用户将被迫对他创建的“LARGE-USELESS-FILE.tar”采取一些措施。除非该用户将他的总文件大小控制在他的限制之内,否则他将无法做很多其他事情。
本实验在 Linux 文件系统上全部完成。
作者:瓦莱·索因卡
撰稿人:Steven Spencer、Ganna Zhyrnova