1. 程式人生 > >四周第一次課

四周第一次課

lvm 擴容 xfs

4.10/4.11/4.12 lvm講解

簡介
邏輯卷管理LVM是一個多才多藝的硬盤系統工具。無論在Linux或者其他類似的系統,都是非常的好用。傳統分區使用固定大小分區,重新調整大小十分麻煩。但是,LVM可以創建和管理“邏輯”卷,而不是直接使用物理硬盤。可以讓管理員彈性的管理邏輯卷的擴大縮小,操作簡單,而不損壞已存儲的數據。可以隨意將新的硬盤添加到LVM,以直接擴展已經存在的邏輯卷。LVM並不需要重啟就可以讓內核知道分區的存在。

LVM(Logical Volume Manager)邏輯卷管理,是一種將一個或多個硬盤的分區在邏輯上集合,相當於一個大硬盤來使用,使用LVM主要是方便管理、增加了系統的擴展性,lvm一般應用與大型應用搞分布式文件系統。 比如hdfs、gfs、mfs等;通過LVM可以方便靈活的調整存儲卷組的大小,

  1. PV(Physical Volume):物理卷,處於LVM最底層,可以是物理硬盤或者分區。
    2.PE(Physical Extend):物理區域,PV中可以用於分配的最小存儲單元,可以在創建PV的時候制定,如1M, 2M, 4M, 8M, 32M, 64M…組成同一VG中所有PV的PE大小應該相同。
    3.VG(Volume Group):卷組,建立在PV之上,可以含有一個到多個PV。
  2. LV(Logical Volume):邏輯卷,建立在VG之上,相當於原來分區的概念。不過大小可以動態改變。

LVM使用分層結構,如下圖所示。
技術分享圖片
首先是實際的物理磁盤及其劃分的分區和其上的物理卷(PV)。一個或多個物理卷可以用來創建卷組(VG)。然後基於卷組可以創建邏輯卷(LV)。只要在卷組中有可用空間,就可以隨心所欲的創建邏輯卷。文件系統就是在邏輯卷上創建的,然後可以在操作系統掛載和訪問

磁盤分區
1、首先使用fdisk增加的磁盤分區

#fdisk /dev/sdb 
Command (m for help): p

Disk /dev/sdb: 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: 0xff3a8918

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   8e  Linux LVM
/dev/sdb2         4196352     8390655     2097152   8e  Linux LVM
/dev/sdb3         8390656    12584959     2097152   8e  Linux LVM

迅速創建擴展分區,並劃分3個邏輯分區

Command (m for help): n
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): e
Selected partition 4
First sector (12584960-41943039, default 12584960): 
Using default value 12584960
Last sector, +sectors or +size{K,M,G} (12584960-41943039, default 41943039): 
Using default value 41943039
Partition 4 of type Extended and of size 14 GiB is set

Command (m for help): n
All primary partitions are in use
Adding logical partition 5
First sector (12587008-41943039, default 12587008): 
Using default value 12587008
Last sector, +sectors or +size{K,M,G} (12587008-41943039, default 41943039): +2G
Partition 5 of type Linux and of size 2 GiB is set

Command (m for help): n
All primary partitions are in use
Adding logical partition 6
First sector (16783360-41943039, default 16783360): 
Using default value 16783360
Last sector, +sectors or +size{K,M,G} (16783360-41943039, default 41943039): +2G
Partition 6 of type Linux and of size 2 GiB is set

Command (m for help): n  
All primary partitions are in use
Adding logical partition 7
First sector (20979712-41943039, default 20979712): 
Using default value 20979712
Last sector, +sectors or +size{K,M,G} (20979712-41943039, default 41943039): +2G
Partition 7 of type Linux and of size 2 GiB is set

Command (m for help): p

Disk /dev/sdb: 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: 0xff3a8918

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   8e  Linux LVM
/dev/sdb2         4196352     8390655     2097152   8e  Linux LVM
/dev/sdb3         8390656    12584959     2097152   8e  Linux LVM
/dev/sdb4        12584960    41943039    14679040    5  Extended
/dev/sdb5        12587008    16781311     2097152   83  Linux
/dev/sdb6        16783360    20977663     2097152   83  Linux
/dev/sdb7        20979712    25174015     2097152   83  Linux

