四周第一次课

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了四周第一次课相关的知识,希望对你有一定的参考价值。

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命令手动更新

[[email protected] ~]# partprobe
[[email protected] ~]# ll /dev/sdb
sdb   sdb1  sdb2  sdb3  sdb4  sdb5  sdb6  sdb7  

4、创建物理卷PV

[[email protected] ~]# 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 ---”

[[email protected] ~]# 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命令可以列出物理卷的使用列表,物理卷大小,使用情况,剩余的物理卷大小

[[email protected] ~]# 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
先查看现有的卷组

[[email protected] ~]# 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 

[[email protected] ~]# vgcreate vg2 /dev/sdb[5-6]
  Volume group "vg2" successfully created
[[email protected] ~]# 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

[[email protected] ~]# 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

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

**格式化逻辑卷**

[[email protected] ~]# 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 

[[email protected] ~]# 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                                                    
[[email protected] ~]# 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                                                                  
[[email protected] ~]# mkdir /data **#创建目录**
[[email protected] ~]# mount /dev/vg2/lv2 /data/** #挂载格式化后的逻辑卷**

[[email protected] ~]# 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文件系统**

首先看下现在的文件系统挂载和使用情况
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# cat /mnt/1.txt 
12345678

卸载掉lv1卷

[[email protected] ~]# umount /mnt/

设置lv卷大小
下面的做法经过试验是会报错的:

[[email protected] ~]# 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.
[[email protected] ~]# 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
[[email protected] ~]# 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

正确的做法
刚才的错误可以修正回之前的大小:

[[email protected] ~]# 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.
[[email protected] ~]# resize2fs /dev/vg1/lv1 5.99G
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Invalid new size: 5.99G

[[email protected] ~]# 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
[[email protected] ~]# 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.

[[email protected] ~]# 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.

[[email protected] ~]# !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
[[email protected] ~]# 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

    [[email protected] ~]# 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

     [[email protected] ~]# mount /dev/vg1/lv1 /mnt/
[[email protected] ~]# 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
[[email protected] ~]# cat /mnt/1.txt 
12345678
[[email protected] ~]# 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卷**

[[email protected] ~]# umount /mnt/ 

**设置卷的大小为4.5G**

[[email protected] ~]# 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.

**检查磁盘错误    **

[[email protected] ~]# 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卷信息**

[[email protected] ~]# 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.

**查看卷信息** 

[[email protected] ~]# 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

   **重新挂载**

[[email protected] ~]# mount !$ /mnt/
mount /dev/vg1/lv1 /mnt/

**查看分区挂载情况**

[[email protected] ~]# 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在线扩容

增加磁盘(略)

快速分区

[[email protected] ~]# 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!

手动更新分区表

[[email protected] ~]# partprobe

列出分区

**[[email protected] ~]# 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

[[email protected] ~]# pvcreate /dev/sdb8
  Physical volume "/dev/sdb8" successfully created.

    **vg扩展**

[[email protected] ~]# vgextend vg1 /dev/sdb8
  Volume group "vg1" successfully extended
[[email protected] ~]# 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扩容
这里是比较简单的扩容,直接把新分区给加了进去

[[email protected] ~]# 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.
[[email protected] ~]# 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挂载卷前

[[email protected] ~]# 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挂载信息(这步很重要)

[[email protected] ~]# 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.

更新后

[[email protected] ~]# 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

[[email protected] ~]# 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

[[email protected] ~]# cat /mnt/1.txt 
12345678

之前都是ext4文件系统的lvm操作,虽然xfs不支持缩容但是可以扩容,这里是在线扩容

[[email protected] ~]# 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
[[email protected] ~]# 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大小**
[[email protected] ~]# 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卷信息(这步很重要)

[[email protected] ~]# 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

[[email protected] ~]# 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重启系统。

技术分享图片

以上是关于四周第一次课的主要内容,如果未能解决你的问题,请参考以下文章

四周第一次课(1月2日)

20165305 苏振龙《Java程序设计》第四周课上测试补做

2018.4.16 四周第一次课

四周第一次课

四周第一次课(2月26日)

四周第一次课(2月26日)