Linux-LVM的管理及磁盤配額

LVM是 Logical Volume Manager(邏輯卷管理)的簡寫,它是Linux環境下對磁盤分區進行管理的一種機制,用戶可以在無需停機的情況下可以方便地調整各個分區大小。

LVM名詞:

物理卷(Physical Volume,PV):包含LVM信息的分區、磁盤

物理卷組(Volume Group,VG):多個物理卷組成,可以在VG上創建一個或多個LV

邏輯卷(Logical Volume,LV):創建在VG上的分區,可以在LV上創建文件系統

物理塊(Physical Extent,PE):物理卷的基本單元,PV=PE*數量

創建LVM:

查看命令:
pvs|pvdisplay    ##查看物理卷
vgs|vgdisplay    ##查看物理卷組
lvs|lvdisplay    ##查看邏輯卷

##測試前我們可以使用watch命令監控測試的變化
[root@server ~]# watch -n 1 'pvs;vgs;lvs;df -h /mnt'

##測試用的虛擬機準備了三塊磁盤vdb、vdc、vdd,並且都創建了LVM分區
不會創建的可以參考https://blog.csdn.net/xin1889/article/details/80171516 
[root@server ~]# parted
GNU Parted 3.1
Using /dev/vda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print list                                                       
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  10.7GB  10.7GB  primary  xfs          boot


Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  10.7GB  10.7GB  primary               lvm


Model: Virtio Block Device (virtblk)
Disk /dev/vdc: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  10.7GB  10.7GB  primary               lvm


Model: Virtio Block Device (virtblk)
Disk /dev/vdd: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  10.7GB  10.7GB  primary               lvm

創建LVM
創建命令:
pvcreate    ##將物理分區製作成物理卷(pv)
vgcreate    ##用製作好的物理卷製作一個物理卷組(vg)
lvcreate    ##在物理卷組(vg)中創建lv設備,-L 指定大小,-n 指定設備名稱
[root@server ~]# pvcreate /dev/vdb1            ##將分區/dev/vdb1創建成pv
  Physical volume "/dev/vdb1" successfully created
[root@server ~]# vgcreate vg0 /dev/vdb1 /      ##將pv製作成一個vg
  Volume group "vg0" successfully created
[root@server ~]# lvcreate -L 8G -n lv0 vg0     ##在vg上創建一個lv
  Logical volume "lv0" created

[root@server ~]# mkfs.xfs /dev/vg0/lv0         ##格式化
meta-data=/dev/vg0/lv0           isize=256    agcount=4, agsize=524288 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=2097152, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@server ~]# mount /dev/vg0/lv0 /mnt/      ##掛載

####監控內容####
Every 1.0s: pvs;vgs;lvs;df -h /mnt                      Wed May  9 09:35:31 2018

  PV         VG   Fmt  Attr PSize  PFree
  /dev/vdb1  vg0  lvm2 a--  10.00g 2.00g
  VG   #PV #LV #SN Attr   VSize  VFree
  vg0    1   1   0 wz--n- 10.00g 2.00g
  LV   VG   Attr       LSize Pool Origin Data%  Move Log Cpy%Sync Convert
  lv0  vg0  -wi-ao---- 8.00g
Filesystem              Size  Used Avail Use% Mounted on
/dev/mapper/vg0-lv0  8.0G   33M  8.0G   1% /mnt

擴充LVM

[root@server ~]# pvcreate /dev/vdc1                ##將/dev/vdc1製作成物理卷
  Physical volume "/dev/vdd1" successfully created
[root@server ~]# vgextend vg0 /dev/vdc1           ##將/dev/vdc1添加至vg0
  Volume group "vg0" successfully extended
[root@server ~]# lvextend -L 12G /dev/vg0/lv0     ##將lv0擴大至12G
  Extending logical volume lv0 to 12.00 GiB
  Logical volume lv0 successfully resized
[root@server ~]# xfs_growfs /dev/vg0/lv0          ##將文件系統擴大至lv0大小
meta-data=/dev/mapper/vg0-lv0    isize=256    agcount=4, agsize=524288 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=2097152, 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
data blocks changed from 2097152 to 3145728

####監控內容####
Every 1.0s: pvs;vgs;lvs;df -h /mnt                      Wed May  9 09:42:31 2018

  PV         VG   Fmt  Attr PSize  PFree
  /dev/vdb1  vg0  lvm2 a--  10.00g    0
  /dev/vdc1  vg0  lvm2 a--  10.00g 7.99g
  VG   #PV #LV #SN Attr   VSize  VFree
  vg0    2   1   0 wz--n- 19.99g 7.99g
  LV   VG   Attr       LSize  Pool Origin Data%  Move Log Cpy%Sync Convert
  lv0  vg0  -wi-ao---- 12.00g
Filesystem              Size  Used Avail Use% Mounted on
/dev/mapper/vg0-lv0   12G   33M   12G   1% /mnt