2、t更改分區文件系統的ID為8E,並w保存操作

Command (m for help): t
Partition number (1-7, default 7): 5
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-7, default 7): 6
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-7, default 7): 
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: 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: 0xff3a8918

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   8e  Linux LVM
/dev/sdb2         4196352     8390655     2097152   8e  Linux LVM
/dev/sdb3         8390656    12584959     2097152   8e  Linux LVM
/dev/sdb4        12584960    41943039    14679040    5  Extended
/dev/sdb5        12587008    16781311     2097152   8e  Linux LVM
/dev/sdb6        16783360    20977663     2097152   8e  Linux LVM
/dev/sdb7        20979712    25174015     2097152   8e  Linux LVM

Command (m for help): w
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.

3、為了能快速的加載新分區,我們使用partprobe命令手動更新

[root@aiker01 ~]# partprobe
[root@aiker01 ~]# ll /dev/sdb
sdb   sdb1  sdb2  sdb3  sdb4  sdb5  sdb6  sdb7  

4、創建物理卷PV

[root@aiker01 ~]# pvcreate /dev/sdb5 /dev/sdb6 /dev/sdb7
  Physical volume "/dev/sdb5" successfully created.
  Physical volume "/dev/sdb6" successfully created.
  Physical volume "/dev/sdb7" successfully created.

列出剛才創建的物理卷,新創建的物理卷都會帶“--- NEW Physical volume ---”

[root@aiker01 ~]# pvdisplay /dev/sdb[5-7]
  "/dev/sdb7" is a new physical volume of "2.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb7
  VG Name               
  PV Size               2.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               XegRtK-Fr4V-AfJ2-QVxu-hOo2-dGE8-HR2dox

  "/dev/sdb6" is a new physical volume of "2.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb6
  VG Name               
  PV Size               2.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               p6WjVw-X3th-BZVW-UfI9-nCIo-uLPg-uO5pUW

  "/dev/sdb5" is a new physical volume of "2.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb5
  VG Name               
  PV Size               2.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               KCPzgS-O43B-CFua-xSUn-fdCX-emSS-WHWQBG

pvs命令可以列出物理卷的使用列表,物理卷大小,使用情況,剩余的物理卷大小

[root@aiker01 ~]# pvs
  PV         VG  Fmt  Attr PSize   PFree
  /dev/sda3  cl  lvm2 a--  <19.51g 4.00m
  /dev/sdb1  vg1 lvm2 a--   <2.00g    0 
  /dev/sdb2  vg1 lvm2 a--   <2.00g    0 
  /dev/sdb3  vg1 lvm2 a--   <2.00g    0 
  /dev/sdb5      lvm2 ---    2.00g 2.00g
  /dev/sdb6      lvm2 ---    2.00g 2.00g
  /dev/sdb7      lvm2 ---    2.00g 2.00g

創建卷組VGS
先查看現有的卷組

[root@aiker01 ~]# vgs
  VG  #PV #LV #SN Attr   VSize   VFree
  cl    1   4   0 wz--n- <19.51g 4.00m
  vg1   3   1   0 wz--n-  <5.99g    0 

[root@aiker01 ~]# vgcreate vg2 /dev/sdb[5-6]
  Volume group "vg2" successfully created
[root@aiker01 ~]# vgdisplay vg2 #列出剛才創建的卷組詳情
   --- Volume group ---
  VG Name               vg2
  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               3.99 GiB
  PE Size               4.00 MiB
  Total PE              1022
  Alloc PE / Size       0 / 0   
  Free  PE / Size       1022 / 3.99 GiB
  VG UUID               0yIJqM-O6HU-c7yO-VeBZ-AWz3-WiJQ-cUSM1i

[root@aiker01 ~]# vgs
  VG  #PV #LV #SN Attr   VSize   VFree
  cl    1   4   0 wz--n- <19.51g 4.00m
  vg1   3   1   0 wz--n-  <5.99g    0 
  vg2   2   0   0 wz--n-   3.99g 3.99g

