Several Ways and Explanations of Online Capacity Expansion under Linux

Posted by sprint10s on Mon, 19 Aug 2019 08:42:23 +0200

The following are mainly aimed at on-line expansion of Linux in VM environment


In the production environment, the disk space is insufficient, but it is not convenient for downtime maintenance.
The following operations can be extended online in some cases without downtime.
There are two ways to expand the capacity from the space source: adding space to the original disk and adding new disk.
There are also two main types of disk partitions under Linux, LVM and EXT[3/4]
There are also two kinds of space destinations: expanding the original partition and adding partitions and mounting them.
But the overall process is basically the same, that is, adding space - > letting the kernel identify the refresh capacity - > partitioning the new space - > delivering the file system to use.

Adding disks to VM and delivering them to the system for use

Unlike Windows, new disks or additional capacity to the original disks under Linux are not automatically recognized by the kernel.

Add new disks to VM

A brief introduction to the process

Scanning SCSI Bus to Identify Newly Added Devices

echo "- - -"  > /sys/class/scsi_host/host0/scan

The host0 here refers to the first scsi host adapter, which corresponds to 0 in the device node SCSI(0:1) of the disk in ESXI.
The three "-" in the transmitted "-" denote scanning all channels (first-), every target (second-), and every device i.e. disk/lun (third-)

To identify new disk partitions

[root]# fdisk /dev/sdb
Command (m for help):n
Command action
	e 			extended
	p			primary partition (1 - 4 )
p
Partition number (1 - 4 ):1
First cylinder (1-261,default 1):
Using default value 1
Last cylinder or + size or +sizeM or +sizeK(1-261, default 261):
Using default value 261
Command (m for help): w
The partition table has been altered!
Calling ioctr() to re-read partition table.
Syncing disks

Preparing file systems for new partitions

[root]# msfs -t ext3 /dev/sdb1
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type:Linux
Block size=4096 (log=2)
Fragment size=4096(log=2)
...
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 23 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.

Create directories and mount new spaces

[root]#mkdir /tmp/dd
[root]#mount /dev/sdb1 /tmp/dd

Edit / etc/fstab for future automatic mounting

/dev/sdb1			/tmp/dd	ext3				defaults 		0 				0
 Device, mount point, file system, mount rule, dump order

Mounting rules:
auto boot-up automatic mounting
Default sets the mount definition by default for most permanent file systems
noauto boot does not automatically mount
Noser can only be mounted by super users
ro mounts with read-only permission
rw is mounted with readable and writable permissions
User can be mounted by any user
order refers to fsck (the sequence of fsck checks at startup). 0 means no checking, and (/) partitions are always
1. Other partitions can only start from 2. When the numbers are the same, they can be checked at the same time (but not two 1).

Expansion of existing disks in VM

Increase the size of existing disks in VM

A brief introduction to the process
Note: If there is a snapshot of this disk, it cannot be operated. The reason is obvious.

Rescan disks of varying sizes

[root@ksyoweb01 ~]# echo 1> /sys/class/block/sda/device/rescan  #kernel 3.x
[root@ksyoweb01 ~]#echo 1>/sys/class/scsi_device/0:0:1:0/device/block:sdb/device/rescan #kernel 2.x

Rezoning to use the new space

[root@ksyoweb01 ~]#  fdisk /dev/sdb
 Command (m for instructions):d
 Partition Number (1,2, default 2): 2
Partition 2 is deleted
 Command (m for instructions):n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
 Partition Number (2-4, default 2): 2
 Initial sector (1026048-92274687, preset 1026048):
Use default value 1026048
 Finally sector, +sectors or + size {K,M,G} (1026048-92274687, default 92274687):
Using default value 92274687
Partition 2 of type Linux and of size 43.5 GiB is set
 ** If allocated to LVM, you need to specify type**
Command (m for instructions):t
 Partition Number (1,2, default 2): 2
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Call ioctl() to re-read the split table.

WARNING: Re-reading the partition table failed with error 16: device or system resources are 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)
Synchronized disks.

Be careful:
Older versions of the core prompt for writing partition tables is The new table will be used at the next reboot.
This shows that the dream of online expansion is shattered and can only be restarted.

Notify system partition table changes partprobe:

[root@ksyoweb01 ~]# partprobe 
Error: /dev/sda Partition 2 above has been written, but we can't notify the kernel of the change, perhaps because of it./They are in use, resulting in the continued use of old partitions. You should reboot immediately before making further changes.

If the above prompt appears, it is unfortunate that it must be restarted.

Changing the size of the file system

root@test:~# resize2fs /dev/sdb2
resize2fs 1.42.9 (4-Feb-2014)
Filesystem at /dev/sdb2 is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 4
The filesystem on /dev/vda1 is now 12976128 blocks long.

Operation under LVM

[root@ksyoweb01 ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  **PV Size               39.51 GiB / not usable 2.00 MiB**
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              10114
  Free PE               11
  Allocated PE          10103
  PV UUID               JLiFFR-TWvr-E6uW-x77q-dZl1-MfrZ-T2PhUw
   
[root@ksyoweb01 ~]# pvresize /dev/sda2
  Physical volume "/dev/sda2" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized
[root@ksyoweb01 ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  **PV Size               43.51 GiB / not usable 2.00 MiB**
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              11138
  Free PE               1035
  Allocated PE          10103
  PV UUID               JLiFFR-TWvr-E6uW-x77q-dZl1-MfrZ-T2PhUw
[root@ksyoweb01 ~]# pvs
  PV         VG     Fmt  Attr PSize  PFree
  /dev/sda2  centos lvm2 a--  43.51g 4.04g
[root@ksyoweb01 ~]# vgs
  VG     #PV #LV #SN Attr   VSize  VFree
  centos   1   2   0 wz--n- 43.51g 4.04g
[root@ksyoweb01 ~]# lvs
  LV   VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- 35.59g                                                    
  swap centos -wi-ao----  3.88g                                                    
[root@ksyoweb01 ~]# lvextend -L +4G -f -r /dev/centos/root
  Size of logical volume centos/root changed from 35.59 GiB (9111 extents) to 39.59 GiB (10135 extents).
  Logical volume centos/root successfully resized.
meta-data=/dev/mapper/centos-root isize=256    agcount=4, agsize=2332416 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=9329664, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=4555, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 9329664 to 10378240 

So, obviously, if there may be future demand for online space expansion, please use LVM, otherwise, online space expansion is just a dream.

Topics: CentOS Linux Windows Fragment