CentOS7添加新硬盘(LVM格式)

  • 添加硬盘驱动器

[root@linuxprobe ~]# fdisk /dev/sdb

# enter operation mode for partitions

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.

Command (m for help): p
# show partition table

Disk /dev/vdb: 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: 0xd97d5b18

Device Boot Start End Blocks Id System

# none

Command (m for help): n
# create a partition

Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended

Select (default p): p
# primary

Partition number (1-4, default 1): 1
# specify partition number

First sector (2048-41943039, default 2048): # starting cylinder

Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): # end cylinder

Using default value 41943039
Partition 1 of type Linux and of size 20 GiB is set
Command (m for help): p
# show partition table

Disk /dev/vdb: 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: 0xd97d5b18

Device Boot Start End Blocks Id System
/dev/vdb1 2048 41943039 20970496 83 Linux

# just created

Command (m for help): 5
# change type
Selected partition 1
Hex code (type L to list all codes): L # 查看全部格式
# show list

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
Command (m for help): t # 改变硬盘格式
Selected partition 1
Hex code (type L to list all codes): 8e #为LVM标识
# specify Linux LVM

Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
# show partition table

Disk /dev/vdb: 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: 0xd97d5b18

Device Boot Start End Blocks Id System
/dev/vdb1 2048 41943039 20970496 8e Linux LVM

# changed

Command (m for help): w #保存更改
# save and quit

The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

[root@linuxprobe ~]# sfdisk -l /dev/sdb
# show status


Disk /dev/vdb: 41610 cylinders, 16 heads, 63 sectors/track
sfdisk: Warning: The partition table looks like it was made
for C/H/S=*/3/34 (instead of 41610/16/63).
For this listing I'll assume that geometry.

Units: cylinders of 52224 bytes, blocks of 1024 bytes, counting from 0

Device Boot Start End #cyls #blocks Id System
/dev/vdb1 20+ 411206- 411187- 20970496 8e Linux LVM
sfdisk: start: (c,h,s) expected (20,0,9) found (2,0,33)

sfdisk: end: (c,h,s) expected (1023,2,34) found (650,2,34)

/dev/vdb2 0 - 0 0 0 Empty
/dev/vdb3 0 - 0 0 0 Empty
/dev/vdb4 0 - 0 0 0 Empty

  • 管理Phisical卷

  • 创建物理卷

[root@linuxprobe ~]# pvcreate /dev/sdb1

Physical volume "/dev/sdb1" successfully created

# 制定创建的PV大小
[root@linuxprobe ~]# pvcreate --setphysicalvolumesize 50G /dev/sdb1
Physical volume "/dev/sdb1" successfully created

  • 显示物理卷

[root@linuxprobe ~]# pvdisplay /dev/sdb1

"/dev/sdb1" is a new physical volume of "20.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 20.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID o0nkv0-1XbS-O5Sa-OjKK-kEaL-IVd7-UrSrCP

  • 改变物理卷大小

[root@linuxprobe ~]# pvresize --setphysicalvolumesize 10G /dev/sdb1

Physical volume "/dev/sdb1" changed
1 physical volume(s) resized / 0 physical volume(s) not resized
[root@linuxprobe ~]# pvdisplay /dev/sdb1
"/dev/sdb1" is a new physical volume of "10.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 10.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID o0nkv0-1XbS-O5Sa-OjKK-kEaL-IVd7-UrSrCP

  • 扫描pv的信息状态

[root@linuxprobe ~]# pvs /dev/sdb1
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 --- 20.00g 20.00g
[root@linuxprobe ~]# pvscan
PV /dev/sda2 VG centos lvm2 [19.51 GiB / 40.00 MiB free]
PV /dev/sdb1 lvm2 [20.00 GiB]
Total: 2 [39.51 GiB] / in use: 1 [19.51 GiB] / in no VG: 1 [20.00 GiB]

  • 删除pv

[root@linuxprobe ~]# pvremove /dev/sdb1
Labels on physical volume "/dev/sdb1" successfully wiped
[root@linuxprobe ~]# pvdisplay /dev/sdb1
Failed to find physical volume "/dev/sdb1".

  • 创建卷组(已创建PV)

[root@linuxprobe ~]# vgcreate vg_linux/dev/sdb1
Volume group "vg_linux" successfully created