創建邏輯卷LVS

[```root@aiker01 ~]# lvcreate -l 1022 -n lv2 vg2 #-l 按pe,-L按size
Logical volume "lv2" created.

**格式化邏輯卷**

[root@aiker01 ~]# mkfs.ext4 /dev/vg2/lv2 
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
261632 inodes, 1046528 blocks
52326 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1071644672
32 block groups
32768 blocks per group, 32768 fragments per group
8176 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736

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

[root@aiker01 ~]# lvs
  LV   VG  Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home cl  -wi-ao----  4.00g                                                    
  root cl  -wi-ao----  8.57g                                                    
  swap cl  -wi-ao----  4.00g                                                    
  var  cl  -wi-ao---- <2.93g                                                    
  lv1  vg1 -wi-ao---- <5.99g                                                    
  lv2  vg2 -wi-a-----  3.99g                                                    
[root@aiker01 ~]# lsblk -f
NAME        FSTYPE      LABEL UUID                                   MOUNTPOINT
sda                                                                  
├─sda1                                                               
├─sda2      xfs               460aa503-2017-41c1-a406-c94f8b1097b7   /boot
└─sda3      LVM2_member       KzLxC5-B3g0-qvz5-4Bsf-Neaq-y3f1-cmRV0I 
  ├─cl-root xfs               16cc098f-10c5-4b7d-9a3a-cfd0289a0e12   /
  ├─cl-swap swap              542d997a-3a61-4e10-afe5-f4f96277eba9   [SWAP]
  ├─cl-var  xfs               2b8c9632-31ea-4299-9cb3-aa8d6d9c030e   /var
  └─cl-home xfs               b7a246f3-746e-49e4-bab0-4def83faaa26   /home
sdb                                                                  
├─sdb1      LVM2_member       osUKLK-uhqg-5owr-qicf-m5Rj-NnxA-7kZOdb 
│ └─vg1-lv1 ext4              22c8fa37-a406-4d65-b853-1bb361296a7f   /mnt
├─sdb2      LVM2_member       1lNxWq-1jKg-mRfu-NJpO-8996-d5Nc-lpGfwu 
│ └─vg1-lv1 ext4              22c8fa37-a406-4d65-b853-1bb361296a7f   /mnt
├─sdb3      LVM2_member       CsatVM-Qjr6-OdZi-Z150-ToEk-Fwty-dScaIs 
│ └─vg1-lv1 ext4              22c8fa37-a406-4d65-b853-1bb361296a7f   /mnt
├─sdb4                                                               
├─sdb5      LVM2_member       KCPzgS-O43B-CFua-xSUn-fdCX-emSS-WHWQBG 
│ └─vg2-lv2 ext4              75ba0d5a-f0e5-4182-a543-d24c83843b3d   
├─sdb6      LVM2_member       p6WjVw-X3th-BZVW-UfI9-nCIo-uLPg-uO5pUW 
│ └─vg2-lv2 ext4              75ba0d5a-f0e5-4182-a543-d24c83843b3d   
└─sdb7      LVM2_member       XegRtK-Fr4V-AfJ2-QVxu-hOo2-dGE8-HR2dox 
sr0                                                                  
[root@aiker01 ~]# mkdir /data **#創建目錄**
[root@aiker01 ~]# mount /dev/vg2/lv2 /data/** #掛載格式化後的邏輯卷**

[root@aiker01 ~]# df -hT** #查看文件系統的詳情**
Filesystem          Type      Size  Used Avail Use% Mounted on
/dev/mapper/cl-root xfs       8.6G  5.3G  3.4G  62% /
devtmpfs            devtmpfs  2.4G     0  2.4G   0% /dev
tmpfs               tmpfs     2.4G     0  2.4G   0% /dev/shm
tmpfs               tmpfs     2.4G  8.7M  2.4G   1% /run
tmpfs               tmpfs     2.4G     0  2.4G   0% /sys/fs/cgroup
/dev/mapper/cl-home xfs       4.0G   33M  4.0G   1% /home
/dev/mapper/cl-var  xfs       3.0G  159M  2.8G   6% /var
/dev/sda2           xfs       497M  132M  366M  27% /boot
/dev/mapper/vg1-lv1 ext4      5.9G   16M  5.6G   1% /mnt
tmpfs               tmpfs     479M     0  479M   0% /run/user/0
/dev/mapper/vg2-lv2 ext4      3.9G   16M  3.7G   1% /data

**聲明:xfs文件系統不支持lv縮容,這裏是ext4文件系統**

首先看下現在的文件系統掛載和使用情況
[root@aiker01 ~]# df -hT               
Filesystem          Type      Size  Used Avail Use% Mounted on
/dev/mapper/cl-root xfs       8.6G  5.3G  3.4G  62% /
devtmpfs            devtmpfs  2.4G     0  2.4G   0% /dev
tmpfs               tmpfs     2.4G     0  2.4G   0% /dev/shm
tmpfs               tmpfs     2.4G  8.7M  2.4G   1% /run
tmpfs               tmpfs     2.4G     0  2.4G   0% /sys/fs/cgroup
/dev/mapper/cl-home xfs       4.0G   33M  4.0G   1% /home
/dev/mapper/cl-var  xfs       3.0G  159M  2.8G   6% /var
/dev/sda2           xfs       497M  132M  366M  27% /boot
/dev/mapper/vg1-lv1 ext4      5.9G   16M  5.6G   1% /mnt
tmpfs               tmpfs     479M     0  479M   0% /run/user/0
/dev/mapper/vg2-lv2 ext4      3.9G   16M  3.7G   1% /data
[root@aiker01 ~]# ll /mnt/
total 36
-rw-r--r-- 1 root root     9 Dec  7 02:26 1.txt
-rw-r--r-- 1 root root     9 Dec  7 02:26 2.txt
drwxr-xr-x 2 root root  4096 Dec  7 02:34 6
drwxr-xr-x 2 root root  4096 Dec  7 02:34 7
drwxr-xr-x 2 root root  4096 Dec  7 02:34 8
drwx------ 2 root root 16384 Dec  6 03:02 lost+found
[root@aiker01 ~]# cat /mnt/1.txt 
12345678

卸載掉lv1卷

[root@aiker01 ~]# umount /mnt/

設置lv卷大小
下面的做法經過試驗是會報錯的:

[root@aiker01 ~]# lvresize -L 2G /dev/vg1/lv1 
  WARNING: Reducing active logical volume to 2.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg1/lv1? [y/n]: 
  WARNING: Invalid input ‘‘.
Do you really want to reduce vg1/lv1? [y/n]: y
  Size of logical volume vg1/lv1 changed from <5.99 GiB (1533 extents) to 2.00 GiB (512 extents).
  Logical volume vg1/lv1 successfully resized.
[root@aiker01 ~]# e2fsck -f !$
e2fsck -f /dev/vg1/lv1
e2fsck 1.42.9 (28-Dec-2013)
The filesystem size (according to the superblock) is 1569792 blocks
The physical size of the device is 524288 blocks
Either the superblock or the partition table is likely to be corrupt!
Abort<y>? y
[root@aiker01 ~]# e2fsck -f /dev/vg1/lv1
e2fsck 1.42.9 (28-Dec-2013)
The filesystem size (according to the superblock) is 1569792 blocks
The physical size of the device is 524288 blocks
Either the superblock or the partition table is likely to be corrupt!
Abort<y>? yes

正確的做法
剛才的錯誤可以修正回之前的大小:

[root@aiker01 ~]# lvresize -l 1533 /dev/vg1/lv1  
  Size of logical volume vg1/lv1 changed from <2.00 GiB (511 extents) to <5.99 GiB (1533 extents).
  Logical volume vg1/lv1 successfully resized.
[root@aiker01 ~]# resize2fs /dev/vg1/lv1 5.99G
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Invalid new size: 5.99G

[root@aiker01 ~]# 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: 16/392448 files (0.0% non-contiguous), 45115/1569792 blocks
[root@aiker01 ~]# resize2fs /dev/vg1/lv1 4G 
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg1/lv1 to 1048576 (4k) blocks.
The filesystem on /dev/vg1/lv1 is now 1048576 blocks long.

[root@aiker01 ~]# lvresize -L 4G /dev/vg1/lv1  #設置lv大小為4G
  WARNING: Reducing active logical volume to 4.00 GiB.
  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 <5.99 GiB (1533 extents) to 4.00 GiB (1024 extents).
  Logical volume vg1/lv1 successfully resized.

[root@aiker01 ~]# !e2  #檢查是lv有錯誤
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: 16/261632 files (0.0% non-contiguous), 36907/1048576 blocks
[root@aiker01 ~]# lvdisplay  /dev/vg1/lv1

--- Logical volume ---
  LV Path                /dev/vg1/lv1
  LV Name                lv1
  VG Name                vg1
  LV UUID                q6dAzJ-1dbC-2EoY-Fzcp-QqEE-enuU-FS5BLT
  LV Write Access        read/write
  LV Creation host, time aiker01, 2017-12-06 03:02:40 +0800
  LV Status              available
  #open                 0
  LV Size                4.00 GiB
  Current LE             1024
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:4

    [root@aiker01 ~]# vgdisplay  /dev/vg1
    --- Volume group ---
  VG Name               vg1
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  23
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               <5.99 GiB
  PE Size               4.00 MiB
  Total PE              1533
  Alloc PE / Size       1024 / 4.00 GiB
  Free  PE / Size       509 / <1.99 GiB
  VG UUID               dx3XD9-rQBV-QtBu-EebN-wgjI-CQcn-36iIbm

     [root@aiker01 ~]# mount /dev/vg1/lv1 /mnt/
[root@aiker01 ~]# ll /mnt/
total 36
-rw-r--r-- 1 root root     9 Dec  7 02:26 1.txt
-rw-r--r-- 1 root root     9 Dec  7 02:26 2.txt
drwxr-xr-x 2 root root  4096 Dec  7 02:34 6
drwxr-xr-x 2 root root  4096 Dec  7 02:34 7
drwxr-xr-x 2 root root  4096 Dec  7 02:34 8
drwx------ 2 root root 16384 Dec  6 03:02 lost+found
[root@aiker01 ~]# cat /mnt/1.txt 
12345678
[root@aiker01 ~]# df -hT
Filesystem          Type      Size  Used Avail Use% Mounted on
/dev/mapper/cl-root xfs       8.6G  5.3G  3.4G  62% /
devtmpfs            devtmpfs  2.4G     0  2.4G   0% /dev
tmpfs               tmpfs     2.4G     0  2.4G   0% /dev/shm
tmpfs               tmpfs     2.4G  8.7M  2.4G   1% /run
tmpfs               tmpfs     2.4G     0  2.4G   0% /sys/fs/cgroup
/dev/mapper/cl-home xfs       4.0G   33M  4.0G   1% /home
/dev/mapper/cl-var  xfs       3.0G  159M  2.8G   6% /var
/dev/sda2           xfs       497M  132M  366M  27% /boot
tmpfs               tmpfs     479M     0  479M   0% /run/user/0
/dev/mapper/vg2-lv2 ext4      3.9G   16M  3.7G   1% /data
/dev/mapper/vg1-lv1 ext4      3.9G   16M  3.7G   1% /mnt    

可以清楚的查看後lv1縮容後的大小,裏面的文件並沒有損壞。

擴容

**卸載掉lv1卷**

[root@aiker01 ~]# umount /mnt/ 

**設置卷的大小為4.5G**

[root@aiker01 ~]# lvresize -L 4.5G /dev/vg1/lv1   
  Size of logical volume vg1/lv1 changed from 1.00 GiB (256 extents) to 4.50 GiB (1152 extents).
  Logical volume vg1/lv1 successfully resized.

**檢查磁盤錯誤    **

[root@aiker01 ~]# 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: 16/261632 files (0.0% non-contiguous), 36907/1048576 blocks

**更新lv卷信息**

[root@aiker01 ~]# resize2fs !$
resize2fs /dev/vg1/lv1
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg1/lv1 to 1179648 (4k) blocks.
The filesystem on /dev/vg1/lv1 is now 1179648 blocks long.

**查看卷信息** 

[root@aiker01 ~]# lvdisplay !$
lvdisplay /dev/vg1/lv1
  --- Logical volume ---
  LV Path                /dev/vg1/lv1
  LV Name                lv1
  VG Name                vg1
  LV UUID                q6dAzJ-1dbC-2EoY-Fzcp-QqEE-enuU-FS5BLT
  LV Write Access        read/write
  LV Creation host, time aiker01, 2017-12-06 03:02:40 +0800
  LV Status              available
  # open                 0
  LV Size                4.50 GiB
  Current LE             1152
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:4

   **重新掛載**

[root@aiker01 ~]# mount !$ /mnt/
mount /dev/vg1/lv1 /mnt/

**查看分區掛載情況**

[root@aiker01 ~]# df -hT
Filesystem          Type      Size  Used Avail Use% Mounted on
/dev/mapper/cl-root xfs       8.6G  5.3G  3.4G  62% /
devtmpfs            devtmpfs  2.4G     0  2.4G   0% /dev
tmpfs               tmpfs     2.4G     0  2.4G   0% /dev/shm
tmpfs               tmpfs     2.4G  8.7M  2.4G   1% /run
tmpfs               tmpfs     2.4G     0  2.4G   0% /sys/fs/cgroup
/dev/mapper/cl-home xfs       4.0G   33M  4.0G   1% /home
/dev/mapper/cl-var  xfs       3.0G  159M  2.8G   6% /var
/dev/sda2           xfs       497M  132M  366M  27% /boot
tmpfs               tmpfs     479M     0  479M   0% /run/user/0
/dev/mapper/vg2-lv2 ext4      3.9G   16M  3.7G   1% /data
/dev/mapper/vg1-lv1 ext4      4.4G   16M  4.2G   1% /mnt

擴容成功完成

卷組增加容量和對lv在線擴容

增加磁盤(略)

快速分區

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

Command (m for help): n
All primary partitions are in use
Adding logical partition 8
First sector (25176064-41943039, default 25176064): 
Using default value 25176064
Last sector, +sectors or +size{K,M,G} (25176064-41943039, default 41943039): +2G
Partition 8 of type Linux and of size 2 GiB is set
Command (m for help): t
Partition number (1-8, default 8): 
Hex code (type L to list all codes): 8e
Changed type of partition ‘Linux‘ to ‘Linux LVM‘

Command (m for help): w
The partition table has been altered!

手動更新分區表

[root@aiker01 ~]# partprobe

列出分區

**[root@aiker01 ~]# parted -l

Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  2149MB  2147MB  primary                lvm
 2      2149MB  4296MB  2147MB  primary                lvm
 3      4296MB  6443MB  2147MB  primary                lvm
 4      6443MB  21.5GB  15.0GB  extended
 5      6445MB  8592MB  2147MB  logical                lvm
 6      8593MB  10.7GB  2147MB  logical                lvm
 7      10.7GB  12.9GB  2147MB  logical                lvm
 8      12.9GB  15.0GB  2147MB  logical                lvm

**

創建PV

[root@aiker01 ~]# pvcreate /dev/sdb8
  Physical volume "/dev/sdb8" successfully created.

    **vg擴展**

[root@aiker01 ~]# vgextend vg1 /dev/sdb8
  Volume group "vg1" successfully extended
[root@aiker01 ~]# vgdisplay vg1
  --- Volume group ---
  VG Name               vg1
  System ID             
  Format                lvm2
  Metadata Areas        4
  Metadata Sequence No  26
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                4
  Act PV                4
  VG Size               7.98 GiB
  PE Size               4.00 MiB
  Total PE              2044
  Alloc PE / Size       1152 / 4.50 GiB
  Free  PE / Size       892 / 3.48 GiB
  VG UUID               dx3XD9-rQBV-QtBu-EebN-wgjI-CQcn-36iIbm

lv擴容
這裏是比較簡單的擴容,直接把新分區給加了進去

[root@aiker01 ~]# lvextend /dev/vg1/lv1 /dev/sdb8
  Size of logical volume vg1/lv1 changed from 4.50 GiB (1152 extents) to <6.50 GiB (1663 extents).
  Logical volume vg1/lv1 successfully resized.
[root@aiker01 ~]# lvdisplay /dev/vg1/lv1
  --- Logical volume ---
  LV Path                /dev/vg1/lv1
  LV Name                lv1
  VG Name                vg1
  LV UUID                q6dAzJ-1dbC-2EoY-Fzcp-QqEE-enuU-FS5BLT
  LV Write Access        read/write
  LV Creation host, time aiker01, 2017-12-06 03:02:40 +0800
  LV Status              available
  # open                 1
  LV Size                <6.50 GiB
  Current LE             1663
  Segments               4
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:4  

更新lv掛載卷前

[root@aiker01 ~]# df -hT
Filesystem          Type      Size  Used Avail Use% Mounted on
/dev/mapper/cl-root xfs       8.6G  5.3G  3.4G  62% /
devtmpfs            devtmpfs  2.4G     0  2.4G   0% /dev
tmpfs               tmpfs     2.4G     0  2.4G   0% /dev/shm
tmpfs               tmpfs     2.4G  8.7M  2.4G   1% /run
tmpfs               tmpfs     2.4G     0  2.4G   0% /sys/fs/cgroup
/dev/mapper/cl-home xfs       4.0G   33M  4.0G   1% /home
/dev/mapper/cl-var  xfs       3.0G  159M  2.8G   6% /var
/dev/sda2           xfs       497M  132M  366M  27% /boot
tmpfs               tmpfs     479M     0  479M   0% /run/user/0
/dev/mapper/vg2-lv2 ext4      3.9G   16M  3.7G   1% /data
/dev/mapper/vg1-lv1 ext4      4.4G   16M  4.2G   1% /mnt

更新lv掛載信息(這步很重要)

[root@aiker01 ~]# resize2fs /dev/mapper/vg1-lv1  #xfs文件系統不適用,xfs文件系統需要執行xfs_growfs /dev/vg1/lv1  xfs
![](http://i2.51cto.com/images/blog/201712/08/f4977ad91fc0c475ced44d60e51dc25a.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mapper/vg1-lv1 is mounted on /mnt; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mapper/vg1-lv1 is now 1702912 blocks long.

更新後

[root@aiker01 ~]# df -hT
Filesystem          Type      Size  Used Avail Use% Mounted on
/dev/mapper/cl-root xfs       8.6G  5.3G  3.4G  62% /
devtmpfs            devtmpfs  2.4G     0  2.4G   0% /dev
tmpfs               tmpfs     2.4G     0  2.4G   0% /dev/shm
tmpfs               tmpfs     2.4G  8.7M  2.4G   1% /run
tmpfs               tmpfs     2.4G     0  2.4G   0% /sys/fs/cgroup
/dev/mapper/cl-home xfs       4.0G   33M  4.0G   1% /home
/dev/mapper/cl-var  xfs       3.0G  159M  2.8G   6% /var
/dev/sda2           xfs       497M  132M  366M  27% /boot
tmpfs               tmpfs     479M     0  479M   0% /run/user/0
/dev/mapper/vg2-lv2 ext4      3.9G   16M  3.7G   1% /data
/dev/mapper/vg1-lv1 ext4      6.4G   18M  6.1G   1% /mnt

[root@aiker01 ~]# ll /mnt/
total 36
-rw-r--r-- 1 root root     9 Dec  7 02:26 1.txt
-rw-r--r-- 1 root root     9 Dec  7 02:26 2.txt
drwxr-xr-x 2 root root  4096 Dec  7 02:34 6

[root@aiker01 ~]# cat /mnt/1.txt 
12345678

之前都是ext4文件系統的lvm操作,雖然xfs不支持縮容但是可以擴容,這裏是在線擴容

[root@aiker01 ~]# lvdisplay /dev/vg1/lv3
  --- Logical volume ---
  LV Path                /dev/vg1/lv3
  LV Name                lv3
  VG Name                vg1
  LV UUID                I4f9zy-OqLx-DRtM-LRs8-FCS8-Ia2R-BcsT4r
  LV Write Access        read/write
  LV Creation host, time aiker01, 2017-12-08 01:21:35 +0800
  LV Status              available
  # open                 0
  LV Size                700.00 MiB
  Current LE             175
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:6
[root@aiker01 ~]# df -hT
Filesystem          Type      Size  Used Avail Use% Mounted on
/dev/mapper/cl-root xfs       8.6G  5.3G  3.4G  62% /
devtmpfs            devtmpfs  2.4G     0  2.4G   0% /dev
tmpfs               tmpfs     2.4G     0  2.4G   0% /dev/shm
tmpfs               tmpfs     2.4G  8.7M  2.4G   1% /run
tmpfs               tmpfs     2.4G     0  2.4G   0% /sys/fs/cgroup
/dev/mapper/cl-home xfs       4.0G   33M  4.0G   1% /home
/dev/mapper/cl-var  xfs       3.0G  159M  2.8G   6% /var
/dev/sda2           xfs       497M  132M  366M  27% /boot
tmpfs               tmpfs     479M     0  479M   0% /run/user/0
/dev/mapper/vg2-lv2 ext4      3.9G   16M  3.7G   1% /data
/dev/mapper/vg1-lv1 ext4      6.4G   18M  6.1G   1% /mnt
/dev/mapper/vg1-lv3 xfs       697M   26M  672M   4% /temp

**重新設置lv大小**
[root@aiker01 ~]# lvresize -L 800M /dev/vg1/lv3
  Size of logical volume vg1/lv3 changed from 700.00 MiB (175 extents) to 800.00 MiB (200 extents).
  Logical volume vg1/lv3 successfully resized.

更新lv卷信息(這步很重要)

[root@aiker01 ~]# xfs_growfs /dev/mapper/vg1-lv3
meta-data=/dev/mapper/vg1-lv3    isize=512    agcount=6, agsize=32000 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=179200, 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 179200 to 204800

[root@aiker01 ~]# df -hT
Filesystem          Type      Size  Used Avail Use% Mounted on
/dev/mapper/cl-root xfs       8.6G  5.3G  3.4G  62% /
devtmpfs            devtmpfs  2.4G     0  2.4G   0% /dev
tmpfs               tmpfs     2.4G     0  2.4G   0% /dev/shm
tmpfs               tmpfs     2.4G  8.7M  2.4G   1% /run
tmpfs               tmpfs     2.4G     0  2.4G   0% /sys/fs/cgroup
/dev/mapper/cl-home xfs       4.0G   33M  4.0G   1% /home
/dev/mapper/cl-var  xfs       3.0G  159M  2.8G   6% /var
/dev/sda2           xfs       497M  132M  366M  27% /boot
tmpfs               tmpfs     479M     0  479M   0% /run/user/0
/dev/mapper/vg2-lv2 ext4      3.9G   16M  3.7G   1% /data
/dev/mapper/vg1-lv1 ext4      6.4G   18M  6.1G   1% /mnt
/dev/mapper/vg1-lv3 xfs       797M   26M  772M   4% /temp

4.13 磁盤故障小案例

1.新建分區的時候 w 保存出現警告。使用fdisk裏面的 p 還可以查看到已經分好的區,但是/dev/沒有該分區, 執行了partprobe會顯示出來,但是無法格式化。

技術分享圖片
原因:另外一塊磁盤不存在,但是配置文件/etc/fstab中又有掛載的配置,掛載出問題就導致這樣的故障。

2.重啟造成的開機錯誤。進入不了系統,

技術分享圖片
解決辦法:輸入root密碼,就可以進去了。
技術分享圖片

使用vi /etc/fstab命令查看原因

vim /etc/fstab

刪除最後一行,將光標移到最後一行,然後按dd,刪除整行。:wq保存。reboot重啟系統。

技術分享圖片

四周第一次課