Linux下的磁盘管理、挂载是一个令笔者陌生又惧怕的领域,陌生是因为笔者非专职的运维人员,在实际工作中遇到生产环境磁盘的扩容和挂载的场景少之又少,就算有也是交由更加专业运维同事处理。惧怕是因为习惯了使用Windows下如Diskgenius这样可视化的工具,使用命令行操作磁盘总有点看不见摸不着的感觉,尤其是对于保存重要数据的生产环境,总会担心一失足成千古恨,造成"删库跑路"的悲剧。总之,对于有磁盘操作一定要小心再小心,<u>以下内容仅供在测试环境下学习备考RHCE使用,生产环境操作请自行评估,由此造成任何影响与笔者无关。</u>
一、文章大纲
-
LVM
-
VDO卷
-
SWAP
-
挂载
-
NFS
二、LVM
LVM全称是逻辑卷管理(Logical Volume Manager),其作用通俗来说就是,管理员在给服务器分配磁盘空间时,很难预估准确配额,使用LVM分配空间后,可以在不停机不停服务的情况下,动态扩展空间大小。
网上关于LVM介绍的文章很多,但总感觉差点意思,笔者在这里以一个更加生动的场景,带领大家熟悉熟悉。
需求一:现在有两块20G磁盘,名称分别为****sdc****和****sdd****,需要将两个磁盘使用****LVM****格式配置,留着备用。
#查看磁盘情况如下,sdc和sdd是需要操作的两个20G磁盘
[root@localhost ~]# lsblk -a
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 500G 0 disk
├─sda1 8:1 0 200M 0 part /boot/efi
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 498.8G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 2G 0 lvm [SWAP]
└─centos-home 253:2 0 446.8G 0 lvm /home
sdc 8:32 0 20G 0 disk
sdd 8:48 0 20G 0 disk
sr0 11:0 1 1024M 0 rom
#使用fdisk先给磁盘分区,格式化
[root@localhost ~]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.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
Building a new DOS disklabel with disk identifier 0xdb8c233f.
The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.
Command (m for help): m #m查看帮助
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition #n添加分区
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id #t修改分区格式
u change display/entry units
v verify the partition table
w write table to disk and exit #w写入配置并退出
x extra functionality (experts only)
Command (m for help): n #新建分区
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +20G
Value out of range.
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039):
Using default value 41943039
Partition 1 of type Linux and of size 20 GiB is set
Command (m for help): t #修改分区格式
Selected partition 1
Hex code (type L to list all codes): L #L查看格式代码
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
1e Hidden W95 FAT1 80 Old Minix
Hex code (type L to list all codes): 8e #8e 为Linux LVM
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): w #写入
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
#相同步骤对sdd进行操作
[root@localhost ~]# fdisk /dev/sdd
Welcome to fdisk (util-linux 2.23.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
Building a new DOS disklabel with disk identifier 0xabcbdd8c.
The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039):
Using default value 41943039
Partition 1 of type Linux and of size 20 GiB is set
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
#操作完成后查看当前磁盘情况
[root@localhost ~]# lsblk -a
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 500G 0 disk
├─sda1 8:1 0 200M 0 part /boot/efi
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 498.8G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 2G 0 lvm [SWAP]
└─centos-home 253:2 0 446.8G 0 lvm /home
sdc 8:32 0 20G 0 disk
└─sdc1 8:33 0 20G 0 part
sdd 8:48 0 20G 0 disk
└─sdd1 8:49 0 20G 0 part
sr0 11:0 1 1024M 0 rom
经过上面操作,现在我们有两个20G,格式为Linux LVM的分区,如下图
image.png需求二:程序员小张需要使用/****opt****目录,并要求你帮忙预留35G使用空间。
分析需求,小张需要35G空间,一个磁盘空间不够,你想着干嘛35G呀,直接将sdc1和sdd1空间做成40G的LVM,配置后分配给opt目录。
#将分区sdc1和sdd1做成pv卷
[root@localhost ~]# pvcreate /dev/sdc1
Physical volume "/dev/sdc1" successfully created.
[root@localhost ~]# pvcreate /dev/sdd1
Physical volume "/dev/sdd1" successfully created.
#查看本地pv卷
[root@localhost ~]# pvscan
PV /dev/sda3 VG centos lvm2 [498.80 GiB / 4.00 MiB free]
PV /dev/sdc1 lvm2 [<20.00 GiB]
PV /dev/sdd1 lvm2 [<20.00 GiB]
Total: 3 [<538.80 GiB] / in use: 1 [498.80 GiB] / in no VG: 2 [<40.00 GiB]
#将pv卷sdc1和sdd1加入卷组vgdata
[root@localhost ~]# vgcreate vgdata /dev/sdc1 /dev/sdd1
Volume group "vgdata" successfully created
#在卷组vgdata上分配一个大小为40G,名称为lv001的lv
[root@localhost ~]# lvcreate -L 40G -n lv001 vgdata
Volume group "vgdata" has insufficient free space (10238 extents): 10240 required.
#提示空间不足,查看发现最大空间为39.99G
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name vgdata
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 39.99 GiB
PE Size 4.00 MiB
Total PE 10238
Alloc PE / Size 0 / 0
Free PE / Size 10238 / 39.99 GiB
VG UUID TEewMb-i4WA-EpuW-274D-NPSl-xDWE-R7q5Ij
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 1
Act PV 1
VG Size 498.80 GiB
PE Size 4.00 MiB
Total PE 127693
Alloc PE / Size 127692 / <498.80 GiB
Free PE / Size 1 / 4.00 MiB
VG UUID 0eA18f-Tnv8-IzuY-RMhJ-YzT2-zzoz-GoNiuK
#重新分配
[root@localhost ~]# lvcreate -L 39.99G -n lv001 vgdata
Rounding up size to full physical extent 39.99 GiB
Logical volume "lv001" created.
#对lv进行格式化,格式化为ext3格式
[root@localhost ~]# mkfs.ext3 /dev/vgdata/lv001
mke2fs 1.42.9 (28-Dec-2013)
Discarding device blocks: done
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
2621440 inodes, 10483712 blocks
524185 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
320 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
#可以使用命令查看lv001的UUID信息
[root@localhost ~]# blkid /dev/vgdata/lv001
/dev/vgdata/lv001: UUID="1231d07b-80d4-41bb-99ef-86085146e9a3" SEC_TYPE="ext2" TYPE="ext3"
#编辑fstab,挂载刚才空间到/opt下面。挂载使用路径或UUID均可
[root@localhost ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Aug 18 09:57:48 2023
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=93f2cc65-ba60-45f9-ac23-20636ff33131 /boot xfs defaults 0 0
UUID=8DBE-5CF6 /boot/efi vfat umask=0077,shortname=winnt 0 0
/dev/mapper/centos-home /home xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
/dev/vgdata/lv001 /opt ext3 defaults 0 0
#重新加载fstab
[root@localhost ~]# mount -a
#查看当前磁盘分配
[root@localhost ~]# lsblk -a
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 500G 0 disk
├─sda1 8:1 0 200M 0 part /boot/efi
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 498.8G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 2G 0 lvm [SWAP]
└─centos-home 253:2 0 446.8G 0 lvm /home
sdc 8:32 0 20G 0 disk
└─sdc1 8:33 0 20G 0 part
└─vgdata-lv001 253:3 0 40G 0 lvm /opt
sdd 8:48 0 20G 0 disk
└─sdd1 8:49 0 20G 0 part
└─vgdata-lv001 253:3 0 40G 0 lvm /opt
sr0 11:0 1 1024M 0 rom
#查看磁盘空间
[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 50G 2.2G 48G 5% /
devtmpfs devtmpfs 876M 0 876M 0% /dev
tmpfs tmpfs 887M 0 887M 0% /dev/shm
tmpfs tmpfs 887M 8.5M 879M 1% /run
tmpfs tmpfs 887M 0 887M 0% /sys/fs/cgroup
/dev/sda2 xfs 1014M 137M 878M 14% /boot
/dev/sda1 vfat 200M 12M 189M 6% /boot/efi
/dev/mapper/centos-home xfs 447G 33M 447G 1% /home
tmpfs tmpfs 178M 0 178M 0% /run/user/0
/dev/mapper/vgdata-lv001 ext3 40G 49M 38G 1% /opt
经过上面操作后,我们成功将两个20G硬盘做成一个40G空间分配给/opt,在上面操作过程中,大家也可以看到引入pv、vg、lv等等一些概念,不要着急,听我慢慢道来:
-
PV(Physical Volume)物理卷,处于LVM最底层,可以是物理硬盘或分区。
-
VG(Volume Group)卷组,多个PV组成VG。
如下图,先在磁盘上建立分区sdc1、sdd1,将分区做成PV卷,将PV卷添加vgdata的VG卷组,这样LVM卷组就准备好了。
image.png接下来,我们在建好的VG卷组上划分一块区域给opt,划分出来的区域叫LV,相当于原来分区。
- LV(Logical Volume)逻辑卷,相当于原来分区,建立在VG之上,可以动态分配大小。
我们在vgdata上分配空间lv001,接下来操作和正常挂载操作一样,先格式化,然后配置挂载,最后效果如下:
image.png需求三:小张找到你,说只需要35G的空间。不要40G空间。。。。。。
你心想,幸亏自己使用了LVM技术,不然得被他搞死了。
#使用lvreduce命令,-L后面指定空间大小
[root@localhost ~]# lvreduce -L 35G -r /dev/mapper/vgdata-lv001
Do you want to unmount "/opt" ? [Y|n] y
fsck from util-linux 2.23.2
/dev/mapper/vgdata-lv001: 11/2621440 files (0.0% non-contiguous), 209587/10483712 blocks
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/mapper/vgdata-lv001 to 9175040 (4k) blocks.
The filesystem on /dev/mapper/vgdata-lv001 is now 9175040 blocks long.
Size of logical volume vgdata/lv001 changed from 39.99 GiB (10238 extents) to 35.00 GiB (8960 extents).
Logical volume vgdata/lv001 successfully resized.
#查看
[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 50G 2.2G 48G 5% /
devtmpfs devtmpfs 876M 0 876M 0% /dev
tmpfs tmpfs 887M 0 887M 0% /dev/shm
tmpfs tmpfs 887M 8.4M 879M 1% /run
tmpfs tmpfs 887M 0 887M 0% /sys/fs/cgroup
/dev/sda2 xfs 1014M 137M 878M 14% /boot
/dev/sda1 vfat 200M 12M 189M 6% /boot/efi
/dev/mapper/centos-home xfs 447G 33M 447G 1% /home
tmpfs tmpfs 178M 0 178M 0% /run/user/0
/dev/mapper/vgdata-lv001 ext3 35G 49M 33G 1% /opt
[root@localhost ~]# lsblk -a
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 500G 0 disk
├─sda1 8:1 0 200M 0 part /boot/efi
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 498.8G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 2G 0 lvm [SWAP]
└─centos-home 253:2 0 446.8G 0 lvm /home
sdc 8:32 0 20G 0 disk
└─sdc1 8:33 0 20G 0 part
└─vgdata-lv001 253:3 0 35G 0 lvm /opt
sdd 8:48 0 20G 0 disk
└─sdd1 8:49 0 20G 0 part
└─vgdata-lv001 253:3 0 35G 0 lvm /opt
sr0 11:0 1 1024M 0 rom
使用lvreduce收缩空间后,效果如下:
image.png需求四:小张又找到你,空间不够用,经过测算需要50G。。。。。。。
行吧,就让你折腾吧,你心想。
思路:
现在sdc和sdd两个盘空间只有40G,如果要扩充50G,需要再增加一个20G硬盘,将硬盘加入vgdata的VG卷,然后再扩容lv001 LV逻辑卷。
说干就干
#添加20G磁盘sdb
[root@localhost ~]# lsblk -a
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 500G 0 disk
├─sda1 8:1 0 200M 0 part /boot/efi
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 498.8G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 2G 0 lvm [SWAP]
└─centos-home 253:2 0 446.8G 0 lvm /home
sdb 8:16 0 20G 0 disk
sdc 8:32 0 20G 0 disk
└─sdc1 8:33 0 20G 0 part
└─vgdata-lv001 253:3 0 35G 0 lvm /opt
sdd 8:48 0 20G 0 disk
└─sdd1 8:49 0 20G 0 part
└─vgdata-lv001 253:3 0 35G 0 lvm /opt
sr0 11:0 1 1024M 0 rom
#按上面操作,先做分区,再做pv,然后加入vg
[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.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
Building a new DOS disklabel with disk identifier 0x8725ccca.
The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039):
Using default value 41943039
Partition 1 of type Linux and of size 20 GiB is set
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
[root@localhost ~]# lsblk -a
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 500G 0 disk
├─sda1 8:1 0 200M 0 part /boot/efi
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 498.8G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 2G 0 lvm [SWAP]
└─centos-home 253:2 0 446.8G 0 lvm /home
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
sdc 8:32 0 20G 0 disk
└─sdc1 8:33 0 20G 0 part
└─vgdata-lv001 253:3 0 35G 0 lvm /opt
sdd 8:48 0 20G 0 disk
└─sdd1 8:49 0 20G 0 part
└─vgdata-lv001 253:3 0 35G 0 lvm /opt
sr0 11:0 1 1024M 0 rom
#查看vgs空间
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 3 0 wz--n- 498.80g 4.00m
vgdata 2 1 0 wz--n- 39.99g 4.99g
#使用vgextend将pv sdb1加入vg vgdata
[root@localhost ~]# vgextend vgdata /dev/sdb1
Volume group "vgdata" successfully extended
#查看vgs空间
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 3 0 wz--n- 498.80g 4.00m
vgdata 3 1 0 wz--n- <59.99g <24.99g
#目前vg已经有60G空间,可以给lv扩容
[root@localhost ~]# lvextend -L 50G -r /dev/mapper/vgdata-lv001
Size of logical volume vgdata/lv001 changed from 35.00 GiB (8960 extents) to 50.00 GiB (12800 extents).
Logical volume vgdata/lv001 successfully resized.
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mapper/vgdata-lv001 is mounted on /opt; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 4
The filesystem on /dev/mapper/vgdata-lv001 is now 13107200 blocks long.
#也可以使用下面命令,给lv001增加15G空间
# lvextend -L +15G -r /dev/mapper/vgdata-lv001
[root@localhost ~]# lsblk -a
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 500G 0 disk
├─sda1 8:1 0 200M 0 part /boot/efi
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 498.8G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 2G 0 lvm [SWAP]
└─centos-home 253:2 0 446.8G 0 lvm /home
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
└─vgdata-lv001 253:3 0 50G 0 lvm /opt
sdc 8:32 0 20G 0 disk
└─sdc1 8:33 0 20G 0 part
└─vgdata-lv001 253:3 0 50G 0 lvm /opt
sdd 8:48 0 20G 0 disk
└─sdd1 8:49 0 20G 0 part
└─vgdata-lv001 253:3 0 50G 0 lvm /opt
sr0 11:0 1 1024M 0 rom
[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 50G 2.2G 48G 5% /
devtmpfs devtmpfs 876M 0 876M 0% /dev
tmpfs tmpfs 887M 0 887M 0% /dev/shm
tmpfs tmpfs 887M 8.5M 879M 1% /run
tmpfs tmpfs 887M 0 887M 0% /sys/fs/cgroup
/dev/sda2 xfs 1014M 137M 878M 14% /boot
/dev/sda1 vfat 200M 12M 189M 6% /boot/efi
/dev/mapper/centos-home xfs 447G 33M 447G 1% /home
tmpfs tmpfs 178M 0 178M 0% /run/user/0
/dev/mapper/vgdata-lv001 ext3 50G 52M 47G 1% /opt
目前已完成扩容。
image.png三、VDO卷
VDO全称是虚拟数据优化(Virtual Data <u>Optimize</u>),是Linux的一个内核模块,从内核层对磁盘空间进行压缩和删重,以节省磁盘空间,存储更多数据。
#安装模块
[root@localhost ~]# yum install -y vdo kmod-kvdo
#给虚拟机添加一个10G新磁盘,sde
[root@localhost ~]# lsblk -a
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 500G 0 disk
├─sda1 8:1 0 200M 0 part /boot/efi
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 498.8G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 2G 0 lvm [SWAP]
└─centos-home 253:2 0 446.8G 0 lvm /home
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
└─vgdata-lv001 253:3 0 50G 0 lvm /opt
sdc 8:32 0 20G 0 disk
└─sdc1 8:33 0 20G 0 part
└─vgdata-lv001 253:3 0 50G 0 lvm /opt
sdd 8:48 0 20G 0 disk
└─sdd1 8:49 0 20G 0 part
└─vgdata-lv001 253:3 0 50G 0 lvm /opt
sde 8:64 0 10G 0 disk
sr0 11:0 1 1024M 0 rom
#将服务设置自启并启动
[root@localhost ~]# systemctl enable --now vdo
#创建vdo
[root@localhost ~]# vdo create --name=vdough --device=/dev/sde --vdoLogicalSize=20G
Creating VDO vdough
vdo: ERROR - Kernel module kvdo not installed
vdo: ERROR - modprobe: FATAL: Module kvdo not found.
#上面提示未加载,先把服务器重启下
[root@localhost ~]# reboot
#再次创建,成功
[root@localhost ~]# vdo create --name=vdough --device=/dev/sde --vdoLogicalSize=20G
Creating VDO vdough
Starting VDO vdough
Starting compression on VDO vdough
VDO instance 0 volume is ready at /dev/mapper/vdough
#查看新建vdo
[root@localhost ~]# vdo list
vdough
#或使用 vdostats --human-readable
#查看vdo状态信息
[root@localhost ~]# vdo status -n vdough
VDO status:
Date: '2023-10-24 04:51:01-04:00'
Node: localhost.localdomain
Kernel module:
Loaded: true
Name: kvdo
Version information:
kvdo version: 6.1.3.23
Configuration:
File: /etc/vdoconf.yml
Last modified: '2023-10-24 04:49:31'
VDOs:
vdough:
Acknowledgement threads: 1
Activate: enabled
Bio rotation interval: 64
Bio submission threads: 4
Block map cache size: 128M
Block map period: 16380
Block size: 4096
CPU-work threads: 2
Compression: enabled
Configured write policy: auto
Deduplication: enabled
Device mapper status: 0 41943040 vdo /dev/sde normal - online online 1049638 2621440
Emulate 512 byte: disabled
.....
#创建挂载测试目录
[root@localhost ~]# mkdir /vdotest
#格式化
[root@localhost ~]# mkfs.xfs -K /dev/mapper/vdough
meta-data=/dev/mapper/vdough isize=512 agcount=4, agsize=1310720 blks
= sectsz=4096 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=5242880, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=4096 sunit=1 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
#编辑fstab进行挂载
[root@localhost ~]# vim /etc/fstab
[root@localhost ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Aug 18 09:57:48 2023
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=93f2cc65-ba60-45f9-ac23-20636ff33131 /boot xfs defaults 0 0
UUID=8DBE-5CF6 /boot/efi vfat umask=0077,shortname=winnt 0 0
/dev/mapper/centos-home /home xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
/dev/vgdata/lv001 /opt ext3 defaults 0 0
/dev/mapper/vdough /vdotest xfs defaults,x-systemd.requires=vdo.service 0 0
#重新加载fstab
[root@localhost ~]# mount -a
#查看挂载情况
[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 875M 0 875M 0% /dev
tmpfs tmpfs 887M 0 887M 0% /dev/shm
tmpfs tmpfs 887M 8.4M 879M 1% /run
tmpfs tmpfs 887M 0 887M 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 50G 2.5G 48G 5% /
/dev/sda2 xfs 1014M 180M 835M 18% /boot
/dev/sda1 vfat 200M 12M 189M 6% /boot/efi
/dev/mapper/centos-home xfs 447G 33M 447G 1% /home
/dev/mapper/vgdata-lv001 ext3 50G 52M 47G 1% /opt
tmpfs tmpfs 178M 0 178M 0% /run/user/0
/dev/mapper/vdough xfs 20G 33M 20G 1% /vdotest
[root@localhost ~]# lsblk -a
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 500G 0 disk
├─sda1 8:1 0 200M 0 part /boot/efi
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 498.8G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 2G 0 lvm [SWAP]
└─centos-home 253:2 0 446.8G 0 lvm /home
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
└─vgdata-lv001 253:3 0 50G 0 lvm /opt
sdc 8:32 0 20G 0 disk
└─sdc1 8:33 0 20G 0 part
└─vgdata-lv001 253:3 0 50G 0 lvm /opt
sdd 8:48 0 20G 0 disk
└─sdd1 8:49 0 20G 0 part
└─vgdata-lv001 253:3 0 50G 0 lvm /opt
sde 8:64 0 10G 0 disk
└─vdough 253:4 0 20G 0 vdo /vdotest
sr0 11:0 1 1024M 0 rom
#删除vdo,先卸载
[root@localhost ~]# umount /vdotest
#去掉加载项
[root@localhost ~]# vim /etc/fstab
#移除
[root@localhost ~]# vdo remove --name=vdough
Removing VDO vdough
Stopping VDO vdough
四、SWAP
SWAP空间比较特殊,是作为内存的缓存空间,一般是内存两倍大小,操作如下:
#将上面使用sde磁盘做成swap
[root@localhost ~]# lsblk -a
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 500G 0 disk
├─sda1 8:1 0 200M 0 part /boot/efi
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 498.8G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 2G 0 lvm [SWAP]
└─centos-home 253:2 0 446.8G 0 lvm /home
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
└─vgdata-lv001 253:3 0 50G 0 lvm /opt
sdc 8:32 0 20G 0 disk
└─sdc1 8:33 0 20G 0 part
└─vgdata-lv001 253:3 0 50G 0 lvm /opt
sdd 8:48 0 20G 0 disk
└─sdd1 8:49 0 20G 0 part
└─vgdata-lv001 253:3 0 50G 0 lvm /opt
sde 8:64 0 10G 0 disk
sr0 11:0 1 1024M 0 rom
#格式化为swap
[root@localhost ~]# mkswap /dev/sde
Setting up swapspace version 1, size = 10485756 KiB
no label, UUID=52a5a7ad-1c3f-4053-896b-c196a222fc03
#配置fstab
[root@localhost ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Aug 18 09:57:48 2023
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=93f2cc65-ba60-45f9-ac23-20636ff33131 /boot xfs defaults 0 0
UUID=8DBE-5CF6 /boot/efi vfat umask=0077,shortname=winnt 0 0
/dev/mapper/centos-home /home xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
/dev/vgdata/lv001 /opt ext3 defaults 0 0
UUID=52a5a7ad-1c3f-4053-896b-c196a222fc03 swap swap defaults 0 0
#激活swap,注意这里需要使用swapon命令
[root@localhost ~]# swapon -a
#查看已经成功加载
[root@localhost ~]# lsblk -a
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 500G 0 disk
├─sda1 8:1 0 200M 0 part /boot/efi
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 498.8G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 2G 0 lvm [SWAP]
└─centos-home 253:2 0 446.8G 0 lvm /home
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
└─vgdata-lv001 253:3 0 50G 0 lvm /opt
sdc 8:32 0 20G 0 disk
└─sdc1 8:33 0 20G 0 part
└─vgdata-lv001 253:3 0 50G 0 lvm /opt
sdd 8:48 0 20G 0 disk
└─sdd1 8:49 0 20G 0 part
└─vgdata-lv001 253:3 0 50G 0 lvm /opt
sde 8:64 0 10G 0 disk [SWAP]
sr0 11:0 1 1024M 0 rom
五、挂载
挂载指将外部的存储空间挂载到本地目录进行文件访问和管理相关操作,下面演示常用挂载ISO文件和SMB共享场景:
挂载ISO文件至本地
#将DVD镜像文件上传至服务器
[root@servera home]# pwd
/home
[root@servera home]# ll
total 4481024
-rw-r--r--. 1 root root 4588568576 Sep 12 04:53 CentOS-7-x86_64-DVD-1810.iso
#切换至media目录,新建文件夹dvd
[root@servera home]# cd /media/
[root@servera media]# mkdir dvd
#将iso文件挂载至dvd目录
[root@servera media]# mount -o loop -t iso9660 /home/Cent
mount: /dev/loop0 is write-protected, mounting read-only
#参数说明:
#-t 指定挂载文件系统的类型,通常不用指定系统可以自动识别
# iso9660: 光盘
# msdos: DOS fat16文件系统
# ntfs:ntfs文件系统
# smbfs:SMB文件共享
# nfs:NFS文件共享
#-o 参数
#loop
# loop:用来把一个文件当成硬盘分区挂接上系统
# ro:采用只读方式挂接设备
# rw:采用读写方式挂接设备
# iocharset:指定访问文件系统所用字符集
#查看目录,已经成功挂载
[root@servera media]# ll dvd/
total 686
-rw-rw-r--. 1 root root 14 Nov 25 2018 CentOS_BuildTag
drwxr-xr-x. 3 root root 2048 Nov 25 2018 EFI
-rw-rw-r--. 1 root root 227 Aug 30 2017 EULA
-rw-rw-r--. 1 root root 18009 Dec 9 2015 GPL
drwxr-xr-x. 3 root root 2048 Nov 25 2018 images
drwxr-xr-x. 2 root root 2048 Nov 25 2018 isolinux
drwxr-xr-x. 2 root root 2048 Nov 25 2018 LiveOS
drwxrwxr-x. 2 root root 663552 Nov 25 2018 Packages
drwxrwxr-x. 2 root root 4096 Nov 25 2018 repodata
-rw-rw-r--. 1 root root 1690 Dec 9 2015 RPM-GPG-KEY-CentOS-7
-rw-rw-r--. 1 root root 1690 Dec 9 2015 RPM-GPG-KEY-CentOS-Testing-7
-r--r--r--. 1 root root 2883 Nov 25 2018 TRANS.TBL
#使用mount命令可以查看当前挂载项
[root@servera media]# mount |grep dvd
/home/CentOS-7-x86_64-DVD-1810.iso on /media/dvd type iso9660 (ro,relatime)
#使用umount取消挂载
[root@servera media]# umount /media/dvd
[root@servera media]# mount |grep dvd
[root@servera media]# ll /media/dvd/
total 0
#上述挂载命令只能临时作用,若要永久修改配置可以在/etc/fstab中配置
[root@servera ~]# vim /etc/fstab
/home/CentOS-7-x86_64-DVD-1810.iso /media/dvd iso9660 defaults,ro,loop 0 0
#重启服务器后查看,已经成功挂载
[root@servera ~]# ls /media/dvd/
CentOS_BuildTag EULA images LiveOS repodata RPM-GPG-KEY-CentOS-Testing-7
EFI GPL isolinux Packages RPM-GPG-KEY-CentOS-7 TRANS.TBL
挂载SMB共享至本地
在Windows测试机下添加文件共享专用用户
output.png添加共享文件夹
output (1).png在服务器上进行下面配置
# 安装smb客户端
[root@servera ~]# yum install samba-client
# 查看共享目录
[root@servera ~]# smbclient -L 172.25.105.23 -U fileuser%abcd1234
Sharename Type Comment
--------- ---- -------
ADMIN$ Disk 远程管理
C$ Disk 默认共享
IPC$ IPC 远程 IPC
Share Disk
Reconnecting with SMB1 for workgroup listing.
do_connect: Connection to 172.25.105.23 failed (Error NT_STATUS_RESOURCE_NAME_NOT_FOUND)
Unable to connect with SMB1 -- no workgroup available
#查看共享的文件
[root@servera ~]# smbclient -c 'ls' //172.25.105.23/Share -U fileuser%abcd1234
. D 0 Wed Sep 13 22:25:41 2023
.. D 0 Wed Sep 13 22:25:41 2023
New Text Document.txt A 5 Wed Sep 13 22:25:49 2023
10452731 blocks of size 4096\. 9807068 blocks available
#临时挂载,创建挂载目录
[root@servera ~]# cd /mnt/
[root@servera mnt]# mkdir share
#使用命令临时挂载
[root@servera mnt]# mount -t cifs //172.25.105.23/Share /mnt/share/ -o username=fileuser,password=abcd1234
#查看挂载文件
[root@servera mnt]# ll share/
total 1
-rwxr-xr-x. 1 root root 5 Sep 13 22:25 New Text Document.txt
[root@servera mnt]# cat share/New\ Text\ Document.txt
hello
#永久挂载
#取消临时挂载
[root@servera mnt]# umount /mnt/share
#配置fstab文件
[root@servera ~]# cat /etc/fstab |grep share
//172.25.105.23/Share /mnt/share cifs defaults,auto,username=fileuser,password=abcd1234 0 0
[root@servera ~]# ll /mnt/share/
total 1
-rwxr-xr-x. 1 root root 5 Sep 13 22:25 New Text Document.txt
六、NFS
NFS全称为(Network File System)网络文件管理系统,顾名思义是用作网络文件共享和管理,与之相对是Windows下常用SMB(Server Message Block)协议。都可用于在局域网进行文件共享。关于NFS详细背景、优缺点介绍不在本文讨论范围内,只需了解其为Linux下常用文件共享协议,可用于NAS或服务器间的文件共享。且在RHCE考点中有相关考题,必须要掌握。
NFS服务端搭建
配置NFS服务端
#由于NFS需要依赖RPC,所以搭建NFS服务前,需要先安装RPC
[root@servera ~]# yum install -y rpcbind nfs-utils
#启动服务
[root@servera ~]# systemctl enable --now rpcbind
#检查rpc服务状态
[root@servera ~]# rpcinfo -p
program vers proto port service
100000 4 tcp 111 portmapper
100000 3 tcp 111 portmapper
100000 2 tcp 111 portmapper
100000 4 udp 111 portmapper
100000 3 udp 111 portmapper
100000 2 udp 111 portmapper
#启动nfs服务
[root@servera ~]# systemctl enable --now nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
#新建共享目录,并准备一些文件用于共享
[root@servera ~]# cd /
[root@servera /]# mkdir data
[root@servera /]# cp /etc/passwd /data/
[root@servera /]#
#配置共享
[root@servera ~]# vim /etc/exports
[root@servera ~]# cat /etc/exports
/data 172.20.126.212(rw,sync)
#路径 允许主机(可以使用通配符*) 权限(rw,可读写;sync,同步写操作)
#重启服务
[root@servera ~]# systemctl reload nfs-server.service
#也可以使用如下命令重新加载
[root@servera ~]# exportfs -r
完成上面配置后,NFS服务端已经配置完毕,现在还存在一个问题就是远程客户端访问时需要在防火墙放开,NFS主要使用端口有:
-
111/tcp
-
111/udp
-
2049/tcp
-
2049/udp
-
20048/tcp
-
20048/udp
服务端的防火墙放开操作如下:
#将端口放开
[root@servera ~]# firewall-cmd --permanent --zone=public --add-port=111/tcp
success
[root@servera ~]# firewall-cmd --permanent --zone=public --add-port=111/udp
success
[root@servera ~]# firewall-cmd --permanent --zone=public --add-port=2049/tcp
success
[root@servera ~]# firewall-cmd --permanent --zone=public --add-port=2049/udp
success
[root@servera ~]# firewall-cmd --permanent --zone=public --add-port=20048/udp
success
[root@servera ~]# firewall-cmd --permanent --zone=public --add-port=20048/tcp
success
#重新加载,,使策略生效
[root@servera ~]# firewall-cmd --reload
success
NFS客户端配置
查看共享情况
#安装nfs
[root@serverb ~]# yum install nfs-utils
#查看服务端共享情况(出现下面错误说明服务端防火墙配置有问题,需要在服务端将端口放开)
[root@serverb ~]# showmount -e 172.20.117.139
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)
#在服务端放开端口后,可以看到共享的文件路径
[root@serverb ~]# showmount -e 172.20.117.139
Export list for 172.20.117.139:
/data 172.20.126.212
将服务端挂载本地
#在客户端新建data目录
[root@serverb ~]# mkdir /data
#将远程data目录挂载在本地下
[root@serverb ~]# mount -t nfs 172.20.117.139:/data /data
#查看目录,可以发现远端文件已经可以查看到了,说明挂载成功
[root@serverb ~]# ll /data/
total 4
-rw-r--r--. 1 root root 1034 Sep 4 22:17 passwd
#以上只能临时挂载,在服务器重启后需要重新挂载
#可以使用/etc/fstab进行永久挂载
#先将临时挂载取消
[root@serverb ~]# umount /data
#配置fstab,在行末添加
[root@serverb ~]# vim /etc/fstab
172.20.117.139:/data /data nfs defaults 0 0
#让配置生效
[root@serverb ~]# mount -a
#查看
[root@serverb ~]# ll /data
total 4
-rw-r--r--. 1 root root 1034 Sep 4 22:17 passwd
使用autofs
通过上面操作,我们已经成功搭建NFS服务,并将服务下data目录成功永久性挂载到了本地。fstab会在服务器每次开机启动时将远程盘挂载至本地。但随着服务器挂载越来越多,占用服务器资源和网络带宽也会越来越多,这个时候会思考这样一个问题,有些挂载资源不经常使用,是否可以在使用的时候挂载,在不使用的时候卸载掉呢?接下来我们学习使用autofs对挂载资源进行配置,autofs会在使用资源时自动挂载,在不使用资源时自动卸载,从而节约服务器资源和网络资源的占用。
#首先我们将上面fstab配置删除掉
[root@serverb ~]# vi /etc/fstab
[root@serverb ~]# mount -a
[root@serverb ~]# umount /data
#安装autofs服务
[root@serverb ~]# yum install -y autofs
#将服务配置开机启动
[root@serverb ~]# systemctl enable autofs
Created symlink from /etc/systemd/system/multi-user.target.wants/autofs.service to /usr/lib/systemd/system/autofs.service.
#配置
[root@serverb ~]# vim /etc/auto.master.d/netdir.autofs
/data /etc/auto.netdir
#本机路径 对应挂载的配置文件
[root@serverb ~]# vim /etc/auto.netdir
share -fstype=nfs,rw,sync 172.20.117.139:/data
#子目录 相关参数 挂载路径
#说明一:这里子目录为share,对应完整路径为/data/share,与手动挂载目录稍有不同
#说明二:注意参数前有中划线,一定不要忘记了,笔者就因为考试时忘记中划线一直挂载不成功
#查看data目录,发现并无文件,这里因为没有调用该路径,所以系统并未挂载
[root@serverb ~]# ll /data/
total 0
#查看share目录,这个时候autofs发现有访问,进行自动挂载的
[root@serverb ~]# ll /data/share
total 4
-rw-r--r--. 1 root root 1034 Sep 4 22:17 passwd
网友评论