####xfs文件系統只能擴大,不能縮小,如果日後需要縮減,需要使用ext文件格式####
[root@server ~]# umount /dev/vg0/lv0            ##解除掛載
[root@server ~]# mkfs.ext4 /dev/vg0/lv0         ##格式化成ext4格式
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
786432 inodes, 3145728 blocks
157286 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2151677952
96 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
[root@server ~]# mount /dev/vg0/lv0 /mnt/        ##重新掛載
[root@server ~]# lvextend -L 15G /dev/vg0/lv0    ##擴大lv0到15G大小
  Extending logical volume lv0 to 15.00 GiB
  Logical volume lv0 successfully resized

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done 

[root@server ~]# resize2fs /dev/vg0/lv0         ##擴展ext4文件系統到lv大小
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg0/lv0 is mounted on /mnt; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 2
The filesystem on /dev/vg0/lv0 is now 3932160 blocks long.

####監控內容####
Every 1.0s: pvs;vgs;lvs;df -h /mnt                      Wed May  9 09:53:31 2018

  PV         VG   Fmt  Attr PSize  PFree
  /dev/vdb1  vg0  lvm2 a--  10.00g    0
  /dev/vdc1  vg0  lvm2 a--  10.00g 4.99g
  VG   #PV #LV #SN Attr   VSize  VFree
  vg0    2   1   0 wz--n- 19.99g 4.99g
  LV   VG   Attr       LSize  Pool Origin Data%  Move Log Cpy%Sync Convert
  lv0  vg0  -wi-ao---- 15.00g
Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/vg0-lv0   15G   41M   14G   1% /mnt

LVM縮減

[root@server ~]# umount /dev/vg0/lv0                ##解除掛載
[root@server ~]# e2fsck -f /dev/vg0/lv0             ##掃描文件系統中已經存在的數據
e2fsck 1.42.9 (28-Dec-2013)
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/983040 files (0.0% non-contiguous), 104724/3932160 blocks
[root@server ~]# resize2fs /dev/vg0/lv0 8G          ##縮減文件系統大小至8G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg0/lv0 to 2097152 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 2097152 blocks long.
[root@server ~]# mount /dev/vg0/lv0 /mnt/           ##重新掛載
[root@server ~]# lvreduce -L 8G /dev/vg0/lv0        ##縮減lv0大小至8G
  WARNING: Reducing active logical volume to 8.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv0? [y/n]: y
  Reducing logical volume lv0 to 8.00 GiB
  Logical volume lv0 successfully resized

####監控內容####
Every 1.0s: pvs;vgs;lvs;df -h /mnt                      Wed May  9 10:11:15 2018

  PV         VG   Fmt  Attr PSize  PFree
  /dev/vdb1  vg0  lvm2 a--  10.00g  2.00g
  /dev/vdc1  vg0  lvm2 a--  10.00g 10.00g
  VG   #PV #LV #SN Attr   VSize  VFree
  vg0    2   1   0 wz--n- 19.99g 11.99g
  LV   VG   Attr       LSize Pool Origin Data%  Move Log Cpy%Sync Convert
  lv0  vg0  -wi-ao---- 8.00g
Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/vg0-lv0  7.8G   37M  7.4G   1% /mnt

####如果我們需要移除/dev/vdb1,但是/dev/vdb1上面有數據,我們需要遷移數據至/dev/vdc1
[root@server ~]# pvmove /dev/vdb1 /dev/vdc1    ##遷移vdb1的數據至vdc1
  /dev/vdb1: Moved: 0.4%
  /dev/vdb1: Moved: 100.0%
[root@server ~]# vgreduce vg0 /dev/vdb1        ##從vg0中移除/dev/vdb1
  Removed "/dev/vdb1" from volume group "vg0"
[root@server ~]# pvremove /dev/vdb1            ##從pv中移除/dev/vdb1
  Labels on physical volume "/dev/vdb1" successfully wiped

####監控內容####
Every 1.0s: pvs;vgs;lvs;df -h /mnt                      Wed May  9 10:18:22 2018

  PV         VG   Fmt  Attr PSize  PFree
  /dev/vdc1  vg0  lvm2 a--  10.00g 2.00g
  VG   #PV #LV #SN Attr   VSize  VFree
  vg0    1   1   0 wz--n- 10.00g 2.00g
  LV   VG   Attr       LSize Pool Origin Data%  Move Log Cpy%Sync Convert
  lv0  vg0  -wi-ao---- 8.00g
Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/vg0-lv0  7.8G   37M  7.4G   1% /mnt

LVM快照

###將vdb1重新添加至vg0###
[root@server ~]# pvcreate /dev/vdb1            
  Physical volume "/dev/vdb1" successfully created
[root@server ~]# vgextend vg0 /dev/vdb1
  Volume group "vg0" successfully extended
[root@server ~]# touch /mnt/file{1..7}        ##創建測試文件
[root@server ~]# ls /mnt
file1  file2  file3  file4  file5  file6  file7  lost+found
[root@server ~]# umount /dev/vg0/lv0          ##解除掛載
###創建快照###
[root@server ~]# lvcreate -L 2G -n lvbak -s /dev/vg0/lv0    ##創建一個2G大小的快照,名稱lvbak
  Logical volume "lvbak" created