# 制定多个磁盘设备如下:
[root@linuxprobe ~]# vgcreate vg_linux /dev/sdb1 /dev/sdd1
Volume group "vg_linux" successfully created

  • 显示卷组信息

[root@linuxprobe ~]# vgcreate vg_linux /dev/sdb1
Volume group "vg_linux" successfully created
[root@linuxprobe ~]# vgdisplay vg_linux
--- Volume group ---
VG Name vg_linux
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 20.00 GiB
PE Size 4.00 MiB
Total PE 5119
Alloc PE / Size 0 / 0
Free PE / Size 5119 / 20.00 GiB
VG UUID RnRfbj-DUC4-BdUB-kmov-L6Lh-uiCP-SVDsKq

  • 改变卷组名称

[root@linuxprobe ~]# vgrename vg_linux vg_probe
Volume group "vg_linux" successfully renamed to "vg_probe"
[root@linuxprobe ~]# vgdisplay vg_probe
--- Volume group ---
VG Name vg_probe
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
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 20.00 GiB
PE Size 4.00 MiB
Total PE 5119
Alloc PE / Size 0 / 0
Free PE / Size 5119 / 20.00 GiB
VG UUID RnRfbj-DUC4-BdUB-kmov-L6Lh-uiCP-SVDsKq

  • 扩展卷组

# add sdc1 to vg_probe

[root@linuxprobe ~]# vgextend vg_probe /dev/sdc1

Volume group "vg_probe" successfully extended
[root@linuxprobe ~]# vgdisplay vg_probe

--- Volume group ---
VG Name vg_probe
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 4
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 40.00 GiB
PE Size 4.00 MiB
Total PE 20479
Alloc PE / Size 0 / 0
Free PE / Size 20479 / 160.00 GiB
VG UUID RnRfbj-DUC4-BdUB-kmov-L6Lh-uiCP-SVDsKq

  • 卷组缩减

# remove sdc1 from vg_probe

[root@linuxprobe ~]# vgreduce vg_probe /dev/sdc1

Removed "/dev/sdc1" from volume group "vg_probe "
[root@linuxprobe ~]# vgdisplay vg_probe

--- Volume group ---
VG Name vg_probe
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
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 20.00 GiB
PE Size 4.00 MiB
Total PE 20479
Alloc PE / Size 0 / 0
Free PE / Size 20479 / 80.00 GiB
VG UUID RnRfbj-DUC4-BdUB-kmov-L6Lh-uiCP-SVDsKq

  • 删除卷组

# disable target volume group first and delete it
[root@linuxprobe ~]# vgchange -a n vg_probe

0 logical volume(s) in volume group "vg_probe" now active
[root@linuxprobe ~]# vgremove vg_probe
Volume group "vg_data" successfully removed

  • 创建LV

