首页
LVM 磁盘扩容
  1. 分区 (本实验环境使用的是物理扩容后的老磁盘/dev/vda)

    
    [root@wenda ~]# fdisk /dev/vda 											#使用fdisk命令来管理磁盘分区
    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.
    
    
    Command (m for help): p															#输入p来打印当前磁盘上的分区
    
    Disk /dev/vda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x0000ea5b
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/vda1   *        2048     2099199     1048576   83  Linux
    /dev/vda2         2099200    41943039    19921920   8e  Linux LVM
    
    Command (m for help): n															#输入n 新建分区
    Partition type:
       p   primary (2 primary, 0 extended, 2 free)
       e   extended
    Select (default p): 																#输入p 选择分区类型为主分区
    Using default response p
    Partition number (3,4, default 3): 									#输入3 选择为第三个主分区
    First sector (41943040-209715199, default 41943040): #直接回车 选择分区起始块
    Using default value 41943040
    Last sector, +sectors or +size{K,M,G} (41943040-209715199, default 209715199): #直接回车,使用所有剩余空间
    Using default value 209715199
    Partition 3 of type Linux and of size 80 GiB is set
    
    Command (m for help): p															#输入p来打印当前磁盘上的分区
    
    Disk /dev/vda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x0000ea5b
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/vda1   *        2048     2099199     1048576   83  Linux
    /dev/vda2         2099200    41943039    19921920   8e  Linux LVM
    /dev/vda3        41943040   209715199    83886080   83  Linux
    
    Command (m for help): t															#输入t 来改变分区类型
    Partition number (1-3, default 3): 									#输入3 来选择改变分区类型的分区号为3
    Hex code (type L to list all codes): L							#输入L 来查看所有分区类型代码
    
     0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
     1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
     2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
     3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
     4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
     5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
     6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
     7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
     8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
     9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
     a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
     b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
     c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs        
     e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
     f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
    10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
    11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
    12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
    14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary  
    16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS    
    17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
    18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
    1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep        
    1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT            
    1e  Hidden W95 FAT1 80  Old Minix      
    Hex code (type L to list all codes): 8e							#输入8e 改变分区类型为LVM
    Changed type of partition 'Linux' to 'Linux LVM'
    
    Command (m for help): p															#输入p来打印当前磁盘上的分区
    
    Disk /dev/vda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x0000ea5b
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/vda1   *        2048     2099199     1048576   83  Linux
    /dev/vda2         2099200    41943039    19921920   8e  Linux LVM
    /dev/vda3        41943040   209715199    83886080   8e  Linux LVM
    
    Command (m for help): w															#输入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.
    [root@wenda ~]# 
    

    新分区完毕后我们需要让内核重新载入,如果执行一次不能载入所有分区那么就多执行几次,直至全部能识别到,我们的vda上有3个分区,下面的命令已经显示全部识别了

    [root@wenda ~]# partx -a /dev/vda
    partx: /dev/vda: error adding partitions 1-2
    [root@wenda ~]# partx -a /dev/vda
    partx: /dev/vda: error adding partitions 1-3
    
  2. 将物理分区与硬盘创建为物理卷(pvcreate)

    [root@wenda ~]# pvs
      PV         VG Fmt  Attr PSize  PFree
      /dev/vda2  cl lvm2 a--  19.00g    0 
    [root@wenda ~]# pvcreate /dev/vda3	#将新硬盘/vda2加入物理卷上
      Physical volume "/dev/vda3" successfully created.
    [root@wenda ~]# pvs
      PV         VG Fmt  Attr PSize  PFree 
      /dev/vda2  cl lvm2 a--  19.00g     0 
      /dev/vda3     lvm2 ---  80.00g 80.00g
    
  3. 将物理卷(pv)扩容到vg中,要扩容的vg名为:cl

    [root@wenda ~]# vgs																		#查看卷组
      VG #PV #LV #SN Attr   VSize  VFree
      cl   1   2   0 wz--n- 19.00g    0 
    [root@wenda ~]# vgextend cl /dev/vda3									#扩展卷组
      Volume group "cl" successfully extended
    [root@wenda ~]# vgs	#查看卷组
      VG #PV #LV #SN Attr   VSize  VFree 
      cl   2   2   0 wz--n- 98.99g 80.00g									#从大小可以看出我们已经扩容成功
    [root@wenda ~]# 
    
  4. 扩展逻辑卷 (支持在线扩展)

    [root@wenda ~]# lvextend -l +100%FREE /dev/mapper/cl-root				#lvextend -L +2G /dev/mapper/cl-root
      Size of logical volume cl/root changed from 17.00 GiB (4351 extents) to 96.99 GiB (24830 extents).
      Logical volume cl/root successfully resized.
    [root@wenda ~]# lvs
      LV   VG Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
      root cl -wi-ao---- 96.99g                                                    
      swap cl -wi-ao----  2.00g                                                    
    [root@wenda ~]# 
    
  5. 扩容文件系统

    
    [root@wenda ~]# xfs_growfs /dev/mapper/cl-root	#由于是xfs的文件系统
    meta-data=/dev/mapper/cl-root    isize=512    agcount=4, agsize=1113856 blks
             =                       sectsz=512   attr=2, projid32bit=1
             =                       crc=1        finobt=0 spinodes=0
    data     =                       bsize=4096   blocks=4455424, imaxpct=25
             =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
    log      =internal               bsize=4096   blocks=2560, version=2
             =                       sectsz=512   sunit=0 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
    data blocks changed from 4455424 to 25425920
    [root@wenda ~]# df -ah
    Filesystem           Size  Used Avail Use% Mounted on
    rootfs                  -     -     -    - /
    sysfs                   0     0     0    - /sys
    proc                    0     0     0    - /proc
    devtmpfs             7.8G     0  7.8G   0% /dev
    securityfs              0     0     0    - /sys/kernel/security
    tmpfs                7.8G     0  7.8G   0% /dev/shm
    devpts                  0     0     0    - /dev/pts
    tmpfs                7.8G  8.6M  7.8G   1% /run
    tmpfs                7.8G     0  7.8G   0% /sys/fs/cgroup
    cgroup                  0     0     0    - /sys/fs/cgroup/systemd
    pstore                  0     0     0    - /sys/fs/pstore
    cgroup                  0     0     0    - /sys/fs/cgroup/net_cls,net_prio
    cgroup                  0     0     0    - /sys/fs/cgroup/devices
    cgroup                  0     0     0    - /sys/fs/cgroup/cpu,cpuacct
    cgroup                  0     0     0    - /sys/fs/cgroup/hugetlb
    cgroup                  0     0     0    - /sys/fs/cgroup/cpuset
    cgroup                  0     0     0    - /sys/fs/cgroup/pids
    cgroup                  0     0     0    - /sys/fs/cgroup/blkio
    cgroup                  0     0     0    - /sys/fs/cgroup/perf_event
    cgroup                  0     0     0    - /sys/fs/cgroup/freezer
    cgroup                  0     0     0    - /sys/fs/cgroup/memory
    configfs                0     0     0    - /sys/kernel/config
    /dev/mapper/cl-root   97G   11G   87G  11% /													#挂载的分区空间已经增加
    systemd-1               -     -     -    - /proc/sys/fs/binfmt_misc
    mqueue                  0     0     0    - /dev/mqueue
    hugetlbfs               0     0     0    - /dev/hugepages
    debugfs                 0     0     0    - /sys/kernel/debug
    /dev/vda1           1014M  175M  840M  18% /boot
    sunrpc                  0     0     0    - /var/lib/nfs/rpc_pipefs
    tmpfs                1.6G     0  1.6G   0% /run/user/0
    binfmt_misc             0     0     0    - /proc/sys/fs/binfmt_misc
    [root@wenda ~]#