1、添加一塊20G的新硬碟并重新開機Linux,硬碟被識别為sdb。
2、在sdb上建立兩個新分區,大小為10G和5G。
[root@centos6 ~]# fdisk /dev/sdb
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-10443, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-10443, default 10443): +10G
Partition number (1-4): 2
First cylinder (1307-10443, default 1307):
Using default value 1307
Last cylinder, +cylinders or +size{K,M,G} (1307-10443, default 10443): +5G
3、将分區系統ID設定為LVM類型,并儲存。
Command (m for help): t
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)
Changed system type of partition 5 to 8e (Linux LVM)
Command (m for help): w
The partition table has been altered!
4、讓Linux核心重讀分區表。(由于此硬碟為新硬碟,之前并沒有分區,所于此步可以忽略。對于之前有分區的硬碟,添加了分區之後需要這一步操作。)
[root@centos6 ~]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
error adding partition 2
5、建立實體卷PV。
[root@centos6 ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created
6、檢視PV資訊。
[root@centos6 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 ---- 10.00g 10.00g
[root@centos6 ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sdb1
VG Name vg0
PV Size 10.00 GiB / not usable 4.54 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 2560
Free PE 2560
Allocated PE 0
PV UUID 8oFFsN-mdFh-kgkQ-eSmt-IBhS-HJWP-xnnUzJ
7、建立卷組VG。
[root@centos6 ~]# vgcreate vg0 /dev/sdb1
Volume group "vg0" successfully created
8、檢視VG資訊。
[root@centos6 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg0 1 0 0 wz--n- 10.00g 10.00g
[root@centos6 ~]# vgdisplay
--- Volume group ---
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 10.00 GiB
Alloc PE / Size 0 / 0
Free PE / Size 2560 / 10.00 GiB
VG UUID E1lTkT-ttoh-K7rG-edfj-GTNJ-3zhB-5dJSci
9、擴充VG大小。
[root@centos6 ~]# pvcreate /dev/sdb2
Physical volume "/dev/sdb2" successfully created
[root@centos6 ~]# vgextend vg0 /dev/sdb2
Volume group "vg0" successfully extended
vg0 2 0 0 wz--n- 15.01g 15.01g
10、縮減VG大小。
[root@centos6 ~]# pvmove /dev/sdb2
No data to move for vg0
[root@centos6 ~]# vgreduce vg0 /dev/sdb2
Removed "/dev/sdb2" from volume group "vg0"
11、建立邏輯卷LV。
[root@centos6 ~]# lvcreate -L 2G -n lv0 vg0
Logical volume "lv0" created.
12、檢視LV。
[root@centos6 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv0 vg0 -wi-a----- 2.00g
[root@centos6 ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vg0/lv0
LV Name lv0
VG Name vg0
LV UUID wqjJ5n-uXjY-0uj2-dwSE-wA4T-LfuL-3xlFTS
LV Write Access read/write
LV Creation host, time centos6.localdomain, 2019-02-26 23:54:43 +0800
LV Status available
# open 0
LV Size 2.00 GiB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
13、格式化LV為ext4檔案系統并挂載。
[root@centos6 ~]# mke2fs -t ext4 /dev/vg0/lv0
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@centos6 ~]# mkdir /backup
[root@centos6 ~]# mount /dev/vg0/lv0 /backup
[root@centos6 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 17G 3.4G 13G 22% /
tmpfs 490M 0 490M 0% /dev/shm
/dev/sda1 976M 34M 892M 4% /boot
/dev/mapper/vg0-lv0 2.0G 3.0M 1.9G 1% /backup
14、擴充LV大小。
[root@centos6 ~]# lvextend -L 5G /dev/vg0/lv0
Size of logical volume vg0/lv0 changed from 2.00 GiB (512 extents) to 5.00 GiB (1280 extents).
Logical volume lv0 successfully resized.
15、擴充LV中的ext4檔案系統大小。
[root@centos6 ~]# resize2fs /dev/vg0/lv0
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg0/lv0 is mounted on /backup; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg0/lv0 to 1310720 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 1310720 blocks long.
/dev/mapper/vg0-lv0 4.9G 4.0M 4.7G 1% /backup
16、縮減LV中的ext4檔案系統大小。
[root@centos6 ~]# umount /dev/vg0/lv0
[root@centos6 ~]# e2fsck -f /dev/vg0/lv0
e2fsck 1.41.12 (17-May-2010)
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/vg0/lv0: 11/327680 files (0.0% non-contiguous), 37982/1310720 blocks
[root@centos6 ~]# resize2fs /dev/vg0/lv0 3G
Resizing the filesystem on /dev/vg0/lv0 to 786432 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 786432 blocks long.
17、縮減LV大小。
[root@centos6 ~]# lvreduce -L 3G /dev/vg0/lv0
WARNING: Reducing active logical volume to 3.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg0/lv0? [y/n]: y
Size of logical volume vg0/lv0 changed from 5.00 GiB (1280 extents) to 3.00 GiB (768 extents).
/dev/mapper/vg0-lv0 2.9G 3.0M 2.8G 1% /backup
18、建立快照卷。
[root@centos6 ~]# cp /etc/issue /backup
[root@centos6 ~]# cat /backup/issue
CentOS release 6.10 (Final)
Kernel \r on an \m
[root@centos6 ~]# lvcreate -L 3G -p r -s /dev/vg0/lv0 -n lv0-snap
Logical volume "lv0-snap" created.
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv0 vg0 owi-aos--- 3.00g
lv0-snap vg0 sri-a-s--- 3.00g lv0 0.00
19、挂載快照卷,修改源卷檔案後檢視快照卷檔案是否變化。
[root@centos6 ~]# mount /dev/vg0/lv0-snap /mnt
mount: block device /dev/mapper/vg0-lv0--snap is write-protected, mounting read-only
[root@centos6 ~]# echo "New line." >> /backup/issue
New line.
[root@centos6 ~]# cat /mnt/issue
20、解除安裝及删除快照卷和源LV。
[root@centos6 ~]# umount /mnt
[root@centos6 ~]# lvremove /dev/vg0/lv0-snap
Do you really want to remove active logical volume lv0-snap? [y/n]: y
Logical volume "lv0-snap" successfully removed
[root@centos6 ~]# umount /backup
[root@centos6 ~]# lvremove /dev/vg0/lv0
Do you really want to remove active logical volume lv0? [y/n]: y
Logical volume "lv0" successfully removed
21、删除VG。
[root@centos6 ~]# vgremove vg0
Volume group "vg0" successfully removed
22、删除PV。
[root@centos6 ~]# pvremove /dev/sdb{1,2}