在新增磁盘后要对其进行分区,分区后才可以去存储数据
MBR分区格式:比较古老的分区格式,只能划分4个主分区,如果四个分区划分完成后,硬盘空间还有剩余,那剩余的分区也不能够使用。后来新增加扩展分区(容器)功能,可在扩展分区内划分出更多的逻辑分区,最大支持2.2.T磁盘容量
fdisk 设备路径
命令去进行MBR磁盘分区GPT分区格式:可以划分128个主分区,最大支持18EB磁盘容量
Linux来说无论有几个分区,分给哪一目录使用,他归根结底只有一个根目录,一个独立且唯一的文件结构,Linux中每个分区都是用来组成整个文件系统的一部分。
当我们拿到一张大白纸,首先为了使用方便要裁剪,然后为了书写工整要画格。
“白纸”表示原始的硬盘,“裁剪”意味着分区,“画格”意味着格式化,最后写入数据,即新安装一块硬盘设备后,为了更充分、安全的利用硬盘空间首先要进行分区,然后格式化,最后挂载使用。
主分区、扩展分区和逻辑分区的区别
主分区
扩展分区
扩展分区是不能使用的,它只是做为逻辑分区的容器存在的,先创建一个扩展分区,在扩展分区之上创建逻辑分区;
除去主分区所占用的容量以外,剩下的容量就被认定为扩展分区(也可以这么讲:一块硬盘除去主分区外的容量后,如果对剩下的容量进行了再分区,那么,这个再分区就是扩展分区)
扩展分区是不能进行格式化的,扩展分区本身也是不能使用的,他的作用仅仅是逻辑分区的容器
/dev/nvme0n2p3 1640448 10485759 8845312 4.2G 5 Extended
major minor #blocks name
259 0 62914560 nvme0n1
259 1 1048576 nvme0n1p1
259 2 61864960 nvme0n1p2
259 3 5242880 nvme0n2
259 5 307200 nvme0n2p1
259 4 512000 nvme0n2p2
259 6 0 nvme0n2p3
# 扩展分区的容量从我们的角度看是0
逻辑分区
扩展分区可以分成若干个逻辑分区。他们的关系是包含的关系,所有的逻辑分区都是扩展分区的一部分——扩展分区如果不再进行分区了,那么扩展分区就是逻辑分区了
逻辑分区在使用上和主分区完全相同
/dev/nvme0n2p3 1640448 10485759 8845312 4.2G 5 Extended
/dev/nvme0n2p5 1642496 3076095 1433600 700M b W95 FAT32
查看分区类型
parted命令
{24-06-02 23:07}bogon:~ root# parted /dev/sdc print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 2149MB 2147MB primary
2 2149MB 4296MB 2147MB primary
3 4296MB 6443MB 2147MB primary
4 6443MB 21.5GB 15.0GB extended
5 6445MB 8592MB 2147MB logical
6 8593MB 10.7GB 2147MB logical
7 10.7GB 12.9GB 2147MB logical
8 12.9GB 15.0GB 2147MB logical
9 15.0GB 21.5GB 6436MB logical
当我们在分区的时候,默认使用MBR进行分区
磁盘分区
磁盘首先要进行分区才可以进行挂载
给虚拟机添加一块硬盘
[root@localhost ~]# fdisk -l | grep nv
Disk /dev/nvme0n1: 60 GiB, 64424509440 bytes, 125829120 sectors
/dev/nvme0n1p1 * 2048 2099199 2097152 1G 83 Linux
/dev/nvme0n1p2 2099200 125829119 123729920 59G 8e Linux LVM
Disk /dev/nvme0n2: 5 GiB, 5368709120 bytes, 10485760 sectors # 新增的硬盘
对新增的磁盘进行分区
[root@localhost ~]# fdisk /dev/nvme0n2
# 进入磁盘分区界面
Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p):
Using default response p.
Partition number (1-4, default 1):
First sector (2048-10485759, default 2048): # 由于磁盘的引导文件会占用空间,因此第一个扇区的起始位置是从2048开始
Last sector, +sectors or +size{K,M,G,T,P} (2048-10485759, default 10485759): +200M
Created a new partition 1 of type 'Linux' and of size 200 MiB.
The signature will be removed by a write command.
Command (m for help): p
Disk /dev/nvme0n2: 5 GiB, 5368709120 bytes, 10485760 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: 0x7a7b9c31
Device Boot Start End Sectors Size Id Type
/dev/nvme0n2p1 2048 411647 409600 200M 83 Linux
Filesystem/RAID signature on partition 1 will be wiped.
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p):
Using default response p.
Partition number (2-4, default 2):
First sector (411648-10485759, default 411648):
Last sector, +sectors or +size{K,M,G,T,P} (411648-10485759, default 10485759): +500M
Created a new partition 2 of type 'Linux' and of size 500 MiB.
The signature will be removed by a write command.
Command (m for help): p
Disk /dev/nvme0n2: 5 GiB, 5368709120 bytes, 10485760 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: 0x7a7b9c31
Device Boot Start End Sectors Size Id Type
/dev/nvme0n2p1 2048 411647 409600 200M 83 Linux
/dev/nvme0n2p2 411648 1435647 1024000 500M 83 Linux
Filesystem/RAID signature on partition 1 will be wiped.
Filesystem/RAID signature on partition 2 will be wiped.
#保存退出
重新加载硬盘,因为内核在读取硬盘的状态时,是读取开机时的状态,我们后续对硬盘进行了分区操作后,需要强制重启硬盘,让内核重新读取硬盘的信息
[root@localhost ~]# partprobe /dev/nvme0n2
[root@localhost ~]# cat /proc/partitions
major minor #blocks name
259 0 62914560 nvme0n1
259 1 1048576 nvme0n1p1
259 2 61864960 nvme0n1p2
259 3 5242880 nvme0n2
259 6 204800 nvme0n2p1
259 7 512000 nvme0n2p2
GPT分区使用和MBR相同
{24-06-02 23:21}bogon:~ root# fdisk /dev/sdd
Command (m for help): g
Created a new GPT disklabel (GUID: EEA1265E-7679-0446-BFB7-A3DD6605FB7D).
Command (m for help): n
Partition number (1-128, default 1):
将磁盘分区完成后,进行格式化(赋予空间文件系统的过程)
[root@localhost ~]# mkfs.ext4 /dev/nvme0n2p1
mke2fs 1.45.6 (20-Mar-2020)
Creating filesystem with 204800 1k blocks and 51200 inodes
Filesystem UUID: c93d6606-57de-4d63-949a-db4034c47d3b
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
[root@localhost ~]# mkfs.xfs /dev/nvme0n2p2
meta-data=/dev/nvme0n2p2 isize=512 agcount=4, agsize=32000 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1 bigtime=0 inobtcount=0
data = bsize=4096 blocks=128000, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=1368, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
检查是否格式化成功
{24-06-02 18:30}bogon:~ root# blkid /dev/sdb2
/dev/sdb2: UUID="2c55e9d9-9f5c-4536-97af-c821625328ec" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="2af2b43a-02"
创建挂载点
[root@localhost ~]# mkdir /mnt/etx /mnt/xfs
通过mount命令进行挂载
[root@localhost ~]# mount -a
进行永久挂载,在/etc/fstab中写入
设备路径 挂载点目录 文件系统类型 参数(defaults)
/dev/nvme0n2p1 /mnt/ext ext4 defaults 0 0
/dev/nvme0n2p2 /mnt/xfs xfs defaults 0 0
查看挂载情况
[root@localhost ~]# df -TH | grep -v tmp
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/rhel-root xfs 40G 21G 20G 52% /
/dev/sr0 iso9660 14G 14G 0 100% /mnt/cdrom
/dev/nvme0n2p2 xfs 519M 31M 489M 6% /mnt/xfs
/dev/mapper/rhel-home xfs 20G 170M 20G 1% /home
/dev/nvme0n1p1 xfs 1.1G 208M 856M 20% /boot
/dev/nvme0n2p1 ext4 198M 15k 183M 1% /mnt/ext
至此,挂载成功
文件系统:数据在空间中的存放规则
Windows默认文件系统:NTFS
分类
对于不同的文件系统来说,虽然在使用中没有过大的差距,但是依旧存在着一些小的不同
对于ext4文件系统来说,我们在挂载完成收,就会自动生成一个目录
[root@localhost ~]# ll /mnt/ext/
total 12
drwx------. 2 root root 12288 Mar 29 02:28 lost+found
但是对于xfs文件系统来说,就不会生成
[root@localhost ~]# ll /mnt/xfs
total 0
当我格式化好一个分区后,我想重新格式化文件系统,对于xfs和ext4文件系统的提示是不同的
# 现在是xfs文件系统,我想重新格式化为ext4格式
{24-06-02 18:39}bogon:~ root# mkfs.ext4 /dev/sdb2
mke2fs 1.45.6 (20-Mar-2020)
/dev/sdb2 contains a xfs file system
Proceed anyway? (y,N) y
# 现在是ext4文件系统,我想格式化为xfs文件系统,使用-f强制重新格式化
{24-06-02 18:40}bogon:~ root# mkfs.xfs /dev/sdb2
mkfs.xfs: /dev/sdb2 appears to contain an existing filesystem (ext4).
mkfs.xfs: Use the -f option to force overwrite.
{24-06-02 18:40}bogon:~ root# mkfs.xfs -f /dev/sdb2
如果目录没有被挂载的文件系统,那么默认占用的是根分区的容量
例如/opt目录
若/opt目录下的数据特别大,我们该如何实现数据的迁移,来缓解根分区的负担
# 我们首先将一块新的磁盘挂载
mount /dev/sdb1 /mypart1
# 然后将/opt的数据移动到/mypart1下,此时/opt中就没有数据了,以此减轻了根分区的负担,但是有一些服务的相关配置文件会在/opt目录下。现在我们将数据移走了,服务会找不到配置文件,导致服务无法启动
mv /opt/* /mypart1
# 现在/opt中的数据已经在sdb磁盘的sdb1分区中,我们将sdb1取消挂载
umount /mypart1
# 将sdb1挂载到/opt目录下
mount /dev/sdb1 /opt
# 此时/opt目录下又重新恢复了数据
{24-06-02 22:32}bogon:~ root# ls /opt
10.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt lost+found
# 至此完成了数据的迁移
在内存不够大时,需要开启Swap,使用一部分硬盘,增加虚拟内存,解决内存容量不足的情况
当内存不足的时候,一部分进程会被杀死
通过free -mh查看swap情况
[root@localhost ~]# free -mh
total used free shared buff/cache available
Mem: 3.8Gi 373Mi 3.2Gi 9.0Mi 221Mi 3.2Gi
Swap: 5.9Gi 0B 5.9Gi
使用swapon来管理swap分区
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 4116476 0 -2
通过磁盘分区来划分出新的分区作为swap
Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 6
First sector (2873344-10485759, default 2873344):
Last sector, +sectors or +size{K,M,G,T,P} (2873344-10485759, default 10485759): +2G
Created a new partition 6 of type 'Linux' and of size 2 GiB.
Partition #6 contains a swap signature.
Do you want to remove the signature? [Y]es/[N]o: Y
The signature will be removed by a write command.
Command (m for help): p
Disk /dev/nvme0n2: 5 GiB, 5368709120 bytes, 10485760 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: 0x7a7b9c31
Device Boot Start End Sectors Size Id Type
/dev/nvme0n2p1 2048 411647 409600 200M 83 Linux
/dev/nvme0n2p2 411648 1435647 1024000 500M 83 Linux
/dev/nvme0n2p3 1435648 10485759 9050112 4.3G 5 Extended
/dev/nvme0n2p5 1437696 2871295 1433600 700M b W95 FAT32
/dev/nvme0n2p6 2873344 7067647 4194304 2G 83 Linux
Filesystem/RAID signature on partition 6 will be wiped.
Command (m for help): t #由于使用做swap分区的,所以需要进行类型转换
Partition number (1-3,5,6, default 6):
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'.
#将分区类型从Linux改变为了Linux swap
Command (m for help):
Command (m for help): p
Disk /dev/nvme0n2: 5 GiB, 5368709120 bytes, 10485760 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: 0x7a7b9c31
Device Boot Start End Sectors Size Id Type
/dev/nvme0n2p1 2048 411647 409600 200M 83 Linux
/dev/nvme0n2p2 411648 1435647 1024000 500M 83 Linux
/dev/nvme0n2p3 1435648 10485759 9050112 4.3G 5 Extended
/dev/nvme0n2p5 1437696 2871295 1433600 700M b W95 FAT32
/dev/nvme0n2p6 2873344 7067647 4194304 2G 82 Linux swap / Solaris
让内存重新读取硬盘的信息
[root@localhost ~]# partprobe /dev/nvme0n2
对分区进行格式化,对swap分区的格式化和其他分区格式化不同,格式化后会生成UUID
[root@localhost ~]# mkswap /dev/nvme0n2p6
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=c32e1e46-bab2-45c6-9de5-05627eb2126f
编写/etc/fstab文件,永久挂载,swap分区不需要写挂载路径
/dev/nvme0n2p6 none swap defualts 0 0
重新加载swap
[root@localhost ~]# swapon -a
查看挂载结果
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/dev/nvme0n2p6 partition 2097148 0 -3
/dev/dm-1 partition 4116476 0 -2
通过根目录磁盘剩余容量创建swap,以文件作为交换空间
[root@localhost ~]# df -Th | grep -v tmp
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/rhel-root xfs 37G 20G 18G 52% /
根目录磁盘分区还剩余18G,从这18G中分出一部分来划分为swap,不需要对磁盘进行分区,直接从根磁盘中分出一部分,然后进行格式化即可
[root@localhost ~]# dd if=/dev/zero of=/tmp/swap1 bs=1M count=2048
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB, 2.0 GiB) copied, 22.1407 s, 97.0 MB/s
[root@localhost ~]# mkswap /tmp/swap1
mkswap: /tmp/swap1: insecure permissions 0644, 0600 suggested.
#建议我们将swap1的权限设置为0660
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=dfa53414-4d5c-4237-9f97-f1f7435de6b1
修改权限
[root@localhost ~]# chmod 0600 /tmp/swap1
[root@localhost ~]# ll -d /tmp/swap1
-rw-------. 1 root root 2147483648 Apr 1 02:13 /tmp/swap1
[root@localhost ~]#
修改/etc/fstab文件
后面的步骤就和第一种方式一样了
Linux系统作为一个多用户的操作系统,在生产环境中,会发生多个用户共同使用一个磁盘的情况,会造成Linux根分区的磁盘空间耗尽,导致Linux系统无法建立新的文件,从而出现服务程序崩溃、系统无法启动等故障现象。
为了避免这种情况,解决方法就是 ,对用户在指定文件系统中使用磁盘空间、文件数量进行限制,防止用户占用大量的磁盘空间,从而保持系统存储空间可以稳定使用。
在进行磁盘配额操作之前,先检查操作系统底层是否有支持的模块
[root@localhost ~]# grep CONFIG_QUOTA /boot/config-4.18.0-513.5.1.el8_9.x86_64
CONFIG_QUOTA=y
CONFIG_QUOTA_NETLINK_INTERFACE=y
# CONFIG_QUOTA_DEBUG is not set
CONFIG_QUOTA_TREE=y
CONFIG_QUOTACTL=y
CONFIG_QUOTACTL_COMPAT=y
主要对两个文件系统进行磁盘配额
etx4,不能限制root超级用户
修改/etc/fstab参数
/dev/nvme0n2p1 /mnt/ext ext4 defaults,usrquota,grpquota 0 0
查看/dev/nvme0n2p1信息,此时还没有quota相关参数
[root@localhost ~]# mount | grep nvme0n2p1
/dev/nvme0n2p1 on /mnt/ext type ext4 (rw,relatime,seclabel)
重新加载后就有了相关参数
[root@localhost ~]# mount -o remount /mnt/ext/
mount: (hint) your fstab has been modified, but systemd still uses
the old version; use 'systemctl daemon-reload' to reload.
[root@localhost ~]# mount | grep nvme0n2p1
/dev/nvme0n2p1 on /mnt/ext type ext4 (rw,relatime,seclabel,quota,usrquota,grpquota)
检查支持配额磁盘的分区,同时会在/mnt/ext目录下生成两个文件
[root@localhost ~]# quotacheck -augcv
#-a:检擦所有的磁盘分区
#-u,-g:检查对用户和用户组配额的分区
quotacheck: Your kernel probably supports journaled quota but you are not using it. Consider switching to journaled quota to avoid running quotacheck after an unclean shutdown.
quotacheck: Scanning /dev/nvme0n2p1 [/mnt/ext] done
quotacheck: Cannot stat old user quota file /mnt/ext/aquota.user: No such file or directory. Usage will not be subtracted.
quotacheck: Cannot stat old group quota file /mnt/ext/aquota.group: No such file or directory. Usage will not be subtracted.
quotacheck: Cannot stat old user quota file /mnt/ext/aquota.user: No such file or directory. Usage will not be subtracted.
quotacheck: Cannot stat old group quota file /mnt/ext/aquota.group: No such file or directory. Usage will not be subtracted.
quotacheck: Checked 3 directories and 0 files
quotacheck: Old file not found.
quotacheck: Old file not found.
[root@localhost ~]# ls -l /mnt/ext/
total 26
-rw-------. 1 root root 6144 Apr 1 02:25 aquota.group
-rw-------. 1 root root 6144 Apr 1 02:25 aquota.user
drwx------. 2 root root 12288 Mar 29 02:28 lost+found
对要进行配额的用户进行设置,block限制的是容量大小,inodes限制的是数量,soft和hard分别是软限制和硬限制,也就是超过了soft会给出警告,到达了hard则限制操作
[root@localhost ~]# edquota -u quotauser
Disk quotas for user quotauser (uid 10007):
Filesystem blocks soft hard inodes soft hard
/dev/nvme0n2p1 0 80M 100M 0 8 10
开启配额
[root@localhost ~]# quotaon -a
开始测试
#测试block
[quotauser@localhost ext]$ dd if=/dev/zero of=./test1 bs=30M count=1
1+0 records in
1+0 records out
31457280 bytes (31 MB, 30 MiB) copied, 0.709584 s, 44.3 MB/s
[quotauser@localhost ext]$ dd if=/dev/zero of=./test2 bs=30M count=1
1+0 records in
1+0 records out
31457280 bytes (31 MB, 30 MiB) copied, 0.540205 s, 58.2 MB/s
[quotauser@localhost ext]$ dd if=/dev/zero of=./test3 bs=30M count=1
nvme0n2p1: warning, user block quota exceeded. #此时quotauser创建的文件达到了90M,超过了soft block
1+0 records in
1+0 records out
31457280 bytes (31 MB, 30 MiB) copied, 0.589843 s, 53.3 MB/s
[quotauser@localhost ext]$ dd if=/dev/zero of=./test4 bs=30M count=1
nvme0n2p1: write failed, user block limit reached.
dd: error writing './test4': Disk quota exceeded
#此时quotauser创建的文件达到了120M,到达了hard block,因此超过的部分会被删除
1+0 records in
0+0 records out
10481664 bytes (10 MB, 10 MiB) copied, 0.231525 s, 45.3 MB/s
[quotauser@localhost ext]$ ll -h| grep test4
-rw-rw-r--. 1 quotauser quotauser 10M Apr 1 02:43 test4
#所以test4只用10M
#测试inodes
[quotauser@localhost ext]$ touch {1..10}.txt
nvme0n2p1: warning, user file quota exceeded.
nvme0n2p1: write failed, user file limit reached.
#下面的三个文件超出了inodes的个数限制,不能创建
touch: cannot touch '8.txt': Disk quota exceeded
touch: cannot touch '9.txt': Disk quota exceeded
touch: cannot touch '10.txt': Disk quota exceeded
grace time
当有配额限制的用户达到soft限制后,会产生grace time,它的含义是,在grace time限制内,如果没有将容量或者文件数量降低到soft限制下,那么hard限制将会降低为soft限制。如果在规定时间内降低到soft以下了,grace time会消失
[root@localhost ~]# quota -u quotauser
Disk quotas for user quotauser (uid 10007):
Filesystem blocks quota limit grace files quota limit grace
/dev/nvme0n2p1 92170* 81920 102400 6days 10* 8 10 6days
查看设备对那些用户进行限制
[root@localhost ~]# repquota /dev/nvme0n2p1
*** Report for user quotas on device /dev/nvme0n2p1
Block grace time: 7days; Inode grace time: 7days
Block limits File limits
User used soft hard grace used soft hard grace
----------------------------------------------------------------------
root -- 13 0 0 2 0 0
quotauser -- 61442 81920 102400 2 8 10
下线ext4的quota
quotaoff -a
xfs,可以针对root超级用户,不过需要创建项目
针对普通用户(不需要创建项目)
修改/etc/fstab参数
/dev/nvme0n2p1 /mnt/ext ext4 defaults,usrquota,grpquota 0 0
重新加载磁盘信息
[root@localhost ~]# umount /mnt/xfs/
[root@localhost ~]# mount /mnt/xfs/
mount: (hint) your fstab has been modified, but systemd still uses
the old version; use 'systemctl daemon-reload' to reload.
[root@localhost ~]# mount | grep nvme0n2p2
/dev/nvme0n2p2 on /mnt/xfs type xfs (rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,usrquota,grpquota)
进行user和group的配额设置
[root@localhost ~]# xfs_quota -x -c "limit bsoft=60M bhard=80M isoft=6 ihard=8 quotauser" /mnt/xfs
#limit:进行限制
#bsoft:block soft
查看配置
[root@localhost ~]# xfs_quota -x -c "report -ubih" /mnt/xfs
#u:表示用户,b:表示block,i:表示inodes h:显示形式
进行测试
[quotauser@localhost xfs]$ dd if=/dev/zero of=./test1 bs=25M count=1
1+0 records in
1+0 records out
26214400 bytes (26 MB, 25 MiB) copied, 0.050311 s, 521 MB/s
[quotauser@localhost xfs]$ dd if=/dev/zero of=./test2 bs=25M count=1
1+0 records in
1+0 records out
26214400 bytes (26 MB, 25 MiB) copied, 0.218181 s, 120 MB/s
[quotauser@localhost xfs]$ dd if=/dev/zero of=./test3 bs=25M count=1
1+0 records in
1+0 records out
26214400 bytes (26 MB, 25 MiB) copied, 0.312256 s, 84.0 MB/s
[quotauser@localhost xfs]$ dd if=/dev/zero of=./test4 bs=25M count=1
dd: error writing './test4': Disk quota exceeded
1+0 records in
0+0 records out
4194304 bytes (4.2 MB, 4.0 MiB) copied, 0.0943417 s, 44.5 MB/s
##xfs文件系统在达到配额的soft limit时不会给出警告信息,直到达到hard limit时才给出报错信息,并限制文件大小,同时,xfs和ext4文件系统一样具有grace time,并且机制和ext4一样
针对项目(root)进行配额
修改/etc/fstab参数,grpquota不能和prjquota共存
/dev/nvme0n2p2 /mnt/xfs xfs defaults,usrquota,prjquota 0 0
重新加载磁盘
[root@localhost ~]# umount /mnt/xfs
[root@localhost ~]# mount -a
mount: (hint) your fstab has been modified, but systemd still uses
the old version; use 'systemctl daemon-reload' to reload.
[root@localhost ~]#
[root@localhost ~]# mount | grep nvme0
/dev/nvme0n2p5 on /mnt/win type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,errors=remount-ro)
/dev/nvme0n2p1 on /mnt/ext type ext4 (rw,relatime,seclabel,quota,usrquota,grpquota)
/dev/nvme0n1p1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota)
/dev/nvme0n2p2 on /mnt/xfs type xfs (rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,usrquota,prjquota)
进行项目的绑定匹配
[root@localhost ~]# echo "41:/mnt/xfs">/etc/projects
[root@localhost ~]# cat /etc/projects
41:/mnt/xfs
[root@localhost ~]# echo "quotaxfs:41">>/etc/projid
[root@localhost ~]# cat /etc/projid
quotaxfs:41
启动项目
[root@localhost ~]# xfs_quota -x -c "project -s quotaxfs"
Setting up project quotaxfs (path /mnt/xfs)...
Processed 1 (/etc/projects and cmdline) paths for project quotaxfs with recursion depth infinite (-1).
Setting up project quotaxfs (path /mnt/xfs)...
Processed 1 (/etc/projects and cmdline) paths for project quotaxfs with recursion depth infinite (-1).
Setting up project quotaxfs (path /mnt/xfs)...
Processed 1 (/etc/projects and cmdline) paths for project quotaxfs with recursion depth infinite (-1).
Setting up project quotaxfs (path /mnt/xfs)...
Processed 1 (/etc/projects and cmdline) paths for project quotaxfs with recursion depth infinite (-1).
查看项目
[root@localhost ~]# xfs_quota -x -c "print" /mnt/xfs
Filesystem Pathname
/mnt/xfs /dev/nvme0n2p2 (uquota, pquota)
/mnt/xfs /dev/nvme0n2p2 (project 41, quotaxfs)
进行限制
[root@localhost ~]# xfs_quota -x -c "limit -p bsoft=90M bhard=100M quotaxfs" /mnt/xfs
[root@localhost xfs]# xfs_quota -x -c "report -pbih" /mnt/xfs
Project quota on /mnt/xfs (/dev/nvme0n2p2)
Blocks Inodes
Project ID Used Soft Hard Warn/Grace Used Soft Hard Warn/Grace
---------- --------------------------------- ---------------------------------
#0 0 0 0 00 [------] 2 0 0 00 [------]
quotaxfs 0 90M 100M 00 [------] 1 0 0 00 [------]
测试,通过root创建的文件也会受到限制同时也会有grace time
33554432 bytes (34 MB, 32 MiB) copied, 0.0351664 s, 954 MB/s
[root@localhost xfs]# dd if=/dev/zero of=./root1 bs=32M count=1
1+0 records in
1+0 records out
33554432 bytes (34 MB, 32 MiB) copied, 0.0432654 s, 776 MB/s
[root@localhost xfs]# dd if=/dev/zero of=./root2 bs=32M count=1
1+0 records in
1+0 records out
33554432 bytes (34 MB, 32 MiB) copied, 0.0447339 s, 750 MB/s
[root@localhost xfs]# dd if=/dev/zero of=./root3 bs=32M count=1
1+0 records in
1+0 records out
33554432 bytes (34 MB, 32 MiB) copied, 0.27855 s, 120 MB/s
[root@localhost xfs]# ll
total 98304
-rw-r--r--. 1 root root 33554432 Apr 1 03:53 root1
-rw-r--r--. 1 root root 33554432 Apr 1 03:53 root2
-rw-r--r--. 1 root root 33554432 Apr 1 03:53 root3
[root@localhost xfs]# dd if=/dev/zero of=./root4 bs=32M count=1
dd: error writing './root4': No space left on device
1+0 records in
0+0 records out
4194304 bytes (4.2 MB, 4.0 MiB) copied, 0.116613 s, 36.0 MB/s
[root@localhost xfs]# xfs_quota -x -c "report -pbih" /mnt/xfs
Project quota on /mnt/xfs (/dev/nvme0n2p2)
Blocks Inodes
Project ID Used Soft Hard Warn/Grace Used Soft Hard Warn/Grace
---------- --------------------------------- ---------------------------------
#0 0 0 0 00 [------] 2 0 0 00 [------]
quotaxfs 100M 90M 100M 00 [7 days] 5 0 0 00 [------]
对于项目的配额来说,在一个项目中会有许多的用户,针对划分的空间,采取的是谁先占用就是谁的
只看某一个项目的具体信息
[root@localhost xfs]# xfs_quota -c "quota -p 41" /mnt/xfs
Disk quotas for Project quotaxfs (41)
Filesystem Blocks Quota Limit Warn/Time Mounted on
/dev/nvme0n2p2 36864 92160 102400 00 [--------] /mnt/xfs
查看xfs的quota状态
[root@localhost xfs]# xfs_quota -x -c "state" /mnt/xfs
User quota state on /mnt/xfs (/dev/nvme0n2p2)
Accounting: ON
Enforcement: ON
Inode: #131 (2 blocks, 2 extents)
Blocks grace time: [7 days]
Blocks max warnings: 5
Inodes grace time: [7 days]
Inodes max warnings: 5
Realtime Blocks grace time: [7 days]
Group quota state on /mnt/xfs (/dev/nvme0n2p2)
Accounting: OFF
Enforcement: OFF
Inode: #132 (2 blocks, 2 extents)
Blocks grace time: [7 days]
Blocks max warnings: 5
Inodes grace time: [7 days]
Inodes max warnings: 5
Realtime Blocks grace time: [7 days]
Project quota state on /mnt/xfs (/dev/nvme0n2p2)
Accounting: ON
Enforcement: ON
Inode: #137 (2 blocks, 2 extents)
Blocks grace time: [7 days]
Blocks max warnings: 5
Inodes grace time: [7 days]
Inodes max warnings: 5
Realtime Blocks grace time: [7 days]
xfs的quota功能开关
xfs_quota -x -c "disable -up" /mnt/xfs/ #关闭配额
xfs_quota -x -c "off -up" /mnt/xfs/ #永久下线配额
xfs_quota -x -c "remove -p" /mnt/xfs/#移除配额项目
ext4 文件系统 | xfs 文件系统 |
---|---|
无法对单一目录 | 可对单一目录、超级用户 |
quota工具 | xfs_quota工具 |
quota默认不开启 | 默认开启 |
需要创建配额配置文件:edquota -u user | 不需要创建配额配置文件 |
进行LVM逻辑卷建立的分区或磁盘,必须是没有被使用的(没有被格式化的)
LVM(Logical Volume Manager)是基于内核的一种逻辑卷管理器,LVM适合于管理大存储设备,并允许用户动态调整文件系统大小。对底层硬盘的整合,当我们对LVM逻辑卷进行管理时,不需要在涉及到底层的硬盘了
1.物理卷(Physical Volume, PV)
物理卷是LVM的最底层概念,是LVM的逻辑存储块,物理卷与磁盘分区是逻辑的对应关系。LVM提供了命令工具可以将磁盘分区转换为物理卷,通过组合物理卷生成卷组。
2.卷组( Volume Group,VG)
卷组是LVM逻辑概念上的磁盘设备,通过将单个或多个物理卷组合后生成卷组。卷组的大小取决于物理卷的容量及个数。在Centos7系统中,对容量与个数没有限制。
3.逻辑卷(Logical Volume, LV)
逻辑卷就是LVM逻辑意义上的分区,我们可以指定从卷组中提取多少容量来创建逻辑卷,最后对逻辑卷格式化并挂载使用。
也就是说我们真正去使用的是LVM逻辑卷的分区,并不是直接使用磁盘的分区
首先也要先对磁盘进行分区,然后转换为物理卷PV
创建好分区后进行格式转换,转换为Linux LVM格式
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): L
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'.
将磁盘的分区创建为物理分区
[root@localhost ~]# pvcreate /dev/nvme0n3p1
Physical volume "/dev/nvme0n3p1" successfully created
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/nvme0n1p2 rhel lvm2 a-- <59.00g 0
/dev/nvme0n3p1 lvm2 --- 2.00g 2.00g
##使用pvs命令查看物理分区情况
将物理分区生成卷组
[root@localhost ~]# vgcreate myvg /dev/nvme0n3p1
Volume group "myvg" successfully created
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg 1 0 0 wz--n- <2.00g <2.00g
rhel 1 3 0 wz--n- <59.00g 0
此时再查看物理分区情况
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/nvme0n1p2 rhel lvm2 a-- <59.00g 0
/dev/nvme0n3p1 myvg lvm2 a-- <2.00g <2.00g
出现了vg卷组的名字
从卷组中抽取一部分容量创建LVM逻辑卷
[root@localhost ~]# lvcreate -n mylv -L 300M myvg
Logical volume "mylv" created.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mylv myvg -wi-a----- 300.00m
home rhel -wi-ao---- <18.07g
root rhel -wi-ao---- 37.00g
swap rhel -wi-ao---- <3.93g
然后对创建好的LVM进行格式化和挂载
[root@localhost ~]# mkfs.ext4 /dev/myvg/mylv
mke2fs 1.45.6 (20-Mar-2020)
Creating filesystem with 307200 1k blocks and 76912 inodes
Filesystem UUID: 0d6c7eb8-14ab-4110-b7d1-e83a9a7b93c7
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting i
需要注意:此时在创建好LVM后就已经和底层的磁盘分区没有什么关系了,我们在进行格式化的时候,就不能使用/dev/nvme0n3p1,要使用/dev/myvg/mylv,之后就是修改/etc/fstab文件。然后通过mount进行挂载
物理卷PV不能进行扩展和缩减了,只有卷组和逻辑卷可以实现扩展和缩减
ext4 file system
扩容(卷组空间足够的情况下)
先扩大逻辑卷
root@localhost lvmext4]# lvextend -L +300M /dev/myvg/mylv
Size of logical volume myvg/mylv changed from 300.00 MiB (75 extents) to 600.00 MiB (150 extents).
Logical volume myvg/mylv successfully resized.
[root@localhost lvmext4]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mylv myvg -wi-ao---- 600.00m
home rhel -wi-ao---- <18.07g
root rhel -wi-ao---- 37.00g
swap rhel -wi-ao---- <3.93g
[root@localhost lvmext4]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg 1 1 0 wz--n- <2.00g 1.41g
rhel 1 3 0 wz--n- <59.00g 0
[root@localhost lvmext4]# pvs
PV VG Fmt Attr PSize PFree
/dev/nvme0n1p2 rhel lvm2 a-- <59.00g 0
/dev/nvme0n3p1 myvg lvm2 a-- <2.00g 1.41g
通知文件系统
[root@localhost lvmext4]# df -Th | grep lvm
/dev/mapper/myvg-mylv ext4 281M 186M 77M 71% /mnt/lvmext4
此时文件系统还不知道,我已进行了扩容,所以通知文件系统
[root@localhost lvmext4]# df -Th | grep lvm
/dev/mapper/myvg-mylv ext4 281M 186M 77M 71% /mnt/lvmext4
[root@localhost lvmext4]# resize2fs /dev/myvg/mylv
resize2fs 1.45.6 (20-Mar-2020)
Filesystem at /dev/myvg/mylv is mounted on /mnt/lvmext4; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 5
The filesystem on /dev/myvg/mylv is now 614400 (1k) blocks long.
[root@localhost lvmext4]# df -Th | grep lvm
/dev/mapper/myvg-mylv ext4 572M 186M 355M 35% /mnt/lvmext4
扩容(在卷组空间不足的情况下)
首先我们需要去增加一个磁盘分区,转换为物理分区,让他加入myvg卷组
[root@localhost lvmext4]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[root@localhost lvmext4]# pvcreate /dev/nvme0n3p2
Physical volume "/dev/nvme0n3p2" successfully created.
[root@localhost lvmext4]# vgextend myvg /dev/nvme0n3p2
Volume group "myvg" successfully extended
然后再进行扩容操作
缩减
首先需要下载挂载的内容
[root@localhost ~]# umount /mnt/lvmext4
对逻辑分区进行检查
[root@localhost ~]# e2fsck -f /dev/myvg/mylv
e2fsck 1.45.6 (20-Mar-2020)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/myvg/mylv: 11/65536 files (0.0% non-contiguous), 12955/262144 blocks
先缩小文件系统
[root@localhost ~]# resize2fs /dev/myvg/mylv 300M
resize2fs 1.45.6 (20-Mar-2020)
Resizing the filesystem on /dev/myvg/mylv to 76800 (4k) blocks.
The filesystem on /dev/myvg/mylv is now 76800 (4k) blocks long.
再缩小逻辑卷
[root@xu ~]# lvreduce -L 4G /dev/xuruizahovg/xuruizhaolv
WARNING: Reducing active logical volume to 4.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce xuruizahovg/xuruizhaolv? [y/n]: y
Size of logical volume xuruizahovg/xuruizhaolv changed from 12.00 GiB (3072 extents) to 4.00 GiB (1024 extents).
Logical volume xuruizahovg/xuruizhaolv successfully resized.
重新挂载
[root@localhost ~]# mount -a
缩容操作是一种存在一定风险的操作,因此在重新挂载时,可能会报错,主要是由于缩减后的容量系统认为不能放下目录中的所有数据,所以不让挂载。
解决方法:备份数据,重新格式化分区,再次进行挂载
xfs file system
xfs被创建出来的初衷就是为了解决大文件、大数据存储的,因此对于xfs file system默认不存在容量缩减的操作
扩容
首先也是创建PV(物理分区)、VG(卷组)、LVM(逻辑卷)
进行格式化
[root@localhost xfs]# mkfs.xfs /dev/my_xfs/lvxfs
meta-data=/dev/my_xfs/lvxfs isize=512 agcount=4, agsize=25600 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1 bigtime=0 inobtcount=0
data = bsize=4096 blocks=102400, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=1368, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
进行挂载
扩展分区,并通知文件系统
[root@localhost xfs]# lvextend -L +200M /dev/my_xfs/lvxfs
Size of logical volume my_xfs/lvxfs changed from 400.00 MiB (100 extents) to 600.00 MiB (150 extents).
Logical volume my_xfs/lvxfs successfully resized.
[root@localhost xfs]# xfs_growfs /dev/my_xfs/lvxfs
meta-data=/dev/mapper/my_xfs-lvxfs isize=512 agcount=4, agsize=25600 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1 bigtime=0 inobtcount=0
data = bsize=4096 blocks=102400, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=1368, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 102400 to 153600
[root@localhost xfs]# df -Th | grep lv
/dev/mapper/myvg-mylv ext4 2.5G 186M 2.2G 8% /mnt/lvmext4
/dev/mapper/my_xfs-lvxfs xfs 595M 25M 571M 5% /mnt/lvxfs
对于xfs文件系统,使用xfs_growfs
通知文件系统
功能 | 物理卷管理 | 卷组管理 | 逻辑卷管理 |
---|---|---|---|
Scan扫描 | pvscan | vgscan | lvscan |
Create建立 | pvcreat | vgcreat | lvcreat |
Display显示 | pvdisplay | vgdisplay | lvdisplay |
Remove删除 | pvremove | vgremove | lvmove |
Extend扩展 | vgextent | lvectent | |
Reduce减少 | vgreduce | lvreduce |
我们可以迁移逻辑卷到一个新的磁盘而不会丢失数据,也不用关机操作。该特性的功能是将数据从旧磁盘移动到新磁盘。通常,我们只是在一些磁盘发生错误时,才将数据从一个磁盘迁移到另外一个磁盘存储。
将逻辑卷从一个磁盘移动到另一个磁盘。
我们可以使用任何类型的磁盘,如SATA、SSD、SAS、SAN storage iSCSI或者FC。
在线迁移磁盘,而且数据不会丢失。
迁移方式:
通过新的磁盘将数据进行转移
查看当前系统逻辑卷的情况
[root@bogon ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 12.6G 0 rom /mnt/cdrom
nvme0n1 259:0 0 60G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 59G 0 part
├─rhel-root 253:0 0 37G 0 lvm /
├─rhel-swap 253:1 0 3.9G 0 lvm [SWAP]
└─rhel-home 253:2 0 18.1G 0 lvm /home
nvme0n2 259:3 0 5G 0 disk
├─nvme0n2p1 259:4 0 300M 0 part
└─nvme0n2p2 259:5 0 600M 0 part /mnt/xfs
nvme0n3 259:6 0 10G 0 disk
├─nvme0n3p1 259:7 0 2G 0 part
│ └─myvg-mylv 253:4 0 400M 0 lvm /mnt/lvmext4
├─nvme0n3p2 259:8 0 1G 0 part
└─nvme0n3p3 259:9 0 1G 0 part
└─my_xfs-lvxfs 253:3 0 600M 0 lvm /mnt/lvxfs
删除nvme0n3xfs逻辑卷的物理分区,重新加载
添加一块新的磁盘,进行分区,将其加入到逻辑卷中,对xfs文件系统进行迁移
[root@bogon ~]# pvcreate /dev/nvme0n4p1
Physical volume "/dev/nvme0n4p1" successfully created.
[root@bogon ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/nvme0n1p2 rhel lvm2 a-- <59.00g 0
/dev/nvme0n3p5 myvg_ext4 lvm2 a-- 496.00m 0
/dev/nvme0n3p6 myvg_ext4 lvm2 a-- 496.00m 392.00m
/dev/nvme0n3p7 myvg_xfs lvm2 a-- 596.00m 296.00m
/dev/nvme0n4p1 lvm2 --- 1.00g 1.00g
[root@bogon ~]# vgextend myvg_xfs /dev/nvme0n4p1
Volume group "myvg_xfs" successfully extended
[root@bogon ~]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg_ext4 2 1 0 wz--n- 992.00m 392.00m
myvg_xfs 2 1 0 wz--n- <1.58g <1.29g
rhel 1 3 0 wz--n- <59.00g 0
[root@bogon ~]# pvmove /dev/nvme0n3p7 /dev/nvme0n4p1
/dev/nvme0n3p7: Moved: 50.67%
/dev/nvme0n3p7: Moved: 100.00%
由于删除了物理分区,所以逻辑卷会进行警告
[root@bogon ~]# vgs
WARNING: Couldn't find device with uuid ubE7fk-OWBH-nQ2d-zMZU-GR7w-zdsT-OD1RKJ.
WARNING: VG myvg_xfs is missing PV ubE7fk-OWBH-nQ2d-zMZU-GR7w-zdsT-OD1RKJ (last written to /dev/nvme0n3p7).
VG #PV #LV #SN Attr VSize VFree
myvg_ext4 3 1 0 wz--n- 1.96g <1.38g
myvg_xfs 2 1 0 wz-pn- <1.58g <1.29g
rhel 1 3 0 wz--n- <59.00g 0
[root@bogon ~]# pvs
WARNING: Couldn't find device with uuid ubE7fk-OWBH-nQ2d-zMZU-GR7w-zdsT-OD1RKJ.
WARNING: VG myvg_xfs is missing PV ubE7fk-OWBH-nQ2d-zMZU-GR7w-zdsT-OD1RKJ (last written to /dev/nvme0n3p7).
PV VG Fmt Attr PSize PFree
/dev/nvme0n1p2 rhel lvm2 a-- <59.00g 0
/dev/nvme0n3p5 myvg_ext4 lvm2 a-- 496.00m 0
/dev/nvme0n3p6 myvg_ext4 lvm2 a-- 496.00m 392.00m
/dev/nvme0n4p1 myvg_xfs lvm2 a-- 1020.00m 720.00m
/dev/nvme0n4p2 myvg_ext4 lvm2 a-- 1020.00m 1020.00m
[unknown] myvg_xfs lvm2 a-m 596.00m 596.00m
[root@bogon ~]# lvs
WARNING: Couldn't find device with uuid ubE7fk-OWBH-nQ2d-zMZU-GR7w-zdsT-OD1RKJ.
WARNING: VG myvg_xfs is missing PV ubE7fk-OWBH-nQ2d-zMZU-GR7w-zdsT-OD1RKJ (last written to /dev/nvme0n3p7).
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mylv_ext4 myvg_ext4 -wi-ao---- 600.00m
mylv_xfs myvg_xfs -wi-ao---- 300.00m
home rhel -wi-ao---- <18.07g
root rhel -wi-ao---- 37.00g
swap rhel -wi-ao---- <3.93g
我们需要将对应的卷组删除
[root@bogon ~]# vgreduce --removemissing --force myvg_xfs
WARNING: Couldn't find device with uuid ubE7fk-OWBH-nQ2d-zMZU-GR7w-zdsT-OD1RKJ.
WARNING: VG myvg_xfs is missing PV ubE7fk-OWBH-nQ2d-zMZU-GR7w-zdsT-OD1RKJ (last written to /dev/nvme0n3p7).
WARNING: Couldn't find device with uuid ubE7fk-OWBH-nQ2d-zMZU-GR7w-zdsT-OD1RKJ.
Wrote out consistent volume group myvg_xfs.
[root@bogon ~]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg_ext4 3 1 0 wz--n- 1.96g <1.38g
myvg_xfs 1 1 0 wz--n- 1020.00m 720.00m
rhel 1 3 0 wz--n- <59.00g 0
[root@bogon ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/nvme0n1p2 rhel lvm2 a-- <59.00g 0
/dev/nvme0n3p5 myvg_ext4 lvm2 a-- 496.00m 0
/dev/nvme0n3p6 myvg_ext4 lvm2 a-- 496.00m 392.00m
/dev/nvme0n4p1 myvg_xfs lvm2 a-- 1020.00m 720.00m
/dev/nvme0n4p2 myvg_ext4 lvm2 a-- 1020.00m 1020.00m
[root@bogon ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mylv_ext4 myvg_ext4 -wi-ao---- 600.00m
mylv_xfs myvg_xfs -wi-ao---- 300.00m
home rhel -wi-ao---- <18.07g
root rhel -wi-ao---- 37.00g
swap rhel -wi-ao---- <3.93g
至此迁移成功
[root@bogon ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 12.6G 0 rom /mnt/cdrom
nvme0n1 259:0 0 60G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 59G 0 part
├─rhel-root 253:0 0 37G 0 lvm /
├─rhel-swap 253:1 0 3.9G 0 lvm [SWAP]
└─rhel-home 253:2 0 18.1G 0 lvm /home
nvme0n2 259:3 0 5G 0 disk
├─nvme0n2p1 259:4 0 300M 0 part
└─nvme0n2p2 259:5 0 600M 0 part
nvme0n3 259:6 0 10G 0 disk
├─nvme0n3p1 259:7 0 1G 0 part
├─nvme0n3p2 259:8 0 1K 0 part
├─nvme0n3p5 259:9 0 500M 0 part
│ └─myvg_ext4-mylv_ext4 253:4 0 600M 0 lvm /mnt/lvmext4
└─nvme0n3p6 259:10 0 500M 0 part
└─myvg_ext4-mylv_ext4 253:4 0 600M 0 lvm /mnt/lvmext4
nvme0n4 259:12 0 10G 0 disk
├─nvme0n4p1 259:13 0 1G 0 part
│ └─myvg_xfs-mylv_xfs 253:3 0 300M 0 lvm /mnt/lvxfs
└─nvme0n4p2
ext4文件系统和xfs文件系统操作相同
使用硬盘快照来进行备份
通过建立硬盘快照,对分区中的内进行备份,如果硬盘中的数据丢失,可以通过硬盘快照来恢复硬盘中的数据
建立快照
[root@bogon ~]# lvcreate -n mylv_xfs_snap -s -L 100M /dev/myvg_xfs/mylv_xfs
Logical volume "mylv_xfs_snap" created.
对硬盘快照进行挂载,由于硬盘快照和硬盘分区的UUID是相同的,但是挂载点不同,因此在对磁盘快照进行挂载时要带上忽略uuid的参数nouuid
[root@bogon ~]# mount -o nouuid /dev/myvg_xfs/mylv_xfs_snap /mnt/snap/
当硬盘分区的文件发生错误时,我们可以通过快照复原丢失的文件
[root@bogon ~]# cat /mnt/snap/etc/fstab > /mnt/lvxfs/etc/fstab
[root@bogon ~]# diff /mnt/snap/etc/fstab /mnt/lvxfs/etc/fstab
对于快照,我们在需要时进行挂载,在不需要时即可下线
在逻辑卷之前要先下线设备
首先移除LVM
[root@bogon ~]# lvremove /dev/myvg/mylv /dev/myxfs_vg/myxfs_lv
Do you really want to remove active logical volume myvg/mylv? [y/n]: y
Logical volume "mylv" successfully removed.
Do you really want to remove active logical volume myxfs_vg/myxfs_lv? [y/n]: y
Logical volume "myxfs_lv" successfully removed.
[root@bogon ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home rhel -wi-ao---- <18.07g
root rhel -wi-ao---- 37.00g
swap rhel -wi-ao---- <3.93g
再移除VG
[root@bogon ~]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg 1 0 0 wz--n- 1020.00m 1020.00m
myxfs_vg 1 0 0 wz--n- 1020.00m 1020.00m
rhel 1 3 0 wz--n- <59.00g 0
[root@bogon ~]# vgremove myvg myxfs_vg
Volume group "myvg" successfully removed
Volume group "myxfs_vg" successfully removed
[root@bogon ~]# vgs
VG #PV #LV #SN Attr VSize VFree
rhel 1 3 0 wz--n- <59.00g 0
最后移除PV
[root@bogon ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/nvme0n1p2 rhel lvm2 a-- <59.00g 0
/dev/nvme0n3p1 lvm2 --- 500.00m 500.00m
/dev/nvme0n3p2 lvm2 --- 500.00m 500.00m
/dev/nvme0n4p1 lvm2 --- 1.00g 1.00g
/dev/nvme0n4p2 lvm2 --- 1.00g 1.00g
[root@bogon ~]# pvremove /dev/nvme0n3* /dev/nvme0n4*
Cannot use /dev/nvme0n4: device is partitioned
No PV found on device /dev/nvme0n3.
Labels on physical volume "/dev/nvme0n3p1" successfully wiped.
Labels on physical volume "/dev/nvme0n3p2" successfully wiped.
Labels on physical volume "/dev/nvme0n4p1" successfully wiped.
Labels on physical volume "/dev/nvme0n4p2" successfully wiped.
[root@bogon ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/nvme0n1p2 rhel lvm2 a-- <59.00g 0
在进行激活与反激活切换时需要先下线
查看逻辑卷状态,ACTIVE是激活状态
[root@bogon ~]# lvscan
\ ACTIVE '/dev/myvg_ext4/mylv_ext4' [600.00 MiB] inherit
ACTIVE Original '/dev/myvg_xfs/mylv_xfs' [300.00 MiB] inherit
ACTIVE Snapshot '/dev/myvg_xfs/mylv_xfs_snap' [252.00 MiB] inherit
ACTIVE '/dev/rhel/swap' [<3.93 GiB] inherit
ACTIVE '/dev/rhel/home' [<18.07 GiB] inherit
ACTIVE '/dev/rhel/root' [37.00 GiB] inherit
inactive是反激活状态
[root@bogon ~]# umount /mnt/lvmext4
[root@bogon ~]# vgchange -a n myvg_ext4
0 logical volume(s) in volume group "myvg_ext4" now active
[root@bogon ~]# lvscan
inactive '/dev/myvg_ext4/mylv_ext4' [600.00 MiB] inherit
ACTIVE Original '/dev/myvg_xfs/mylv_xfs' [300.00 MiB] inherit
ACTIVE Snapshot '/dev/myvg_xfs/mylv_xfs_snap' [252.00 MiB] inherit
ACTIVE '/dev/rhel/swap' [<3.93 GiB] inherit
ACTIVE '/dev/rhel/home' [<18.07 GiB] inherit
ACTIVE '/dev/rhel/root' [37.00 GiB] inherit
处于inactive状态的分区是不能够启动的
切换为ACTIVE状态
[root@bogon ~]# vgchange -a y myvg_ext4
1 logical volume(s) in volume group "myvg_ext4" now active
[root@bogon ~]# lvscan
ACTIVE '/dev/myvg_ext4/mylv_ext4' [600.00 MiB] inherit
ACTIVE Original '/dev/myvg_xfs/mylv_xfs' [300.00 MiB] inherit
ACTIVE Snapshot '/dev/myvg_xfs/mylv_xfs_snap' [252.00 MiB] inherit
ACTIVE '/dev/rhel/swap' [<3.93 GiB] inherit
ACTIVE '/dev/rhel/home' [<18.07 GiB] inherit
ACTIVE '/dev/rhel/root' [37.00 GiB] inherit
在我们想要进行逻辑卷创建时,会出现如下问题
我想创建一个10M的逻辑卷,它提示我创建好了一个12M的逻辑卷
❓这是为什么呢
{24-06-03 10:59}locahost:~ root# lvcreate -L 10M -n lvxixi systemvg
Rounding up size to full physical extent 12.00 MiB
Logical volume "lvxixi" created
主要原因:
在卷组当中存在着一个PE(扩展单元)的机制,由于逻辑卷的建立是基于卷组去实现的,而在卷组的角度来说,我只能分给逻辑卷整数个PE的大小。
PE默认是4M,因此,逻辑卷默认只能创建4M整数倍的大小,这也就解释了为什么我想创建10M的大小,系统却创建出了12M的大小
我们可以查看卷组的PE大小vgdisplay
{24-06-03 11:23}locahost:~ root# vgdisplay systemvg | egrep "PE"
PE Size 1.00 MiB
Total PE 71660
Alloc PE / Size 25643 / 25.04 GiB
Free PE / Size 46017 / <44.94 GiB
卷组的默认PE值是可以进行修改的,在修改时,一般不能修改成单数(1除外)
vgchange
{24-06-03 11:08}locahost:~ root# vgchange -s 1M systemvg
Volume group "systemvg" successfully changed
当我们修改完后,就可以创建11M的逻辑卷了
{24-06-03 11:09}locahost:~ root# lvcreate -L 11M -n lvhehe systemvg
Logical volume "lvhehe" created.
PE是不能随便修改的,当我们修改为1M,并且创建了一个11M的逻辑卷
此时我们如果想继续修改PE,那就必须修改为11的倍数才行。
我们想创建一个10个PE大小的逻辑卷,我们可以直接指定大小
也可以从个数方面去创建
lvcreate -l
{24-06-03 11:09}locahost:~ root# lvcreate -l 20 -n lvtest systemvg
Logical volume "lvtest" created.
RAID:进行磁盘整合,将多块硬盘整合到一起,形成RAID盘,多个RAID盘也可以继续整合为一个虚拟硬盘,在虚拟硬盘中就可以继续创建逻辑卷了,这种方式的优势:允许坏掉几块硬盘,数据不会丢失
LVM逻辑卷:进行磁盘扩容
RAID磁盘阵列:
RAID0模式(条带模式)
RAID1模式
RAID1 是磁盘阵列中单位成本最高的一种方式。因为它的原理是在往磁盘写数据的时候,将同一份数据无差别的写两份到磁盘,分别写到工作磁盘和镜像磁盘,那么它的实际空间使用率只有50%了,两块磁盘当做一块用,这是一种比较昂贵的方案。
RAID1其实与RAID0效果刚好相反。RAID1 这种写双份的做法,就给数据做了一个冗余备份。这样的话,任何一块磁盘损坏了,都可以再基于另外一块磁盘去恢复数据,数据的可靠性非常强,但读取性能就没那么好了。
RAID5模式
RAID6模式
为了进一步提高存储的高可用,聪明的人们又提出了RAID6方案,可以在有两块磁盘同时损坏的情况下,也能保障数据可恢复。
为什么RAID6这么牛呢,因为RAID6在RAID5的基础上再次改进,引入了双重校验的概念(加入了伽罗华域算法)
RAID6除了每块磁盘上都有同级数据XOR校验区以外,还有针对每个数据块的XOR校验区,这样的话,相当于每个数据块有两个校验保护措施,因此数据的冗余性更高了。
但是RAID6的这种设计也带来了很高的复杂度,虽然数据冗余性好,读取的效率也比较高,但是写数据的性能就很差。因此RAID6在实际环境中应用的比较少。
RAID10模式
RAID10其实就是RAID1与RAID0的一个合体。
RAID10兼备了RAID1和RAID0的有优点。首先基于RAID1模式将磁盘分为2份,当要写入数据的时候,将所有的数据在两份磁盘上同时写入,相当于写了双份数据,起到了数据保障的作用。且在每一份磁盘上又会基于RAID0技术讲数据分为N份并发的读写,这样也保障了数据的效率。
但也可以看出RAID10模式是有一半的磁盘空间用于存储冗余数据的,浪费的很严重,因此用的也不是很多。
实现RAID的方式
mdadm命令创建软RAID5
添加磁盘,磁盘大小要一致,至少3块磁盘。我们添加四块磁盘,剩余一块用来模拟故障,切换
创建软RAID
[root@bogon ~]# mdadm -C /dev/md0 -n 3 -l 5 /dev/nvme0n[234]
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
在创建软RAID时,可以通过查看内存查看建立过程
[root@bogon ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 nvme0n4[3] nvme0n3[1] nvme0n2[0]
10475520 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/2] [UU_]
[===>.................] recovery = 18.9% (994076/5237760) finish=0.2min speed=248519K/sec
unused devices: <none>
[root@bogon ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 nvme0n4[3] nvme0n3[1] nvme0n2[0]
10475520 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU]
unused devices: <none>
查看RAID的信息
[root@bogon ~]# mdadm -D /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Mon Apr 8 03:21:00 2024
Raid Level : raid5
Array Size : 10475520 (9.99 GiB 10.73 GB)
Used Dev Size : 5237760 (5.00 GiB 5.36 GB)
Raid Devices : 3
Total Devices : 3
Persistence : Superblock is persistent
Update Time : Mon Apr 8 03:21:27 2024
State : clean
Active Devices : 3
Working Devices : 3
Failed Devices : 0
Spare Devices : 0
Layout : left-symmetric
Chunk Size : 512K
Consistency Policy : resync
Name : bogon:0 (local to host bogon)
UUID : 178bfb9f:c88b0b14:70373056:288bb63e
Events : 18
Number Major Minor RaidDevice State
0 259 3 0 active sync /dev/nvme0n2
1 259 4 1 active sync /dev/nvme0n3
3 259 5 2 active sync /dev/nvme0n4
编写RAID的配置文件使其永久生效
[root@bogon ~]# mdadm -E -s --brief > /etc/mdadm.comf
[root@bogon ~]# vim /etc/mdadm.comf
[root@bogon ~]# cat /etc/mdadm.comf
ARRAY /dev/md0 UUID=178bfb9f:c88b0b14:70373056:288bb63e
在RAID的基础上创建LVM
[root@bogon ~]# pvcreate /dev/md0
Physical volume "/dev/md0" successfully created.
[root@bogon ~]# vgcreate mdvg /dev/md0
Volume group "mdvg" successfully created
[root@bogon ~]# lvcreate -n mdlv -L 3G mdvg
Logical volume "mdlv" created.
[root@bogon ~]# mkfs.ext4 /dev/mdvg/mdlv
mke2fs 1.45.6 (20-Mar-2020)
Creating filesystem with 786432 4k blocks and 196608 inodes
Filesystem UUID: e12f0f0a-46d2-460e-a63b-0e025707b97b
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
进行永久挂载
模拟硬盘故障
[root@bogon ~]# mdadm --manage /dev/md0 --fail /dev/nvme0n4
mdadm: set /dev/nvme0n4 faulty in /dev/md0
[root@bogon ~]# mdadm --manage /dev/md0 --remove /dev/nvme0n4
mdadm: hot removed /dev/nvme0n4 from /dev/md0
[root@bogon ~]# mdadm --manage /dev/md0 --add /dev/nvme0n5
mdadm: added /dev/nvme0n5
数据不会丢失
删除磁盘阵列
删除LVM
[root@bogon ~]# umount /mnt/lvmext4
[root@bogon ~]#
[root@bogon ~]#
[root@bogon ~]# lvremove /dev/mdvg/mdlv
Do you really want to remove active logical volume mdvg/mdlv? [y/n]: y
Logical volume "mdlv" successfully removed.
[root@bogon ~]# vgremove mdvg
Volume group "mdvg" successfully removed
[root@bogon ~]# pvremove /dev/md0
Labels on physical volume "/dev/md0" successfully wiped.
[root@bogon ~]#
停止RAID
[root@bogon ~]# mdadm -S /dev/md0
mdadm: stopped /dev/md0
删除RAID的配置文件
[root@bogon ~]# rm -rf /etc/mdadm.comf
重启RAID 进行测试
[root@bogon ~]# mdadm -As /dev/md0
mdadm: /dev/md0 not identified in config file.
#无法重启证明配置文件删除成功
进行重启
重启后检查硬盘中是否存在RAID的缓存
没有缓存,证明RAID被完全删除
有缓存,强制删除
[root@bogon ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md127 : active (auto-read-only) raid5 nvme0n3[1] nvme0n2[0] nvme0n5[3]
10475520 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU]
unused devices: <none>
[root@bogon ~]# mdadm --zero-superblock /dev/nvme0n2
[root@bogon ~]# mdadm --zero-superblock /dev/nvme0n3
[root@bogon ~]# mdadm --zero-superblock /dev/nvme0n4
[root@bogon ~]# mdadm --zero-superblock /dev/nvme0n5
[root@bogon ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
unused devices: <none>
监控RAID可以在磁盘发生故障时通知我们,进而可以及时更换硬盘
在/etc/mdamd.conf中设置正确的邮件,确保可以将通知发送给我们
[root@locahost ~]# echo "MAILADDR xuruizhao00@163.com" >> /etc/mdadm.conf
开启监视服务
[root@locahost ~]# systemctl start mdmonitor.service
[root@locahost ~]# systemctl status mdmonitor.service
● mdmonitor.service - Software RAID monitoring and management
Loaded: loaded (/usr/lib/systemd/system/mdmonitor.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2024-04-08 22:34:54 EDT; 4min 57s ago
Process: 2639 ExecStart=/sbin/mdadm --monitor --scan -f --pid-file=/var/run/mdadm/mdadm.pid (code=exited, status=0/SUCCESS)
Main PID: 2640 (mdadm)
Tasks: 1 (limit: 24565)
Memory: 652.0K
CGroup: /system.slice/mdmonitor.service
└─2640 /sbin/mdadm --monitor --scan -f --pid-file=/var/run/mdadm/mdadm.pid
Apr 08 22:34:54 locahost systemd[1]: Starting Software RAID monitoring and management...
Apr 08 22:34:54 locahost systemd[1]: Started Software RAID monitoring and management.
和fdisk一样,都可以对磁盘进行分区。
fdisk命令进行磁盘分区:进制是1024,更加贴合Linux中磁盘的容量
parted命令进行磁盘分区:进制是1000,更贴合厂商的硬盘划分
{24-06-03 8:52}locahost:~ root# parted /dev/sde
GNU Parted 3.2
Using /dev/sde
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mktable gpt
(parted) mkpart
Partition name? []?
File system type? [ext2]?
Start? 0
End? 5G
Warning: The resulting partition is not properly aligned for best performance: 34s % 2048s != 0s
Ignore/Cancel? Ignore
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sde: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 17.4kB 5000MB 5000MB ext2
(parted) mkpart
Partition name? []?
File system type? [ext2]?
Start? 5G
End? 100%
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sde: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 17.4kB 5000MB 5000MB ext2
2 5001MB 21.5GB 16.5GB ext2
(parted) quit
Information: You may need to update /etc/fstab.
做VDO需要2GB及以上内存
- 重删数据
- 相同的数据只存放一份,重复的数据会删除
- 虚拟大小
- 他本身有20G,但是可以对外声称2000G,建议10倍之差