磁盘管理
基本分区管理
磁盘划分思路
- 进入分区表,新建分区
- 更新分区表
- 格式化分区表
- 挂载使用
#lsblk
#df -h 查看设备挂载情况
#fdisk -l 设备分区情况
#fdisk /dev/sdb
添加一块硬盘,需要将其分两个分区,分别格式化成ext4和vfat格式文件系统使用,最终需要使用2G空间
- 添加硬盘
- fdisk进行分区
- 格式化指定分区
- 创建空目录作为挂载点
- 挂载使用
#添加硬盘
[root@workstation ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos_servera-root 253:0 0 17G 0 lvm /
└─centos_servera-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
sr0 11:0 1 918M 0 rom
#fdisk分区
[root@workstation ~]# fdisk /dev/sdb
Command (m for help): p #打印分区表信息
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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
Disk label type: dos
Disk identifier: 0x46d52402
Device Boot Start End Blocks Id System
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): +1G #分区大小
Partition 1 of type Linux and of size 1 GiB is set
#再次新建一个分区
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p):
Using default response p
Partition number (2-4, default 2): 2
First sector (2099200-41943039, default 2099200):
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-41943039, default 41943039): +1G
Partition 2 of type Linux and of size 1 GiB is set
#打印信息
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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
Disk label type: dos
Disk identifier: 0x46d52402
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
/dev/sdb2 2099200 4196351 1048576 83 Linux
#保存退出
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
#查看分区情况
[root@workstation ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos_servera-root 253:0 0 17G 0 lvm /
└─centos_servera-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 1G 0 part
└─sdb2 8:18 0 1G 0 part
sr0 11:0 1 918M 0 rom
#刷新分区表信息
[root@workstation ~]# partx -d /dev/sdb
[root@workstation ~]# partx -a /dev/sdb
[root@workstation ~]# partx -s /dev/sdb
NR START END SECTORS SIZE NAME UUID
1 2048 2099199 2097152 1G
2 2099200 4196351 2097152 1G
#格式化分区
[root@workstation ~]# mkfs.ext4 /dev/sdb1
[root@workstation ~]# yum install -y dosfstools
[root@workstation ~]# mkfs.vfat /dev/sdb2
mkfs.fat 3.0.20 (12 Jun 2013)
#创建新挂载点
[root@workstation ~]# mkdir /u01
[root@workstation ~]# mkdir /u02
#挂载
[root@workstation ~]# mount /dev/sdb1 /u01
[root@workstation ~]# mount /dev/sdb2 /u02
挂载分区设备
手动挂载
- 设备重启后需要重新挂载
- 手动卸载后需要手动挂载
#查看块设备信息
[root@workstation ~]# blkid /dev/sdb1
/dev/sdb1: UUID="78d4daac-317c-497e-9eb7-5e782fcfded7" TYPE="ext4"
[root@workstation /]# mount -o rw /dev/sdb1 /u01
[root@workstation /]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos_servera-root 253:0 0 17G 0 lvm /
└─centos_servera-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 1G 0 part /u01
└─sdb2 8:18 0 1G 0 part
sr0 11:0 1 918M 0 rom
#卸载
[root@workstation /]# umount /u01
[root@workstation /]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos_servera-root 253:0 0 17G 0 lvm /
└─centos_servera-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 1G 0 part
└─sdb2 8:18 0 1G 0 part
sr0 11:0 1 918M 0 rom
开机自动挂载
- 硬件初始化 硬盘,内存…
- 系统初始化 /sbin/init > /etc/fstab
- 系统重启后自动挂载
#系统自动挂载/dev/sdb1设备 2分钟没有被使用 自动卸载
yum -y install autofs
[root@workstation ~]# rpm -q autofs
autofs-5.0.7-116.el7_9.1.x86_64
#修改配置文件
[root@workstation ~]# vi /etc/auto.master
[root@workstation ~]# vi /etc/auto.test
[root@workstation ~]# cat /etc/auto.master | grep u01
/u01 /etc/auto.test -t 120
[root@workstation ~]# cat /etc/auto.test | grep test
test -fstype=ext4,ro :/dev/sdb1
#重启服务
[root@workstation ~]# service autofs restart
#测试验证
[root@workstation ~]# ls /u01/test
lost+found
[root@workstation ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos_servera-root 17G 1.2G 16G 7% /
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 1014M 133M 882M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/sdb1 976M 2.6M 907M 1% /u01/test
逻辑卷管理
简介
LVM:对Linux环境下对磁盘分区进行管理的一种机制,建立在物理存储设备之上的一个抽象层,优点在于灵活管理。
- 在线扩容
- 离线裁剪
- 数据条带化
- 数据镜像
名称 | 定义 |
---|---|
物理设备 | 保存LVM所存储的存储设备。 |
物理卷(PV) | 物理卷时LVM所使用的基础物理存储。在LVM系统使用设备之前,必须将设备初始化为物理卷。 |
卷组(VG) | 由一或多个物理卷组成。功能上与磁盘相同,一个PV分配一个VG,一个VG包含多个PV |
逻辑卷(LV) | 根据卷组中空闲物理区块创建,提供应用,用户和操作系统所使用的“存储”设备。 |
- 物理设备
- 命令创建
- 物理卷(PV)
- 卷组(VG)
- 逻辑卷(LV)
- 逻辑卷格式化
- 挂载使用
创建逻辑卷
创建一个1.5G的逻辑卷
[root@workstation ~]# lsblk | grep sdb
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 1G 0 part
├─sdb2 8:18 0 1G 0 part
├─sdb3 8:19 0 1G 0 part
├─sdb4 8:20 0 1K 0 part
└─sdb5 8:21 0 100M 0 part
#创建物理卷
[root@workstation ~]# pvcreate /dev/sdb1 /dev/sdb2
[root@workstation ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos_servera lvm2 a-- <19.00g 0
/dev/sdb1 lvm2 --- 1.00g 1.00g
/dev/sdb2 lvm2 --- 1.00g 1.00g
#将创建卷组并将物理卷加入其中
[root@workstation ~]# vgcreate vg01 /dev/sdb1 /dev/sdb2
Volume group "vg01" successfully created
[root@workstation ~]# vgs vg01
VG #PV #LV #SN Attr VSize VFree
vg01 2 0 0 wz--n- 1.99g 1.99g
[root@workstation ~]# vgdisplay vg01
--- Volume group ---
VG Name vg01
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 1.99 GiB
PE Size 4.00 MiB
Total PE 510
Alloc PE / Size 0 / 0
Free PE / Size 510 / 1.99 GiB
VG UUID ABTzbY-lO5x-4HA2-9ZtE-hFht-Y3Ng-fgNIYN
#创建逻辑卷
[root@workstation ~]# lvcreate -n lv01 -L 1.5G vg01
Logical volume "lv01" created.
#被映射到两个地方
[root@workstation ~]# ll /dev/mapper/vg01-lv01
lrwxrwxrwx. 1 root root 7 Aug 3 05:11 /dev/mapper/vg01-lv01 -> ../dm-2
[root@workstation ~]# ll /dev/vg01/lv01
lrwxrwxrwx. 1 root root 7 Aug 3 05:11 /dev/vg01/lv01 -> ../dm-2
[root@workstation ~]# lvdisplay /dev/vg01/lv01
--- Logical volume ---
LV Path /dev/vg01/lv01
LV Name lv01
VG Name vg01
LV UUID j2lsmi-SYjW-Qpn0-btqf-1fBF-tkQf-4eH5fc
LV Write Access read/write
LV Creation host, time workstation, 2023-08-03 05:11:18 -0400
LV Status available
# open 0
LV Size 1.50 GiB
Current LE 384
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
#创建大小为100M的逻辑卷,每个PE为4M,指定25个PE,大小为100M
[root@workstation ~]# lvcreate -n lv02 -l25 vg01
Logical volume "lv02" created.
[root@workstation ~]# vgs vg01
VG #PV #LV #SN Attr VSize VFree
vg01 2 2 0 wz--n- 1.99g 404.00m
[root@workstation ~]# lvs /dev/vg01/lv02
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv02 vg01 -wi-a----- 100.00m
#格式化逻辑卷
[root@workstation ~]# mkfs.ext4 /dev/vg01/lv01
#挂载
[root@workstation ~]# mkdir /mnt01
[root@workstation ~]# mount /dev/vg01/lv01 /mnt01
[root@workstation ~]# lsblk | grep lv
├─centos_servera-root 253:0 0 17G 0 lvm /
└─centos_servera-swap 253:1 0 2G 0 lvm [SWAP]
│ └─vg01-lv01 253:2 0 1.5G 0 lvm /mnt01
│ ├─vg01-lv01 253:2 0 1.5G 0 lvm /mnt01
│ └─vg01-lv02 253:3 0 100M 0 lvm
逻辑卷动态扩容
将/mnt01目录动态扩容到2.2G
- 查看/mnt01目录对应的逻辑卷是哪个
- 查看当前逻辑卷的卷组剩余空间是否足够
- 如果卷组空间不够,先扩容卷组,再扩容逻辑卷
- 如果卷组空间足够,直接扩容逻辑卷
#查看/mnt01属于哪个卷组
[root@workstation ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos_servera-root 17G 1.2G 16G 7% /
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 1014M 133M 882M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/vg01-lv01 1.5G 4.5M 1.4G 1% /mnt01
[root@workstation ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos_servera -wi-ao---- <17.00g
swap centos_servera -wi-ao---- 2.00g
lv01 vg01 -wi-ao---- 1.50g
lv02 vg01 -wi-a----- 100.00m
#查看卷组剩余空间
[root@workstation ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos_servera 1 2 0 wz--n- <19.00g 0
vg01 2 2 0 wz--n- 1.99g 404.00m
#当前卷组空间不足
#扩容逻辑卷所在卷组
#首先得有物理设备
[root@workstation ~]# pvcreate /dev/sdb3
Physical volume "/dev/sdb3" successfully created.
[root@workstation ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos_servera lvm2 a-- <19.00g 0
/dev/sdb1 vg01 lvm2 a-- 1020.00m 0
/dev/sdb2 vg01 lvm2 a-- 1020.00m 404.00m
/dev/sdb3 lvm2 --- 1.00g 1.00g
[root@workstation ~]# vgextend vg01 /dev/sdb3
Volume group "vg01" successfully extended
[root@workstation ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos_servera lvm2 a-- <19.00g 0
/dev/sdb1 vg01 lvm2 a-- 1020.00m 0
/dev/sdb2 vg01 lvm2 a-- 1020.00m 404.00m
/dev/sdb3 vg01 lvm2 a-- 1020.00m 1020.00m
#扩容逻辑卷
[root@workstation ~]# lvextend -L 2.2G /dev/vg01/lv01
Rounding size to boundary between physical extents: 2.20 GiB.
Size of logical volume vg01/lv01 changed from 2.00 GiB (512 extents) to 2.20 GiB (564 extents).
Logical volume vg01/lv01 successfully resized.
[root@workstation ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos_servera -wi-ao---- <17.00g
swap centos_servera -wi-ao---- 2.00g
lv01 vg01 -wi-ao---- 2.20g
lv02 vg01 -wi-a----- 100.00m
[root@workstation ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos_servera-root 17G 1.2G 16G 7% /
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 1014M 133M 882M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/vg01-lv01 1.5G 4.5M 1.4G 1% /mnt01
#同步文件系统
[root@workstation ~]# resize2fs /dev/vg01/lv01
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg01/lv01 is mounted on /mnt01; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vg01/lv01 is now 577536 blocks long.
[root@workstation ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos_servera-root 17G 1.2G 16G 7% /
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 1014M 133M 882M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/vg01-lv01 2.2G 4.5M 2.1G 1% /mnt01
扩容Swap空间
增加一个硬盘或分区来扩容swap空间
#查看swap空间
[root@workstation ~]# free -m
total used free shared buff/cache available
Mem: 972 129 705 7 137 683
Swap: 2047 0 2047
[root@workstation ~]# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 2097148 0 -2
[root@workstation ~]# mkswap /dev/sdb1
mkswap: /dev/sdb1: warning: wiping old ext4 signature.
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=cf7f9c58-7078-43fa-9a7e-a2c628cf57a2
[root@workstation ~]# blkid /dev/sdb1
/dev/sdb1: UUID="cf7f9c58-7078-43fa-9a7e-a2c628cf57a2" TYPE="swap"
#激活swap分区 swap分区不能手动挂载
[root@workstation ~]# swapon /dev/sdb1
[root@workstation ~]# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 2097148 0 -2
/dev/sdb1 partition 2097148 0 -3
逻辑卷实现条带化
条带化:把保存到逻辑卷的数据分成N份,分别写到不同的物理卷,可以提高数据的读写效率,但如果任何一个涉及到的物理卷出现问题,数据就会无法恢复。
[root@workstation ~]# lsblk | grep sdc
sdc 8:32 0 20G 0 disk
├─sdc1 8:33 0 2G 0 part
└─sdc2 8:34 0 2G 0 part
#创建物理卷
[root@workstation ~]# pvcreate /dev/sdc[12]
Physical volume "/dev/sdc1" successfully created.
Physical volume "/dev/sdc2" successfully created.
#创建卷组
[root@workstation ~]# vgcreate vg01 /dev/sdc[12]
Volume group "vg01" successfully created
[root@workstation ~]# pvs /dev/sdc[12]
PV VG Fmt Attr PSize PFree
/dev/sdc1 vg01 lvm2 a-- <2.00g <2.00g
/dev/sdc2 vg01 lvm2 a-- <2.00g <2.00g
#实现条带化逻辑卷
[root@workstation ~]# lvcreate -n lv01 -L 1G vg01 -i 2 /dev/sdc[12]
Using default stripesize 64.00 KiB.
Logical volume "lv01" created.
[root@workstation ~]# lvs /dev/vg01/lv01
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv01 vg01 -wi-a----- 1.00g
[root@workstation ~]# pvs /dev/sdc[12]
PV VG Fmt Attr PSize PFree
/dev/sdc1 vg01 lvm2 a-- <2.00g <1.50g
/dev/sdc2 vg01 lvm2 a-- <2.00g <1.50g
[root@workstation ~]# mkdir /my-mnt
[root@workstation ~]# mkfs.ext4 /dev/vg01/lv01
[root@workstation ~]# mount /dev/vg01/lv01 /my-mnt