[root@linuxprobe ~]# lvcreate -L 5G -n lv_data vg_probe #
Logical volume "lv_data" created.
[root@linuxprobe ~]# lvcreate -l 100%FREE -n lv_data vg_probe
Logical volume "lv_data" already exists in volume group "vg_probe" # lv已存在
[root@linuxprobe ~]# lvcreate -l 100%FREE -n lv_probe vg_probe # 新建LV使用全部的vg
Logical volume "lv_probe" created.
[root@linuxprobe ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- 19.51g 40.00m
vg_probe 1 2 0 wz--n- 20.00g 0

  • lv显示

[root@linuxprobe ~]# lvdisplay /dev/vg_probe/lv_probe
--- Logical volume ---
LV Path /dev/vg_probe/lv_probe
LV Name lv_probe
VG Name vg_probe
LV UUID s11m21-qn5J-2gLx-IrFm-GZ1B-ZEZx-teHFiw
LV Write Access read/write
LV Creation host, time linuxprobe.org, 2017-01-03 19:23:29 +0800
LV Status available
# open 0
LV Size 15.00 GiB
Current LE 3839
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:3

  • 改变LV名称

[root@linuxprobe ~]# lvrename vg_probe lv_probe lv_linux
Renamed "lv_probe" to "lv_linux" in volume group "vg_probe"
[root@linuxprobe ~]# lvdisplay /dev/vg_probe/lv_linux
--- Logical volume ---
LV Path /dev/vg_probe/lv_linux
LV Name lv_linux
VG Name vg_probe
LV UUID s11m21-qn5J-2gLx-IrFm-GZ1B-ZEZx-teHFiw
LV Write Access read/write
LV Creation host, time linuxprobe.org, 2017-01-03 19:23:29 +0800
LV Status available
# open 0
LV Size 15.00 GiB
Current LE 3839
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:3

[root@linuxprobe ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- 17.47g
swap centos -wi-ao---- 2.00g
lv_data vg_probe -wi-a----- 5.00g
lv_linux vg_probe -wi-a----- 15.00g

  • 拍摄逻辑卷快照

[root@linuxprobe ~]# lvcreate -s -L 5G -n snap-lv_linux /dev/vg_probe/lv_linux
Logical volume "snap-lv_linux" created.
[root@linuxprobe ~]# lvdisplay /dev/vg_probe/lv_linux /dev/vg_probe/snap-lv_linux
--- Logical volume ---
LV Path /dev/vg_probe/lv_linux
LV Name lv_linux
VG Name vg_probe
LV UUID s11m21-qn5J-2gLx-IrFm-GZ1B-ZEZx-teHFiw
LV Write Access read/write
LV Creation host, time linuxprobe.org, 2017-01-03 19:23:29 +0800
LV snapshot status source of
snap-lv_linux [active]
LV Status available
# open 0
LV Size 5.00 GiB
Current LE 1280
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:3

--- Logical volume ---
LV Path /dev/vg_probe/snap-lv_linux
LV Name snap-lv_linux
VG Name vg_probe
LV UUID sLLQ2y-IOiv-fgtD-RpPr-y7pw-YWnZ-zqwXhD
LV Write Access read/write
LV Creation host, time linuxprobe.org, 2017-01-03 19:30:49 +0800
LV snapshot status active destination for lv_linux
LV Status available
# open 0
LV Size 5.00 GiB
Current LE 1280
COW-table size 5.00 GiB
COW-table LE 1280
Allocated to snapshot 0.00%
Snapshot chunk size 4.00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:6

  • 扩展逻辑卷

[root@linuxprobe ~]# lvextend -L 6G /dev/vg_probe/lv_data
Size of logical volume vg_probe/lv_data changed from 5.00 GiB (1280 extents) to 6.00 GiB (1536 extents).
Logical volume lv_data successfully resized.
[root@linuxprobe ~]# lvdisplay /dev/vg_probe/lv_data
--- Logical volume ---
LV Path /dev/vg_probe/lv_data
LV Name lv_data
VG Name vg_probe
LV UUID 7MVyp5-xw22-dPnu-vQp8-pa5C-ONvs-LQfS09
LV Write Access read/write
LV Creation host, time linuxprobe.org, 2017-01-03 19:22:52 +0800
LV Status available
# open 0
LV Size 6.00 GiB
Current LE 1536
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2

[root@linuxprobe ~]# xfs_growfs /mnt/
meta-data=/dev/mapper/centos-root isize=256 agcount=4, agsize=1144832 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=4579328, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@linuxprobe ~]# resize
resize2fs resizecons resizepart
[root@linuxprobe ~]# resize2fs /dev/vg_probe/lv_data #重新格式化
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/vg_probe/lv_data
Couldn't find valid filesystem superblock.

  • 减少逻辑卷。

首先卸载目标设备。
[root@linuxprobe ~]# lvreduce -L 5G /dev/vg_probe/lv_data
WARNING: Reducing active logical volume to 5.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_data? [y/n]: y
Size of logical volume vg_probe/lv_data changed from 6.00 GiB (1536 extents) to 5.00 GiB (1280 extents).
Logical volume lv_data successfully resized.
[root@linuxprobe ~]# lvchange -an /dev/vg_probe/lv_data
[root@linuxprobe ~]# lvremove /dev/vg_probe/lv_data
Logical volume "lv_data" successfully removed

常见报错
resize2fs /dev/cl_kvm-centos7/opt
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/cl_kvm-centos7/opt
Couldn't find valid filesystem superblock.

注:由于CentOS7文件系统格式为xfs,因此使用LVM对磁盘进行管理的时候原来的命令有些问题,下面介绍CentOS7下对LVM操作。

# 下载工具包
yum install xfsprogs -y

# 重新格式化挂载分区(类似mkfs.ext4)
mkfs.xfs -f /dev/cl_kvm-centos7/data

# 重新resize分区
xfs_growfs /dev/mapper/cl_kvm--centos7-var

猜你喜欢

转载自www.cnblogs.com/stupidjackey/p/11313558.html