【CentOS 7筆記23】, Logical Volume Manager邏輯卷管理#171105

hellopasswdnode


LVM介紹

  • 物理分區(Phycical Partions)linux

  • 物理存儲介質(PhysicalStorageMedia)
    指系統的物理存儲設備:磁盤,如:/dev/hda、/dev/sda等,是存儲系統最底層的存儲單元centos

  • 物理卷(Physical Volume,PV)
    物理卷是指磁盤分區或者從邏輯上看起來和磁盤分區相似得設備(好比RAID設備)app

  • 邏輯卷(Logical Volume,LV)
    一個或多個物理卷組成一個邏輯卷。對於LVM而言,邏輯卷相似於非LVM系統中的磁盤分區。邏輯卷能夠包含一個文件系統(好比/home或者/usr)socket

  • 卷組(Volume Group,VG)
    一個或者多個邏輯卷組成一個卷組。對於LVM而言,邏輯卷相似於非LVM系統中的物理磁盤。卷組把多個邏輯卷組合再一塊兒,造成了一個可管理的單元。ide

  • 物理塊(Physical Extent,PE)
    物理卷按大小相等的「塊」爲單位存儲,塊的大小與卷組中邏輯塊的大小相同。ui

  • 邏輯塊(Logical Extent,LE)
    邏輯卷按「塊」爲單位存儲,再一卷組中的全部邏輯卷的塊大小是相同的。spa

LVM實現過程:物理磁盤分區->物理卷->卷組->邏輯卷->格式化->掛載.net

準備磁盤分區

  • fdisk /dev/sdb
  • n 建立三個新分區,分別1G
  • t 改變分區類型爲8e
[root@localhost ~]# fdisk -l

	Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 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: 0x000c37f3

	   Device Boot      Start         End      Blocks   Id  System
	/dev/sda1   *        2048      411647      204800   83  Linux
	/dev/sda2          411648     4605951     2097152   82  Linux swap / Solaris
	/dev/sda3         4605952    62914559    29154304   83  Linux

	Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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

[root@localhost ~]# fdisk /dev/sdb
	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 0xe4f99297.

	Command (m for help): p

	Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0xe4f99297

	   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-20971519, default 2048): 
	Using default value 2048
	Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +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): p
	Partition number (2-4, default 2): 2
	First sector (2099200-20971519, default 2099200): 
	Using default value 2099200
	Last sector, +sectors or +size{K,M,G} (2099200-20971519, default 20971519): +1G
	Partition 2 of type Linux and of size 1 GiB is set

	Command (m for help): n  
	Partition type:
	   p   primary (2 primary, 0 extended, 2 free)
	   e   extended
	Select (default p): p
	Partition number (3,4, default 3): 3
	First sector (4196352-20971519, default 4196352): 
	Using default value 4196352
	Last sector, +sectors or +size{K,M,G} (4196352-20971519, default 20971519): +1G
	Partition 3 of type Linux and of size 1 GiB is set

	Command (m for help): p

	Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0xe4f99297

	   Device Boot      Start         End      Blocks   Id  System
	/dev/sdb1            2048     2099199     1048576   83  Linux
	/dev/sdb2         2099200     4196351     1048576   83  Linux
	/dev/sdb3         4196352     6293503     1048576   83  Linux

	Command (m for help):
Command (m for help): t
	Partition number (1-3, default 3): 1
	Hex code (type L to list all codes): 8e
	Changed type of partition 'Linux' to 'Linux LVM'

	Command (m for help): t
	Partition number (1-3, default 3): 2
	Hex code (type L to list all codes): 8e
	Changed type of partition 'Linux' to 'Linux LVM'

	Command (m for help): t
	Partition number (1-3, default 3): 3
	Hex code (type L to list all codes): 8e   
	Changed type of partition 'Linux' to 'Linux LVM'

	Command (m for help): p

	Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x5ec6916a

	   Device Boot      Start         End      Blocks   Id  System
	/dev/sdb1            2048     2099199     1048576   8e  Linux LVM
	/dev/sdb2         2099200     4196351     1048576   8e  Linux LVM
	/dev/sdb3         4196352     6293503     1048576   8e  Linux LVM

	Command (m for help): w

[root@localhost ~]# fdisk -l

	Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 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: 0x000c37f3

	   Device Boot      Start         End      Blocks   Id  System
	/dev/sda1   *        2048      411647      204800   83  Linux
	/dev/sda2          411648     4605951     2097152   82  Linux swap / Solaris
	/dev/sda3         4605952    62914559    29154304   83  Linux

	Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x5ec6916a

	   Device Boot      Start         End      Blocks   Id  System
	/dev/sdb1            2048     2099199     1048576   8e  Linux LVM
	/dev/sdb2         2099200     4196351     1048576   8e  Linux LVM
	/dev/sdb3         4196352     6293503     1048576   8e  Linux LVM

準備物理卷

  • pvcreate /dev/sdb1code

  • pvcreate /dev/sdb2

  • pvcreate /dev/sdb3

  • pvdisplay 列出當前的物理卷

  • pvremove /dev/sdb3 刪除物理卷

安裝lvm包

[root@localhost ~]# yum install -y lvm
Loaded plugins: fastestmirror
base                                                                        | 3.6 kB  00:00:00     
extras                                                                      | 3.4 kB  00:00:00     
updates                                                                     | 3.4 kB  00:00:00     
(1/2): extras/7/x86_64/primary_db                                           | 129 kB  00:00:00     
(2/2): updates/7/x86_64/primary_db                                          | 3.6 MB  00:00:24     
Loading mirror speeds from cached hostfile
 * base: mirrors.shuosc.org
 * extras: mirrors.nju.edu.cn
 * updates: mirrors.nju.edu.cn
No package lvm available.
Error: Nothing to do

上面安裝lvm時出現了問題,說明lvm軟件包名不是爲lvm,當咱們知道某一個軟件包裏的某一個命令時,能夠使用yum provides 「路徑」來查詢到軟件包
例如:[root@localhost ~]# yum provides "/*/pvcreate"

[root@localhost ~]#  yum install -y lvm2
	Loaded plugins: fastestmirror
	base                                                                        | 3.6 kB  00:00:00     
	extras                                                                      | 3.4 kB  00:00:00     
	updates                                                                     | 3.4 kB  00:00:00     
	Determining fastest mirrors
	 * base: mirrors.btte.net
	 * extras: ftp.sjtu.edu.cn
	 * updates: mirrors.btte.net
	Resolving Dependencies
	--> Running transaction check
	---> Package lvm2.x86_64 7:2.02.171-8.el7 will be installed
	--> Processing Dependency: lvm2-libs = 7:2.02.171-8.el7 for package: 7:lvm2-2.02.171-8.el7.x86_64
	--> Processing Dependency: device-mapper-persistent-data >= 0.7.0-0.1.rc6 for package: 7:lvm2-2.02.171-8.el7.x86_64
	--> Processing Dependency: liblvm2app.so.2.2(Base)(64bit) for package: 7:lvm2-2.02.171-8.el7.x86_64
	--> Processing Dependency: libdevmapper.so.1.02(DM_1_02_97)(64bit) for package: 7:lvm2-2.02.171-8.el7.x86_64
	--> Processing Dependency: libdevmapper.so.1.02(DM_1_02_138)(64bit) for package: 7:lvm2-2.02.171-8.el7.x86_64
	--> Processing Dependency: libdevmapper.so.1.02(DM_1_02_135)(64bit) for package: 7:lvm2-2.02.171-8.el7.x86_64
	--> Processing Dependency: libdevmapper.so.1.02(DM_1_02_133)(64bit) for package: 7:lvm2-2.02.171-8.el7.x86_64
	--> Processing Dependency: libdevmapper.so.1.02(DM_1_02_128)(64bit) for package: 7:lvm2-2.02.171-8.el7.x86_64
	--> Processing Dependency: libdevmapper.so.1.02(DM_1_02_113)(64bit) for package: 7:lvm2-2.02.171-8.el7.x86_64
	--> Processing Dependency: libdevmapper.so.1.02(DM_1_02_110)(64bit) for package: 7:lvm2-2.02.171-8.el7.x86_64
	--> Processing Dependency: libdevmapper.so.1.02(DM_1_02_107)(64bit) for package: 7:lvm2-2.02.171-8.el7.x86_64
	--> Processing Dependency: libdevmapper.so.1.02(DM_1_02_104)(64bit) for package: 7:lvm2-2.02.171-8.el7.x86_64
	--> Processing Dependency: libdevmapper.so.1.02(DM_1_02_103)(64bit) for package: 7:lvm2-2.02.171-8.el7.x86_64
	--> Processing Dependency: libdevmapper.so.1.02(DM_1_02_101)(64bit) for package: 7:lvm2-2.02.171-8.el7.x86_64
	--> Processing Dependency: libdevmapper.so.1.02(DM_1_02_100)(64bit) for package: 7:lvm2-2.02.171-8.el7.x86_64
	--> Processing Dependency: libdevmapper-event.so.1.02(Base)(64bit) for package: 7:lvm2-2.02.171-8.el7.x86_64
	--> Processing Dependency: liblvm2app.so.2.2()(64bit) for package: 7:lvm2-2.02.171-8.el7.x86_64
	--> Processing Dependency: libdevmapper-event.so.1.02()(64bit) for package: 7:lvm2-2.02.171-8.el7.x86_64
	--> Running transaction check
	---> Package device-mapper-event-libs.x86_64 7:1.02.140-8.el7 will be installed
	---> Package device-mapper-libs.x86_64 7:1.02.84-14.el7 will be updated
	--> Processing Dependency: device-mapper-libs = 7:1.02.84-14.el7 for package: 7:device-mapper-1.02.84-14.el7.x86_64
	---> Package device-mapper-libs.x86_64 7:1.02.140-8.el7 will be an update
	---> Package device-mapper-persistent-data.x86_64 0:0.7.0-0.1.rc6.el7 will be installed
	--> Processing Dependency: libaio.so.1(LIBAIO_0.4)(64bit) for package: device-mapper-persistent-data-0.7.0-0.1.rc6.el7.x86_64
	--> Processing Dependency: libaio.so.1(LIBAIO_0.1)(64bit) for package: device-mapper-persistent-data-0.7.0-0.1.rc6.el7.x86_64
	--> Processing Dependency: libaio.so.1()(64bit) for package: device-mapper-persistent-data-0.7.0-0.1.rc6.el7.x86_64
	---> Package lvm2-libs.x86_64 7:2.02.171-8.el7 will be installed
	--> Processing Dependency: device-mapper-event = 7:1.02.140-8.el7 for package: 7:lvm2-libs-2.02.171-8.el7.x86_64
	--> Running transaction check
	---> Package device-mapper.x86_64 7:1.02.84-14.el7 will be updated
	---> Package device-mapper.x86_64 7:1.02.140-8.el7 will be an update
	---> Package device-mapper-event.x86_64 7:1.02.140-8.el7 will be installed
	---> Package libaio.x86_64 0:0.3.109-13.el7 will be installed
	--> Finished Dependency Resolution
	
	Dependencies Resolved
	
	===================================================================================================
	 Package                               Arch           Version                   Repository    Size
	===================================================================================================
	Installing:
	 lvm2                                  x86_64         7:2.02.171-8.el7          base         1.3 M
	Installing for dependencies:
	 device-mapper-event                   x86_64         7:1.02.140-8.el7          base         180 k
	 device-mapper-event-libs              x86_64         7:1.02.140-8.el7          base         179 k
	 device-mapper-persistent-data         x86_64         0.7.0-0.1.rc6.el7         base         400 k
	 libaio                                x86_64         0.3.109-13.el7            base          24 k
	 lvm2-libs                             x86_64         7:2.02.171-8.el7          base         1.0 M
	Updating for dependencies:
	 device-mapper                         x86_64         7:1.02.140-8.el7          base         280 k
	 device-mapper-libs                    x86_64         7:1.02.140-8.el7          base         312 k

	Transaction Summary
	===================================================================================================
	Install  1 Package  (+5 Dependent packages)
	Upgrade             ( 2 Dependent packages)

	Total download size: 3.6 M
	Downloading packages:
	Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
	(1/8): device-mapper-event-1.02.140-8.el7.x86_64.rpm                        | 180 kB  00:00:00     
	(2/8): libaio-0.3.109-13.el7.x86_64.rpm                                     |  24 kB  00:00:00     
	(3/8): device-mapper-1.02.140-8.el7.x86_64.rpm                              | 280 kB  00:00:01     
	(4/8): device-mapper-libs-1.02.140-8.el7.x86_64.rpm                         | 312 kB  00:00:01     
	(5/8): device-mapper-persistent-data-0.7.0-0.1.rc6.el7.x86_64.rpm           | 400 kB  00:00:02     
	(6/8): device-mapper-event-libs-1.02.140-8.el7.x86_64.rpm                   | 179 kB  00:00:02     
	(7/8): lvm2-libs-2.02.171-8.el7.x86_64.rpm                                  | 1.0 MB  00:00:02     
	(8/8): lvm2-2.02.171-8.el7.x86_64.rpm                                       | 1.3 MB  00:00:03     
	---------------------------------------------------------------------------------------------------
	Total                                                              912 kB/s | 3.6 MB  00:00:04     
	Running transaction check
	Running transaction test
	Transaction test succeeded
	Running transaction
	  Updating   : 7:device-mapper-libs-1.02.140-8.el7.x86_64                                     1/10 
	  Updating   : 7:device-mapper-1.02.140-8.el7.x86_64                                          2/10 
	  Installing : 7:device-mapper-event-libs-1.02.140-8.el7.x86_64                               3/10 
	  Installing : 7:device-mapper-event-1.02.140-8.el7.x86_64                                    4/10 
	  Installing : 7:lvm2-libs-2.02.171-8.el7.x86_64                                              5/10 
	  Installing : libaio-0.3.109-13.el7.x86_64                                                   6/10 
	  Installing : device-mapper-persistent-data-0.7.0-0.1.rc6.el7.x86_64                         7/10 
	  Installing : 7:lvm2-2.02.171-8.el7.x86_64                                                   8/10 
	ln -s '/usr/lib/systemd/system/lvm2-lvmpolld.socket' '/etc/systemd/system/sysinit.target.wants/lvm2-lvmpolld.socket'
	  Cleanup    : 7:device-mapper-libs-1.02.84-14.el7.x86_64                                     9/10 
	  Cleanup    : 7:device-mapper-1.02.84-14.el7.x86_64                                         10/10 
	  Verifying  : 7:device-mapper-event-libs-1.02.140-8.el7.x86_64                               1/10 
	  Verifying  : 7:device-mapper-event-1.02.140-8.el7.x86_64                                    2/10 
	  Verifying  : 7:lvm2-libs-2.02.171-8.el7.x86_64                                              3/10 
	  Verifying  : 7:device-mapper-1.02.140-8.el7.x86_64                                          4/10 
	  Verifying  : 7:device-mapper-libs-1.02.140-8.el7.x86_64                                     5/10 
	  Verifying  : 7:lvm2-2.02.171-8.el7.x86_64                                                   6/10 
	  Verifying  : device-mapper-persistent-data-0.7.0-0.1.rc6.el7.x86_64                         7/10 
	  Verifying  : libaio-0.3.109-13.el7.x86_64                                                   8/10 
	  Verifying  : 7:device-mapper-libs-1.02.84-14.el7.x86_64                                     9/10 
	  Verifying  : 7:device-mapper-1.02.84-14.el7.x86_64                                         10/10 

	Installed:
	  lvm2.x86_64 7:2.02.171-8.el7                                                                     

	Dependency Installed:
	  device-mapper-event.x86_64 7:1.02.140-8.el7                                                      
	  device-mapper-event-libs.x86_64 7:1.02.140-8.el7                                                 
	  device-mapper-persistent-data.x86_64 0:0.7.0-0.1.rc6.el7                                         
	  libaio.x86_64 0:0.3.109-13.el7                                                                   
	  lvm2-libs.x86_64 7:2.02.171-8.el7                                                                

	Dependency Updated:
	  device-mapper.x86_64 7:1.02.140-8.el7         device-mapper-libs.x86_64 7:1.02.140-8.el7        

	Complete!

partprobe重載分區表

[root@localhost ~]# ls /dev/sdb
[root@localhost ~]# partprobe
	Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
	Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
	Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
[root@localhost ~]# pvcreate /dev/sdb1
	  Device /dev/sdb1 not found (or ignored by filtering).
[root@localhost ~]# df -h
	Filesystem      Size  Used Avail Use% Mounted on
	/dev/sda3        28G  865M   27G   4% /
	devtmpfs        909M     0  909M   0% /dev
	tmpfs           914M     0  914M   0% /dev/shm
	tmpfs           914M   17M  897M   2% /run
	tmpfs           914M     0  914M   0% /sys/fs/cgroup
	/dev/sdb         10G   33M   10G   1% /mnt
	/dev/sda1       197M   75M  123M  38% /boot
[root@localhost ~]# umount /mnt

pvcreate將物理磁盤分區爲物理卷

[root@localhost ~]# pvcreate /dev/sdb1
	WARNING: ext4 signature detected on /dev/sdb1 at offset 1080. Wipe it? [y/n]: y
	  Wiping ext4 signature on /dev/sdb1.
	  Physical volume "/dev/sdb1" successfully created.
[root@localhost ~]# pvcreate /dev/sdb2
	  Physical volume "/dev/sdb2" successfully created.
[root@localhost ~]# pvcreate /dev/sdb3
	  Physical volume "/dev/sdb3" successfully created.

pvdisplay顯示物理卷屬性

[root@localhost ~]# pvdisplay
	  "/dev/sdb3" is a new physical volume of "1.00 GiB"
	  --- NEW Physical volume ---
	  PV Name               /dev/sdb3
	  VG Name               
	  PV Size               1.00 GiB
	  Allocatable           NO
	  PE Size               0   
	  Total PE              0
	  Free PE               0
	  Allocated PE          0
	  PV UUID               JeafFf-5aVO-xNf2-DUv6-17S2-N8Cg-f0E3vX
   
	  "/dev/sdb1" is a new physical volume of "1.00 GiB"
	  --- NEW Physical volume ---
	  PV Name               /dev/sdb1
	  VG Name               
	  PV Size               1.00 GiB
	  Allocatable           NO
	  PE Size               0   
	  Total PE              0
	  Free PE               0
	  Allocated PE          0
	  PV UUID               DEzGdZ-eZpX-qWYI-XndU-e5X9-SEcT-MsiLCc
   
	  "/dev/sdb2" is a new physical volume of "1.00 GiB"
	  --- NEW Physical volume ---
	  PV Name               /dev/sdb2
	  VG Name               
	  PV Size               1.00 GiB
	  Allocatable           NO
	  PE Size               0   
	  Total PE              0
	  Free PE               0
	  Allocated PE          0
	  PV UUID               QI4y20-GOdg-8CBJ-PfZF-GO8l-ambD-f7Jikz

建立卷組

pvs輸出格式化物理卷信息報表

[root@localhost ~]# pvs
	  PV         VG Fmt  Attr PSize PFree
	  /dev/sdb1     lvm2 ---  1.00g 1.00g
	  /dev/sdb2     lvm2 ---  1.00g 1.00g
	  /dev/sdb3     lvm2 ---  1.00g 1.00g

vgcreate 建立lvm卷組

[root@localhost ~]# vgcreate vg1 /dev/sdb1 /dev/sdb2
	  Volume group "vg1" successfully created
[root@localhost ~]# vgdisplay
	  --- Volume group ---
	  VG Name               vg1
	  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               ad3wQ3-M3m1-btaW-ae4b-RhQl-9iPM-xBDFaS

vgs查詢lvm卷組

[root@localhost ~]# vgs
	  VG  #PV #LV #SN Attr   VSize VFree
	  vg1   2   0   0 wz--n- 1.99g 1.99g

vgremove 刪除卷組

建立邏輯卷

lvcreate建立lvm邏輯卷

[root@localhost ~]# lvcreate -L 100M -n lv1 vg1
	  Logical volume "lv1" created.
[root@localhost ~]# mkfs.ext4 /dev/vg1/lv1
	mke2fs 1.42.9 (28-Dec-2013)
	Filesystem label=
	OS type: Linux
	Block size=1024 (log=0)
	Fragment size=1024 (log=0)
	Stride=0 blocks, Stripe width=0 blocks
	25688 inodes, 102400 blocks
	5120 blocks (5.00%) reserved for the super user
	First data block=1
	Maximum filesystem blocks=33685504
	13 block groups
	8192 blocks per group, 8192 fragments per group
	1976 inodes per group
	Superblock backups stored on blocks: 
		8193, 24577, 40961, 57345, 73729

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

[root@localhost ~]# mount /dev/vg1/lv1 /mnt/
[root@localhost ~]# df -h
	Filesystem           Size  Used Avail Use% Mounted on
	/dev/sda3             28G  994M   27G   4% /
	devtmpfs             903M     0  903M   0% /dev
	tmpfs                912M     0  912M   0% /dev/shm
	tmpfs                912M  8.6M  904M   1% /run
	tmpfs                912M     0  912M   0% /sys/fs/cgroup
	/dev/sda1            197M  113M   85M  58% /boot
	tmpfs                183M     0  183M   0% /run/user/0
	/dev/mapper/vg1-lv1   93M  1.6M   85M   2% /mnt

這時候咱們會發現/dev/mapper/vg1-lv1這個目錄,對如下兩個路徑,其實這個兩個目錄都是指向同一個目錄的

[root@localhost ~]# ls -l /dev/vg1/lv1
	lrwxrwxrwx. 1 root root 7 Nov  6 09:09 /dev/vg1/lv1 -> ../dm-0
[root@localhost ~]# ls -l /dev/mapper/vg1-lv1
	lrwxrwxrwx. 1 root root 7 Nov  6 09:09 /dev/mapper/vg1-lv1 -> ../dm-0
[root@localhost ~]# mkdir /mnt/111
[root@localhost ~]# touch /mnt/1.txt
[root@localhost ~]# echo "22222" > /mnt/1.txt

擴容邏輯卷

  • lvresize -L 300M /dev/vg1/lv1 #重置設置卷大小
  • e2fsck -f /dev/vg1/lv1 #檢查磁盤錯誤(ext4執行)
  • resize2fs /dev/vg1/lv1 #更新邏輯卷信息(ext4執行)
  • xfs_growfs /dev/vg1/lv1 #xfs文件系統須要執行
[root@localhost ~]# lvresize -L 200M /dev/vg1/lv1
	  Size of logical volume vg1/lv1 changed from 100.00 MiB (25 extents) to 200.00 MiB (50 extents).
	  Logical volume vg1/lv1 successfully resized.

[root@localhost ~]# df -h
	Filesystem           Size  Used Avail Use% Mounted on
	/dev/sda3             28G  993M   27G   4% /
	devtmpfs             903M     0  903M   0% /dev
	tmpfs                912M     0  912M   0% /dev/shm
	tmpfs                912M  8.6M  904M   1% /run
	tmpfs                912M     0  912M   0% /sys/fs/cgroup
	/dev/sda1            197M  113M   85M  58% /boot
	tmpfs                183M     0  183M   0% /run/user/0
	/dev/mapper/vg1-lv1   93M  1.6M   85M   2% /mnt

不合規範,應當先

[root@localhost ~]# umount /mnt/
[root@localhost ~]# lvresize -L 200M /dev/vg1/lv1
	  New size (50 extents) matches existing size (50 extents).

檢查磁盤是否存在錯誤

[root@localhost ~]# e2fsck -f /dev/vg1/lv1
	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/vg1/lv1: 13/25688 files (7.7% non-contiguous), 8899/102400 blocks

更新邏輯卷

[root@localhost ~]# resize2fs /dev/vg1/lv1
	resize2fs 1.42.9 (28-Dec-2013)
	Resizing the filesystem on /dev/vg1/lv1 to 204800 (1k) blocks.
	The filesystem on /dev/vg1/lv1 is now 204800 blocks long.
[root@localhost ~]# mount /dev/vg1/lv1 /mnt/
[root@localhost ~]# df -h
	Filesystem           Size  Used Avail Use% Mounted on
	/dev/sda3             28G  993M   27G   4% /
	devtmpfs             903M     0  903M   0% /dev
	tmpfs                912M     0  912M   0% /dev/shm
	tmpfs                912M  8.6M  904M   1% /run
	tmpfs                912M     0  912M   0% /sys/fs/cgroup
	/dev/sda1            197M  113M   85M  58% /boot
	tmpfs                183M     0  183M   0% /run/user/0
	/dev/mapper/vg1-lv1  190M  1.6M  175M   1% /mnt
[root@localhost ~]# ls /mnt/
	111  1.txt  lost+found
[root@localhost ~]# cat /mnt/1.txt 
	22222

若擴容致使文件丟失,那麼擴容就沒有存在的意義

若格式爲xfs那麼方法有些不同

縮減邏輯卷

  • 先umount
  • e2fsck -f /dev/vg1/lv1 #檢查磁盤錯誤(ext)
  • resize2fs /dev/vg1/lv1 100M #更新邏輯卷信息(ext)
  • lvresize -L 100M /dev/vg1/lv1 #從新設置卷大小

xfs並不支持

[root@localhost ~]# umount /mnt/
[root@localhost ~]# e2fsck -f /dev/vg1/lv1
	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/vg1/lv1: 13/49400 files (7.7% non-contiguous), 11887/204800 blocks
[root@localhost ~]# resize2fs /dev/vg1/lv1 100M
	resize2fs 1.42.9 (28-Dec-2013)
	Resizing the filesystem on /dev/vg1/lv1 to 102400 (1k) blocks.
	The filesystem on /dev/vg1/lv1 is now 102400 blocks long.
[root@localhost ~]# lvresize -L 100M /dev/vg1/lv1
	  WARNING: Reducing active logical volume to 100.00 MiB.
	  THIS MAY DESTROY YOUR DATA (filesystem etc.)
	Do you really want to reduce vg1/lv1? [y/n]: y
	  Size of logical volume vg1/lv1 changed from 200.00 MiB (50 extents) to 100.00 MiB (25 extents).
	  Logical volume vg1/lv1 successfully resized.
[root@localhost ~]# lvdisplay
	  --- Logical volume ---
	  LV Path                /dev/vg1/lv1
	  LV Name                lv1
	  VG Name                vg1
	  LV UUID                BTZG2e-59Qy-29ls-X6Ko-Dvbm-0zUp-2MTdpk
	  LV Write Access        read/write
	  LV Creation host, time centos1133, 2017-11-06 09:05:29 +0800
	  LV Status              available
	  # open                 0
	  LV Size                100.00 MiB
	  Current LE             25
	  Segments               1
	  Allocation             inherit
	  Read ahead sectors     auto
	  - currently set to     8192
	  Block device           253:0
[root@localhost ~]# lvs
	  LV   VG  Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
	  lv1  vg1 -wi-a----- 100.00m 
[root@localhost ~]# mount /dev/vg1/lv1 /mnt/
[root@localhost ~]# ls /mnt/
	111  1.txt  lost+found
[root@localhost ~]# df -h
	Filesystem           Size  Used Avail Use% Mounted on
	/dev/sda3             28G  994M   27G   4% /
	devtmpfs             903M     0  903M   0% /dev
	tmpfs                912M     0  912M   0% /dev/shm
	tmpfs                912M  8.6M  904M   1% /run
	tmpfs                912M     0  912M   0% /sys/fs/cgroup
	/dev/sda1            197M  113M   85M  58% /boot
	tmpfs                183M     0  183M   0% /run/user/0
	/dev/mapper/vg1-lv1   93M  1.6M   85M   2% /mnt
[root@localhost ~]# umount /mnt/

lvm並不會由於縮容而致使文件丟失

xfs擴容

[root@localhost ~]# mkfs.xfs -f /dev/vg1/lv1
	meta-data=/dev/vg1/lv1           isize=512    agcount=4, agsize=6400 blks
	         =                       sectsz=512   attr=2, projid32bit=1
	         =                       crc=1        finobt=0, sparse=0
	data     =                       bsize=4096   blocks=25600, imaxpct=25
	         =                       sunit=0      swidth=0 blks
	naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
	log      =internal log           bsize=4096   blocks=855, version=2
	         =                       sectsz=512   sunit=0 blks, lazy-count=1
	realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost ~]# mount /dev/vg1/lv1 /mnt/
[root@localhost ~]# ls /mnt/
[root@localhost ~]# touch /mnt/2.txt
[root@localhost ~]# echo 123 > /mnt/2.txt
[root@localhost ~]# cat !$
	cat /mnt/2.txt
	123
[root@localhost ~]# lvs
	  LV   VG  Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
	  lv1  vg1 -wi-ao---- 100.00m

xfs擴容時不須要umount

[root@localhost ~]# lvresize -L 300M /dev/vg1/lv1
	  Size of logical volume vg1/lv1 changed from 100.00 MiB (25 extents) to 300.00 MiB (75 extents).
	  Logical volume vg1/lv1 successfully resized.
[root@localhost ~]# lvs
	  LV   VG  Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
	  lv1  vg1 -wi-ao---- 300.00m   
[root@localhost ~]# df -h
	Filesystem           Size  Used Avail Use% Mounted on
	/dev/sda3             28G  994M   27G   4% /
	devtmpfs             903M     0  903M   0% /dev
	tmpfs                912M     0  912M   0% /dev/shm
	tmpfs                912M  8.6M  904M   1% /run
	tmpfs                912M     0  912M   0% /sys/fs/cgroup
	/dev/sda1            197M  113M   85M  58% /boot
	tmpfs                183M     0  183M   0% /run/user/0
	/dev/mapper/vg1-lv1   97M  5.2M   92M   6% /mnt

[root@localhost ~]# xfs_growfs -L 300M /dev/vg1/lv1
	meta-data=/dev/mapper/vg1-lv1    isize=512    agcount=4, agsize=6400 blks
	         =                       sectsz=512   attr=2, projid32bit=1
	         =                       crc=1        finobt=0 spinodes=0
	data     =                       bsize=4096   blocks=25600, imaxpct=25
	         =                       sunit=0      swidth=0 blks
	naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
	log      =internal               bsize=4096   blocks=855, version=2
	         =                       sectsz=512   sunit=0 blks, lazy-count=1
	realtime =none                   extsz=4096   blocks=0, rtextents=0
	xfs_growfs: XFS_IOC_FSGROWFSLOG xfsctl failed: Invalid argument

無效的參數?

[root@localhost ~]# umount /dev/vg1/lv1
[root@localhost ~]# lvresize -L 310M /dev/vg1/lv1
	  Rounding size to boundary between physical extents: 312.00 MiB.
	  Size of logical volume vg1/lv1 changed from 300.00 MiB (75 extents) to 312.00 MiB (78 extents).
	  Logical volume vg1/lv1 successfully resized.
[root@localhost ~]# xfs_growfs -L 310M /dev/vg1/lv1
	xfs_growfs: /dev/vg1/lv1 is not a mounted XFS filesystem	#必須掛載
[root@localhost ~]# mount /dev/vg1/lv1 /mnt/
[root@localhost ~]# xfs_growfs -L 310M /dev/vg1/lv1
	meta-data=/dev/mapper/vg1-lv1    isize=512    agcount=4, agsize=6400 blks
	         =                       sectsz=512   attr=2, projid32bit=1
	         =                       crc=1        finobt=0 spinodes=0
	data     =                       bsize=4096   blocks=25600, imaxpct=25
	         =                       sunit=0      swidth=0 blks
	naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
	log      =internal               bsize=4096   blocks=855, version=2
	         =                       sectsz=512   sunit=0 blks, lazy-count=1
	realtime =none                   extsz=4096   blocks=0, rtextents=0
	xfs_growfs: XFS_IOC_FSGROWFSLOG xfsctl failed: Invalid argument

[root@localhost ~]# xfs_growfs /dev/vg1/lv1
	meta-data=/dev/mapper/vg1-lv1    isize=512    agcount=4, agsize=6400 blks
	         =                       sectsz=512   attr=2, projid32bit=1
	         =                       crc=1        finobt=0 spinodes=0
	data     =                       bsize=4096   blocks=25600, imaxpct=25
	         =                       sunit=0      swidth=0 blks
	naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
	log      =internal               bsize=4096   blocks=855, version=2
	         =                       sectsz=512   sunit=0 blks, lazy-count=1
	realtime =none                   extsz=4096   blocks=0, rtextents=0
	data blocks changed from 25600 to 79872
[root@localhost ~]# df -h
	Filesystem           Size  Used Avail Use% Mounted on
	/dev/sda3             28G  994M   27G   4% /
	devtmpfs             903M     0  903M   0% /dev
	tmpfs                912M     0  912M   0% /dev/shm
	tmpfs                912M  8.6M  904M   1% /run
	tmpfs                912M     0  912M   0% /sys/fs/cgroup
	/dev/sda1            197M  113M   85M  58% /boot
	tmpfs                183M     0  183M   0% /run/user/0
	/dev/mapper/vg1-lv1  309M  5.5M  304M   2% /mnt

擴展卷組

  • fdisk /dev/sdb #新增/dev/sdb5(邏輯分區8e) 2G
  • pvcreate /dev/sdb5 #建立物理卷
  • vgextend vg1 /dev/sdb5 #把物理卷加入卷組中
  • lvresize -L 100M /dev/vg1/lv1 #從新設置卷大小

擴容卷組最高能擴到卷組同樣大

[root@localhost ~]# vgs
	  VG  #PV #LV #SN Attr   VSize VFree 
	  vg1   2   1   0 wz--n- 1.99g <1.69g

sdb1和sdb2組成卷組,而sdb3是空閒的

[root@localhost ~]# vgextend vg1 /dev/sdb3
	  Volume group "vg1" successfully extended
[root@localhost ~]# vgdisplay
	  --- Volume group ---
	  VG Name               vg1
	  System ID             
	  Format                lvm2
	  Metadata Areas        3
	  Metadata Sequence No  7
	  VG Access             read/write
	  VG Status             resizable
	  MAX LV                0
	  Cur LV                1
	  Open LV               1
	  Max PV                0
	  Cur PV                3
	  Act PV                3
	  VG Size               <2.99 GiB
	  PE Size               4.00 MiB
	  Total PE              765
	  Alloc PE / Size       78 / 312.00 MiB
	  Free  PE / Size       687 / 2.68 GiB
	  VG UUID               G7DZt6-wLGz-HqEr-erYp-lDYP-53eM-KBf5NB
[root@localhost ~]# vgs
	  VG  #PV #LV #SN Attr   VSize  VFree
	  vg1   3   1   0 wz--n- <2.99g 2.68g

先擴容卷組,再擴容邏輯卷


【CentOS 7筆記21】,磁盤掛載
【CentOS 7筆記22】,手動增長Swap空間
【CentOS 7筆記23】, Logical Volume Manager邏輯卷管理
【CentOS 7筆記24】,實驗中發生的磁盤小故障
【CentOS 7筆記25】,Vim的使用

修改於171110

相關文章
相關標籤/搜索