Centos7.4 磁盘卷组管理
文章目录
- Centos7.4 磁盘卷组管理
- 1. 环境和目标
- 2. 知识点
- 3. 实施步骤
- 4. 大于2T的磁盘
- 参考&附录
- 1. 逻辑卷命令总结
- 2. lvextend lvreduce lvresize区别
1. 环境和目标
- 环境:4个1T的磁盘,未分区
- 目标:
- 创建卷组,进行卷组管理
- 创建文件分区: /opt 挂载磁盘分区
- 优点:以后扩展opt会非常容易扩展,缺点:管理复杂
2. 知识点
3. 实施步骤
- 检查现有文件系统
- 检查磁盘信息
- fdisk 进行分区
- 格式化分区为ext4(附录: ext4, xfs分区类型的区别)
- 创建卷组
- 添加分区到卷组
- 创建/opt,并挂载卷组
- 编辑/etc/fstab文件
- 重启检查分区/opt是否正常
# 1. 检查现有文件系统
[root@i-vfyxrtmx network-scripts]# df -lh
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 99G 1.2G 93G 2% /
devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 7.8G 17M 7.8G 1% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
tmpfs 1.6G 0 1.6G 0% /run/user/0
# 2. 检查磁盘信息
[root@i-vfyxrtmx network-scripts]# fdisk -l
Disk /dev/sdb: 17.2 GB, 17179869184 bytes, 33554432 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 /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x00009f6f
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 209713247 104855600 83 Linux
## 以下是新的未分区磁盘
Disk /dev/sdc: 1073.7 GB, 1073741824000 bytes, 2097152000 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 /dev/sdd: 1073.7 GB, 1073741824000 bytes, 2097152000 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 /dev/sde: 1073.7 GB, 1073741824000 bytes, 2097152000 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 /dev/sdf: 1073.7 GB, 1073741824000 bytes, 2097152000 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
# 3. fdisk 进行分区
[root@i-vfyxrtmx network-scripts]# 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 0xf086d717.
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-2097151999, default 2048): ##-- 开始扇区位置: 2048
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151999, default 2097151999): ##-- 结束扇区位置(最大):2097151999
Using default value 2097151999
Partition 1 of type Linux and of size 1000 GiB is set
Command (m for help): w #-- 写分区信息
The partition table has been altered!
Calling ioctl() to re-read partition table. #-- 分区完成
Syncing disks.
# 依次用以上步骤,把 /dev/sdd, /dev/sde, /dev/sdf 进行分区
# 4. 格式化分区为ext4(附录: ext4, xfs分区类型的区别)
# 5. 创建物理卷pv, 卷组
# 5.1 创建物理卷pv, 发现pvcreate没有安装
[root@i-vfyxrtmx network-scripts]# pvcreate /dev/sdc1 /dev/sdd1
-bash: pvcreate: command not found
# 5.1.1 yum安装pvcreate
[root@i-vfyxrtmx network-scripts]# yum -y install lvm2
......
Installed:
lvm2.x86_64 7:2.02.187-6.el7_9.5
# 5.1.2 安装后,pv创建成功
# 此命令会将设备/dev/sdc1和/dev/sdd1标记为PV,准备好分配到卷组中。仅当没有空闲的PV可以创建或扩展VG时,才需要创建PV。
[root@i-vfyxrtmx network-scripts]# pvcreate /dev/sdc1 /dev/sdd1
Physical volume "/dev/sdc1" successfully created.
Physical volume "/dev/sdd1" successfully created.
# 创建卷组, 先创建一个卷组,后面测试扩展vg,把/dev/sdd1加入到卷组vg-opt中
[root@i-vfyxrtmx network-scripts]# vgcreate vg-opt /dev/sdc1
Volume group "vg-opt" successfully created
# 查看卷组信息: vgdisplay, 注意:aix,hp-unix命令可能是lsvg
[root@i-vfyxrtmx network-scripts]# vgdisplay
--- Volume group ---
VG Name vg-opt
System ID
Format lvm2
Metadata Areas 1
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 1
Act PV 1
VG Size <1000.00 GiB
PE Size 4.00 MiB
Total PE 255999
Alloc PE / Size 0 / 0
Free PE / Size 255999 / <1000.00 GiB
VG UUID A2DX7U-IgRI-CExd-pEMx-Pyg6-97Hv-siGZoa
# 创建1T的lv,但是不够,按MB创建
[root@i-vfyxrtmx network-scripts]# lvcreate -L 1000G -n lv-opt vg-opt
Volume group "vg-opt" has insufficient free space (255999 extents): 256000 required.
[root@i-vfyxrtmx network-scripts]#
[root@i-vfyxrtmx network-scripts]# lvcreate -L 1023996M -n lv-opt vg-opt
Logical volume "lv-opt" created.
# 查看已创建的 lv-opt
[root@i-vfyxrtmx network-scripts]# ls -lh /dev/vg-opt/lv-opt
lrwxrwxrwx 1 root root 7 Mar 7 12:36 /dev/vg-opt/lv-opt -> ../dm-0
# 格式化lv-opt为ext4格式
[root@i-vfyxrtmx network-scripts]# mkfs -t ext4 /dev/vg-opt/lv-opt
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
65536000 inodes, 262142976 blocks
13107148 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2409627648
8000 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, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000, 214990848
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information:
done
# 创建挂载目录,并挂载
[root@i-vfyxrtmx network-scripts]# mkdir -p /opt
[root@i-vfyxrtmx network-scripts]# mount /dev/vg-opt/lv-opt /opt/
# 查看挂载后的文件系统
[root@i-vfyxrtmx network-scripts]# df -h
Filesystem Size Used Avail Use% Mounted on
...
/dev/mapper/vg--opt-lv--opt 985G 77M 935G 1% /opt # 已挂载的 文件系统
#7. 向/etc/fstab文件添加条目, 增加如下行
[root@i-vfyxrtmx network-scripts]# vi /etc/fstab
/dev/vg-opt/lv-opt /opt ext4 defaults 1 1
# 或者 用磁盘的uuid进行挂载
[root@i-vfyxrtmx ~]# blkid
/dev/sda1: UUID="021361f1-9d1c-41c1-97ed-ca04a4252efc" TYPE="ext4"
/dev/sdf: LABEL="YUNIFYSWAP" UUID="48eb1df6-1663-4a52-ab30-040d552c2d76" TYPE="swap"
/dev/sde1: UUID="daWrCf-6JbQ-VFH8-LICd-tRFY-EKGu-yEJO6W" TYPE="LVM2_member"
/dev/sdd1: UUID="esbzf4-l1Ke-lhoU-neDz-U3d1-7sFS-W0xlx7" TYPE="LVM2_member"
/dev/mapper/vg--opt-lv--opt: UUID="009bc5ac-a784-4725-bd2e-3fcb072c021f" TYPE="ext4"
[root@i-vfyxrtmx ~]# cat /etc/fstab
...
UUID=009bc5ac-a784-4725-bd2e-3fcb072c021f /opt ext4 defaults 1 1
# 查看pv
[root@i-vfyxrtmx network-scripts]# pvdisplay /dev/sdc1
--- Physical volume ---
PV Name /dev/sdc1
VG Name vg-opt
PV Size <1000.00 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 255999
Free PE 0 # pv已经没有空闲的pe了
Allocated PE 255999
PV UUID daWrCf-6JbQ-VFH8-LICd-tRFY-EKGu-yEJO6W
# 查看vg
[root@i-vfyxrtmx network-scripts]# vgdisplay vg-opt
--- Volume group ---
VG Name vg-opt
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size <1000.00 GiB
PE Size 4.00 MiB
Total PE 255999
Alloc PE / Size 255999 / <1000.00 GiB
Free PE / Size 0 / 0 # vg已经没有空闲的pe了
VG UUID A2DX7U-IgRI-CExd-pEMx-Pyg6-97Hv-siGZoa
# vg扩容
# 重要!!!!把pv直接对vg进行了扩容,没有进行格式化成ext4, 是否会有错误?
# 有的文章说创建分区,然后格式化ext4,再
[root@i-vfyxrtmx network-scripts]# lvextend -l +100G /dev/vg-opt/lv-opt
Invalid argument for --extents: +100G
Error during parsing of command line.
[root@i-vfyxrtmx network-scripts]#
[root@i-vfyxrtmx network-scripts]# lvresize -L +100G /dev/vg-opt/lv-opt
Size of logical volume vg-opt/lv-opt changed from <1000.00 GiB (255999 extents) to 1.07 TiB (281599 extents).
Logical volume vg-opt/lv-opt successfully resized.
[root@i-vfyxrtmx network-scripts]#
[root@i-vfyxrtmx network-scripts]# df -lh
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 99G 1.3G 93G 2% /
devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 7.8G 17M 7.8G 1% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
tmpfs 1.6G 0 1.6G 0% /run/user/0
/dev/mapper/vg--opt-lv--opt 985G 77M 935G 1% /opt
[root@i-vfyxrtmx network-scripts]#
[root@i-vfyxrtmx network-scripts]#
[root@i-vfyxrtmx network-scripts]# resize2fs /dev/vg-opt/lv-opt
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg-opt/lv-opt is mounted on /opt; on-line resizing required
old_desc_blocks = 125, new_desc_blocks = 138
The filesystem on /dev/vg-opt/lv-opt is now 288357376 blocks long.
[root@i-vfyxrtmx network-scripts]#
[root@i-vfyxrtmx network-scripts]# df -lh
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 99G 1.3G 93G 2% /
devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 7.8G 17M 7.8G 1% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
tmpfs 1.6G 0 1.6G 0% /run/user/0
/dev/mapper/vg--opt-lv--opt 1.1T 76M 1.1T 1% /opt
[root@i-vfyxrtmx network-scripts]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
└─sda1 8:1 0 100G 0 part /
sdb 8:16 0 16G 0 disk [SWAP]
sdc 8:32 0 1000G 0 disk
└─sdc1 8:33 0 1000G 0 part
└─vg--opt-lv--opt 253:0 0 1.1T 0 lvm /opt
sdd 8:48 0 1000G 0 disk
└─sdd1 8:49 0 1000G 0 part
└─vg--opt-lv--opt 253:0 0 1.1T 0 lvm /opt
sde 8:64 0 1000G 0 disk
sdf 8:80 0 1000G 0 disk
[root@i-vfyxrtmx network-scripts]# pvdisplay /dev/sdd1
--- Physical volume ---
PV Name /dev/sdd1
VG Name vg-opt
PV Size <1000.00 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 255999
Free PE 230399
Allocated PE 25600
PV UUID esbzf4-l1Ke-lhoU-neDz-U3d1-7sFS-W0xlx7
[root@i-vfyxrtmx network-scripts]# lvdisplay /dev/vg-opt/lv-opt
--- Logical volume ---
LV Path /dev/vg-opt/lv-opt
LV Name lv-opt
VG Name vg-opt
LV UUID PfY1TJ-YXP6-nuS5-pPs0-74si-lLgU-SwcDbS
LV Write Access read/write
LV Creation host, time i-vfyxrtmx, 2022-03-07 12:36:53 +0800
LV Status available
# open 1
LV Size 1.07 TiB
Current LE 281599
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
# 扩展400G之后
[root@i-vfyxrtmx opt]# df -lh
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 99G 1.3G 93G 2% /
devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 7.8G 17M 7.8G 1% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
tmpfs 1.6G 0 1.6G 0% /run/user/0
/dev/mapper/vg--opt-lv--opt 1.4T 73M 1.3T 1% /opt
[root@i-vfyxrtmx opt]# lvdisplay /dev/vg-opt/lv-opt
--- Logical volume ---
LV Path /dev/vg-opt/lv-opt
LV Name lv-opt
VG Name vg-opt
LV UUID PfY1TJ-YXP6-nuS5-pPs0-74si-lLgU-SwcDbS
LV Write Access read/write
LV Creation host, time i-vfyxrtmx, 2022-03-07 12:36:53 +0800
LV Status available
# open 1
LV Size <1.37 TiB
Current LE 358399
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
# 扩展逻辑卷和扩展文件系统
[root@i-vfyxrtmx opt]# lvextend -L +300M /dev/vg-opt/lv-opt
Size of logical volume vg-opt/lv-opt changed from <1.37 TiB (358399 extents) to <1.37 TiB (358474 extents).
Logical volume vg-opt/lv-opt successfully resized.
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 99G 1.3G 93G 2% /
devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 7.8G 17M 7.8G 1% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
tmpfs 1.6G 0 1.6G 0% /run/user/0
/dev/mapper/vg--opt-lv--opt 1.4T 73M 1.3T 1% /opt
[root@i-vfyxrtmx opt]#
[root@i-vfyxrtmx opt]# resize2fs /dev/vg-opt/lv-opt
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg-opt/lv-opt is mounted on /opt; on-line resizing required
old_desc_blocks = 175, new_desc_blocks = 176
The filesystem on /dev/vg-opt/lv-opt is now 367256576 blocks long.
[root@i-vfyxrtmx opt]#
[root@i-vfyxrtmx opt]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 99G 1.3G 93G 2% /
devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 7.8G 17M 7.8G 1% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
tmpfs 1.6G 0 1.6G 0% /run/user/0
/dev/mapper/vg--opt-lv--opt 1.4T 73M 1.3T 1% /opt
[root@i-vfyxrtmx opt]#
[root@i-vfyxrtmx opt]#
[root@i-vfyxrtmx opt]# resize2fs /dev/vg-opt/lv-opt
resize2fs 1.42.9 (28-Dec-2013)
The filesystem is already 367256576 blocks long. Nothing to do!
[root@i-vfyxrtmx opt]# lvextend -L +300G /dev/vg-opt/lv-opt
Size of logical volume vg-opt/lv-opt changed from <1.37 TiB (359074 extents) to 1.66 TiB (435874 extents).
Logical volume vg-opt/lv-opt successfully resized.
[root@i-vfyxrtmx opt]#
[root@i-vfyxrtmx opt]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 99G 1.3G 93G 2% /
devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 7.8G 17M 7.8G 1% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
tmpfs 1.6G 0 1.6G 0% /run/user/0
/dev/mapper/vg--opt-lv--opt 1.4T 73M 1.3T 1% /opt
[root@i-vfyxrtmx opt]# resize2fs /dev/vg-opt/lv-opt
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg-opt/lv-opt is mounted on /opt; on-line resizing required
old_desc_blocks = 176, new_desc_blocks = 213
The filesystem on /dev/vg-opt/lv-opt is now 446334976 blocks long.
[root@i-vfyxrtmx opt]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 99G 1.3G 93G 2% /
devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 7.8G 17M 7.8G 1% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
tmpfs 1.6G 0 1.6G 0% /run/user/0
/dev/mapper/vg--opt-lv--opt 1.7T 70M 1.6T 1% /opt
# 6. 添加分区到卷组
# 7. 创建/opt,并挂载卷组
# 8. 编辑/etc/fstab文件
# 9. 重启检查分区/opt是否正常
其他参考
4. 大于2T的磁盘
linux大于2T磁盘分区方法(parted分区方法说明)
参考:
添加磁盘大于2T的方法:必须使用GPT分区
parted命令详解
帮助选项
-h, --help 显示此求助信息
-l, --list 列出所有设别的分区信息
-i, --interactive 在必要时,提示用户
-s, --script 从不提示用户
-v, --version 显示版本
# 1. 查看磁盘
[root@i-tsqjarpa ~]# fdisk -l
Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x00009f6f
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 209713247 104855600 83 Linux
Disk /dev/sdb: 17.2 GB, 17179869184 bytes, 33554432 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 /dev/sdc: 2147.5 GB, 2147483648000 bytes, 4194304000 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
# 2. 选择分区硬盘
[root@i-tsqjarpa ~]# parted /dev/sdc
GNU Parted 3.1
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print list
Model: QEMU QEMU HARDDISK (scsi)
Disk /dev/sdc: 2147GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
Model: QEMU QEMU HARDDISK (scsi)
Disk /dev/sda: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 107GB 107GB primary ext4 boot
Model: QEMU QEMU HARDDISK (scsi)
Disk /dev/sdb: 17.2GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags:
Number Start End Size File system Flags
1 0.00B 17.2GB 17.2GB linux-swap(v1)
# 3. 创建分区
[root@i-ownquxt7 ~]# parted /dev/sdc
GNU Parted 3.1
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel # 输入这个创建分区信息
New disk label type? gpt # 默认为msdos形式的分区,分区大于2TB的磁盘要使用gpt方式的分区表,所以输入gpt后回车
(parted) mkpart
# 4. 按百分比方式进行创建
(parted)
(parted) mkpart #分区命令
Partition name? []? dp1 #分区名称(自定义)
File system type? [ext2]? ext4 #指定类型(ext3、ext4、xfs)
Start? 0%
End? 50%
(parted) mkpart
Partition name? []? dp2
File system type? [ext2]? ext4
Start? 50%
End? 100%
(parted) print # 也可以只输入一个 p
Model: QEMU QEMU HARDDISK (scsi)
Disk /dev/sdc: 2147GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 1074GB 1074GB dp1
2 1074GB 2147GB 1074GB dp2
(parted) quit
Information: You may need to update /etc/fstab.
# 错误或警告1: 以下按照大小创建时出现警告
(parted) mkpart
Partition name? []? dp1
File system type? [ext2]? ext4
Start? 0
End? 1000GB
Warning: The resulting partition is not properly aligned for best performance.
# 警告,解决方法参见:
Ignore/Cancel? C # 取消后,按照参见的推荐的进行创建,即百分比方式
# 删除分区示例
# 如果分区错了,可以使用rm命令删除分区,比如我们要删除上面的分区,然后打印删除后的结果
(parted) rm 1 #rm后面使用分区的号码,就是用print打印出来的Number
(parted) print #再次查看确认已删除
Model: VBOX HARDDISK (ide)
Disk /dev/sdb: 2147GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name Flags
# 1. gpt创建分区后
[root@i-tsqjarpa ~]# fdisk -l
Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x00009f6f
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 209713247 104855600 83 Linux
Disk /dev/sdb: 17.2 GB, 17179869184 bytes, 33554432 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
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Disk /dev/sdc: 2147.5 GB, 2147483648000 bytes, 4194304000 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: gpt
Disk identifier: CA32C7B4-5C04-4B8B-AE86-31EF24B66577
# Start End Size Type Name
1 2048 2097151999 1000G Microsoft basic dp1
2 2097152000 4194301951 1000G Microsoft basic dp2
# 2. 查询分区
[root@i-tsqjarpa ~]# ls /dev/sdc* -lh
brw-rw---- 1 root disk 8, 32 Mar 31 16:00 /dev/sdc
brw-rw---- 1 root disk 8, 33 Mar 31 16:00 /dev/sdc1
brw-rw---- 1 root disk 8, 34 Mar 31 16:00 /dev/sdc2
# 3. 创建pv
[root@i-tsqjarpa ~]# pvcreate /dev/sdc1 /dev/sdc2
Physical volume "/dev/sdc1" successfully created.
Physical volume "/dev/sdc2" successfully created.
# 4. 创建vg
[root@i-tsqjarpa ~]# vgcreate vg-opt /dev/sdc1 /dev/sdc2
Volume group "vg-opt" successfully created
# 5. 显示vg
[root@i-tsqjarpa ~]# vgdisplay
--- Volume group ---
VG Name vg-opt
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.95 TiB
PE Size 4.00 MiB
Total PE 511998
Alloc PE / Size 0 / 0
Free PE / Size 511998 / 1.95 TiB
VG UUID heLoZn-VeiR-YHN7-2nI8-dWCV-dB9D-iiMA8V
# 创建1T的lv
[root@i-tsqjarpa ~]# lvcreate -L 1000G -n lv-opt vg-opt
Logical volume "lv-opt" created.
[root@i-tsqjarpa ~]# vgdisplay
--- Volume group ---
VG Name vg-opt
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 1.95 TiB
PE Size 4.00 MiB
Total PE 511998
Alloc PE / Size 256000 / 1000.00 GiB
Free PE / Size 255998 / 999.99 GiB # 剩余
VG UUID heLoZn-VeiR-YHN7-2nI8-dWCV-dB9D-iiMA8V
# 查看已创建的 lv-opt
[root@i-vfyxrtmx network-scripts]# ls -lh /dev/vg-opt/lv-opt
lrwxrwxrwx 1 root root 7 Mar 7 12:36 /dev/vg-opt/lv-opt -> ../dm-0
# 扩容lv
# 先扩100G
[root@i-tsqjarpa ~]# lvresize -L +100G /dev/vg-opt/lv-opt
Size of logical volume vg-opt/lv-opt changed from 1000.00 GiB (256000 extents) to 1.07 TiB (281600 extents).
Logical volume vg-opt/lv-opt successfully resized.
[root@i-tsqjarpa ~]#
[root@i-tsqjarpa ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vg-opt/lv-opt
LV Name lv-opt
VG Name vg-opt
LV UUID BQU3TE-1gIt-sqPZ-uAmx-034A-ZyWW-FeT30T
LV Write Access read/write
LV Creation host, time i-tsqjarpa, 2022-03-31 16:09:09 +0800
LV Status available
# open 0
LV Size 1.07 TiB
Current LE 281600
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
[root@i-tsqjarpa ~]# vgdisplay
--- Volume group ---
... # vg 还剩899G
Free PE / Size 230398 / 899.99 GiB
VG UUID heLoZn-VeiR-YHN7-2nI8-dWCV-dB9D-iiMA8V
[root@i-tsqjarpa ~]# lvresize -L +921592M /dev/vg-opt/lv-opt
Size of logical volume vg-opt/lv-opt changed from 1.07 TiB (281600 extents) to 1.95 TiB (511998 extents).
Logical volume vg-opt/lv-opt successfully resized.
[root@i-tsqjarpa ~]#
[root@i-tsqjarpa ~]# vgdisplay
--- Volume group ---
...
Alloc PE / Size 511998 / 1.95 TiB
Free PE / Size 0 / 0 # 已经全部扩完
VG UUID heLoZn-VeiR-YHN7-2nI8-dWCV-dB9D-iiMA8V
# 查看扩容后的lv
[root@i-tsqjarpa ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vg-opt/lv-opt
LV Name lv-opt
VG Name vg-opt
LV UUID BQU3TE-1gIt-sqPZ-uAmx-034A-ZyWW-FeT30T
LV Write Access read/write
LV Creation host, time i-tsqjarpa, 2022-03-31 16:09:09 +0800
LV Status available
# open 0
LV Size 1.95 TiB # 已经扩容
Current LE 511998
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
# 还没有挂载
[root@i-tsqjarpa ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 99G 1.9G 92G 2% /
devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 7.8G 25M 7.8G 1% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
tmpfs 1.6G 0 1.6G 0% /run/user/0
# 格式化lv-opt为ext4格式
[root@i-vfyxrtmx network-scripts]# mkfs -t ext4 /dev/vg-opt/lv-opt
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
65536000 inodes, 262142976 blocks
13107148 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2409627648
8000 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, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000, 214990848
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information:
done
# 创建挂载目录,并挂载
[root@i-vfyxrtmx network-scripts]# mkdir -p /opt
[root@i-vfyxrtmx network-scripts]# mount /dev/vg-opt/lv-opt /opt/
# 查看挂载后的文件系统
[root@i-vfyxrtmx network-scripts]# df -h
Filesystem Size Used Avail Use% Mounted on
...
/dev/mapper/vg--opt-lv--opt 2.0T 81M 1.9T 1% /opt # 已挂载的 文件系统
#7. 向/etc/fstab文件添加条目, 增加如下行
[root@i-vfyxrtmx network-scripts]# vi /etc/fstab
/dev/vg-opt/lv-opt /opt ext4 defaults 1 1
# 或者 用磁盘的uuid进行挂载
[root@i-vfyxrtmx ~]# blkid
/dev/sda1: UUID="021361f1-9d1c-41c1-97ed-ca04a4252efc" TYPE="ext4"
/dev/sdf: LABEL="YUNIFYSWAP" UUID="48eb1df6-1663-4a52-ab30-040d552c2d76" TYPE="swap"
/dev/sde1: UUID="daWrCf-6JbQ-VFH8-LICd-tRFY-EKGu-yEJO6W" TYPE="LVM2_member"
/dev/sdd1: UUID="esbzf4-l1Ke-lhoU-neDz-U3d1-7sFS-W0xlx7" TYPE="LVM2_member"
/dev/mapper/vg--opt-lv--opt: UUID="009bc5ac-a784-4725-bd2e-3fcb072c021f" TYPE="ext4"
[root@i-vfyxrtmx ~]# cat /etc/fstab
...
UUID=009bc5ac-a784-4725-bd2e-3fcb072c021f /opt ext4 defaults 1 1
# 重启测试自启动挂载点
[root@i-tsqjarpa ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 99G 1.9G 92G 2% /
devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 7.8G 17M 7.8G 1% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
/dev/mapper/vg--opt-lv--opt 2.0T 81M 1.9T 1% /opt
gtp+vg 创建完毕
参考&附录
1. 逻辑卷命令总结
LVM的整体思路是:
创建卷管理:首先创建PV–>然后创建VG并将多个PV加到VG里–>然后创建LV–>格式化分区–>mount分区
扩容思路: 首先创建PV–>加入已创建的VG–>然后扩容LV–> 刷新文件系统
1.创建PV
pvcreate /dev/sdb pvcreate /dev/sdc pvcreate /dev/sde
2.创建VG并将多个PV加到VG
vgcreate vg_test /dev/sdb /dev/sdc
3.创建LV
lvcreate -L 2000G -n lv_test vg_test
4.创建文件系统
mkfs.ext4 /dev/vg_test/lv_test
5.mount分区
mkdir /data
echo "/dev/vg_test/lv_test /data ext4 defaults 0 0" >> /etc/fstab
6.将新的物理卷添加到现有卷组:
# vgextend vg_test /dev/sdc
/dev/cdrom: open failed: 只读文件系统
Attempt to close device '/dev/cdrom' which isnot open.
Volume group "vg_test" successfullyextended
7.从现有的卷组中删除一个物理卷:
从一个卷组中删除一个物理卷,首先要确认要删除的物理卷没有被任何逻辑卷正在使用,就要使用pvdisplay命令察看一个该物理卷信息。
如果某个物理卷正在被逻辑卷所使用,就需要将该物理卷的数据备份到其他地方,然后再删除。
删除物理卷的命令为vgreduce:
# vgreduce vg_test /dev/sdc
Removed "/dev/sdc" from volume group "vg_test" -----已经删除成功
2. lvextend lvreduce lvresize区别
lvresize -size +40G /dev/vg0/foo
要么:
lvresize -size 120G /dev/vg0/foo
区别在于lvextend只能增加一个体积的大小,而lvresize可以增加或减小它的大小。
这使lvresize功能更强大但更危险。
如果不经意地减小卷的大小而没有先减小其中包含的文件系统的大小,则该文件系统很可能会受到不可挽回的损害。
对于与此处描述的情况类似的情况,lvextend建议使用此方法,因为这样就不可能出现这种类型的错误。
- **lvresize可以用于收缩和/或扩展,而lvextend只能用于扩展。**如果不经意地减小卷的大小而没有先减小其中包含的文件系统的大小,则该文件系统很可能会受到不可挽回的损害。
- 卷组的物理扩展大小(PE)设置为32M,这就是lveextend将它从1到32M舍入的原因
在考虑调整PE值的大小之前,我担心在这种情况下不可能,如果必须更改PE,则可能需要再次创建VG.
关于那个,你可以在这里看到[How to change Volumegroup PE size](javascript:void())