[root@server ~]# mount /dev/vg0/lvbak /mnt                  ##掛載快照
[root@server ~]# ls /mnt                                    ##查看測試文件
file1  file2  file3  file4  file5  file6  file7  lost+found
[root@server ~]# rm -rf /mnt/*                              ##刪除測試文件
[root@server ~]# ls /mnt                                    
[root@server ~]# umount /mnt                                ##解除掛載
[root@server ~]# lvremove /dev/vg0/lvbak                    ##刪除快照
Do you really want to remove active logical volume lvbak? [y/n]: y
  Logical volume "lvbak" successfully removed
[root@server ~]# lvcreate -L 2G -n lvbak -s /dev/vg0/lv0    ##重新創建快照
  Logical volume "lvbak" created
[root@server ~]# mount /dev/vg0/lvbak /mnt                  ##掛載
[root@server ~]# ls /mnt                                    ##查看
file1  file2  file3  file4  file5  file6  file7  lost+found

####監控內容####
Every 1.0s: pvs;vgs;lvs;df -h /mnt                      Wed May  9 10:35:16 2018

  PV         VG   Fmt  Attr PSize  PFree
  /dev/vdb1  vg0  lvm2 a--  10.00g 8.00g
  /dev/vdc1  vg0  lvm2 a--  10.00g 2.00g
  VG   #PV #LV #SN Attr   VSize  VFree
  vg0    2   2   1 wz--n- 19.99g 9.99g
  LV    VG   Attr       LSize Pool Origin Data%  Move Log Cpy%Sync Convert
  lv0   vg0  owi-a-s--- 8.00g
  lvbak vg0  swi-aos--- 2.00g       lv0      0.00
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/vg0-lvbak  7.8G   37M  7.4G   1% /mnt
####創建快照設備的數據被更改,重新建立快照就可恢復數據####

磁盤配額

[root@server ~]# umount /dev/vg0/lvbak                     
[root@server ~]# mount -o usrquota,grpquota /dev/vg0/lvbak /mnt    ##重新掛載激活配額功能參數
[root@server ~]# quotaon -ug /dev/vg0/lvba                         ##開啓配額
[root@server ~]# edquota -u student                                ##編輯student用戶的配額
isk quotas for user student (uid 1000):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/mapper/vg0-lvbak             0          0        20M          0        0        0

[root@server ~]# su - student                                      ##切換到student用戶
Last login: Wed May  9 10:57:10 EDT 2018 on pts/0
[student@server ~]$ dd if=/dev/zero of=/mnt/studentfile bs=1M count=10    ##創建一個10M大小的測試文件
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.00391326 s, 2.7 GB/s
[student@server ~]$ ls -hl /mnt/studentfile                         ##查看文件
-rw-rw-r--. 1 student student 10M May  9 10:59 /mnt/studentfile
[student@server ~]$ dd if=/dev/zero of=/mnt/studentfile bs=1M count=30    ##創建一個30M大小的測試文件
dm-1: write failed, user block limit reached.
dd: error writing ‘/mnt/studentfile’: Disk quota exceeded
21+0 records in
20+0 records out
20971520 bytes (21 MB) copied, 0.029637 s, 708 MB/s
[student@server ~]$ ls -lh /mnt/studentfile                    
-rw-rw-r--. 1 student student 20M May  9 11:03 /mnt/studentfile     ##配額20M,配額生效

###開機自動激活配額###
vim /etc/fstab
/dev/vg0/lvbak    /mnt        ext4    defaults,usrquota,grpquota    0 0

###關閉配額###
刪除/etc/fstab中的配額參數
[root@server ~]# quotaoff -ugv /dev/vg0/lvbak                     ##關閉配額功能
/dev/mapper/vg0-lvbak [/mnt]: group quotas turned off
/dev/mapper/vg0-lvbak [/mnt]: user quotas turned off

刪除LVM

[root@server ~]# umount /mnt                                      ##解除掛載
[root@server ~]# lvremove /dev/vg0/lvbak                          ##刪除lvbak
Do you really want to remove active logical volume lvbak? [y/n]: y
  Logical volume "lvbak" successfully removed
[root@server ~]# lvremove /dev/vg0/lv0                            ##刪除lv0
Do you really want to remove active logical volume lv0? [y/n]: y
  Logical volume "lv0" successfully removed
[root@server ~]# vgremove vg0                                     ##刪除vg0
  Volume group "vg0" successfully removed
[root@server ~]# pvremove /dev/vd{b,c}1                           ##刪除pv,/dev/vdb1和/dev/vdc1
  Labels on physical volume "/dev/vdb1" successfully wiped
  Labels on physical volume "/dev/vdc1" successfully wiped

###監控內容###
Every 1.0s: pvs;vgs;lvs;df -h /mnt                      Wed May  9 11:13:54 2018

  No volume groups found
  No volume groups found
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        10G  3.0G  7.1G  30% /

到此測試環境清理完畢,記得清理/etc/fstab中配置的自動掛載,否則開機時可能會出現錯誤。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章