Increase the size of a Linux LVM

背景

工作用的Linux 虚机使用的是默认安装, 根只分了50G的空间, 用着用着就不够了,所以想着给扩个空间。

[root@localhost repository]#      df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   50G   41G  9.4G  82% /
devtmpfs                  32G     0   32G   0% /dev
tmpfs                     32G     0   32G   0% /dev/shm
tmpfs                     32G  637M   31G   2% /run
tmpfs                     32G     0   32G   0% /sys/fs/cgroup
/dev/sda1               1014M  150M  865M  15% /boot
/dev/mapper/centos-home  441G   15G  427G   4% /home
overlay                   50G   41G  9.4G  82% /var/lib/docker/overlay2/398d2eeb78bef327ecaca5cd7de0087ce59f76a938e5ef5dbce91c2895a4b1aa/merged
overlay                   50G   41G  9.4G  82% /var/lib/docker/overlay2/0f05cdc7e42716c57997501e96310ca1769824b28ea91d2509502a35be589840/merged
overlay                   50G   41G  9.4G  82% /var/lib/docker/overlay2/5c10dbf435357bf50eecc148cb9a90d211384abca9fd9a5314de5fc62910c6f5/merged
overlay                   50G   41G  9.4G  82% /var/lib/docker/overlay2/a0fe47c9a9dca33b1fcfd35684aadc2037dc1398a23b2b09ebcad476636e0dab/merged
overlay                   50G   41G  9.4G  82% /var/lib/docker/overlay2/2eb88ff71135e7b9be851a22905aeded34038a1bdf06cf5d9c963a8378ecc923/merged
overlay                   50G   41G  9.4G  82% /var/lib/docker/overlay2/2013e4d4c128e0da65e969cfc432850615257874a81dd7bf73ade4bec75c4600/merged
overlay                   50G   41G  9.4G  82% /var/lib/docker/overlay2/912b49fbc5ead76d17e25059db4301cbcd0230e6be9063c5e17c6c9a1279da01/merged
tmpfs                    6.3G     0  6.3G   0% /run/user/0
overlay                   50G   41G  9.4G  82% /var/lib/docker/overlay2/11a70ceeddd9c3201fef095c4e65d61e24d1429a5c3e04b90310e1f3ebda6572/merged

Step1: 添加新硬盘,格式化分区

[root@localhost repository]# fdisk -l

Disk /dev/sda: 536.9 GB, 536870912000 bytes, 1048576000 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: 0x000cf381

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200  1048575999   523238400   8e  Linux LVM

Disk /dev/mapper/centos-root: 53.7 GB, 53687091200 bytes, 104857600 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 /dev/mapper/centos-swap: 8455 MB, 8455716864 bytes, 16515072 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 /dev/mapper/centos-home: 473.6 GB, 473645973504 bytes, 925089792 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 /dev/sdb: 536.9 GB, 536870912000 bytes, 1048576000 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 repository]# 
[root@localhost repository]# 
[root@localhost repository]# 
[root@localhost repository]#   
You have new mail in /var/spool/mail/root
[root@localhost repository]# 
[root@localhost repository]# 
[root@localhost repository]# 
[root@localhost repository]# lsblk 
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0   500G  0 disk 
├─sda1            8:1    0     1G  0 part /boot
└─sda2            8:2    0   499G  0 part 
  ├─centos-root 253:0    0    50G  0 lvm  /
  ├─centos-swap 253:1    0   7.9G  0 lvm  [SWAP]
  └─centos-home 253:2    0 441.1G  0 lvm  /home
sdb               8:16   0   500G  0 disk 
sr0              11:0    1  1024M  0 rom  
[root@localhost repository]# 
[root@localhost repository]# 
[root@localhost repository]# 
[root@localhost repository]# 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 0x64836409.

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

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost repository]# 

格式化分区

[root@localhost repository]# mkfs.ext4 /dev/sdb1 
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
32768000 inodes, 131071744 blocks
6553587 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2279604224
4000 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
	102400000

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

[root@localhost repository]# 

Step 2:调整逻辑卷大小

[root@localhost repository]# vgextend centos /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.
  Volume group "centos" successfully extended
[root@localhost repository]# 
[root@localhost repository]# 
[root@localhost repository]# pvs
  PV         VG     Fmt  Attr PSize    PFree   
  /dev/sda2  centos lvm2 a--  <499.00g    4.00m
  /dev/sdb1  centos lvm2 a--  <500.00g <500.00g
[root@localhost repository]# 
[root@localhost repository]# 
[root@localhost repository]# vgs 
  VG     #PV #LV #SN Attr   VSize   VFree  
  centos   2   3   0 wz--n- 998.99g 500.00g
[root@localhost ~]# lvs
  LV   VG     Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home centos -wi-ao---- <441.12g                                                    
  root centos -wi-ao----   50.00g                                                    
  swap centos -wi-ao----   <7.88g                                                    
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# lvextend -L 550G /dev/mapper/centos-root
  Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 550.00 GiB (140800 extents).
  Logical volume centos/root successfully resized.
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# pvs
  PV         VG     Fmt  Attr PSize    PFree
  /dev/sda2  centos lvm2 a--  <499.00g    0 
  /dev/sdb1  centos lvm2 a--  <500.00g    0 
[root@localhost ~]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=3276800 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=13107200, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=6400, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 13107200 to 144179200
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root  550G   41G  510G   8% /
devtmpfs                  32G     0   32G   0% /dev
tmpfs                     32G     0   32G   0% /dev/shm
tmpfs                     32G  637M   31G   2% /run
tmpfs                     32G     0   32G   0% /sys/fs/cgroup
/dev/sda1               1014M  150M  865M  15% /boot
/dev/mapper/centos-home  441G   15G  427G   4% /home

Tags: