当前位置:网站首页>How to expand disk space on AWS host

How to expand disk space on AWS host

2022-06-24 15:39:00 Exclusive rainy days

The purpose is to : In the existing EC2 Insufficient disk space on the host , how Expand aws Disk space on the host

Overview of Logic Volume Manager(LVM)

And I saw LVM( Logical volume group management ). Using logical volumes can easily expand the size of partitions .

LVM Hierarchical structure of

  • Physical Volume(PV):This can be created on a whole physical disk (think /dev/sda) or a Linux partition.( Physical volume – Disk partition )
  • Volume Group (VG): This is made up of at least one or more physical volumes.( Logical volume group )
  • Logical Volume (LV): This is sometimes referred to as the partition, it sits within a volume group and has a file system written to it.( Logic volume )
  • File System: A file system such as ext4 will be on the logical volume.( file system )

A mounted disk , It should correspond to this one Physical Volume.

How to increase or expand LVM

First we start with aws The console in is ready for the new EBS volume (create Volume), And mount (attached). And then go to the terminal .

[email protected]:~$ lsblk
NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0     7:0    0   91M  1 loop /snap/core/6350
loop1     7:1    0   18M  1 loop /snap/amazon-ssm-agent/930
loop2     7:2    0 89.3M  1 loop /snap/core/6673
loop3     7:3    0 17.9M  1 loop /snap/amazon-ssm-agent/1068
loop4     7:4    0 89.4M  1 loop /snap/core/6818
xvda    202:0    0    8G  0 disk
└─xvda1 202:1    0    8G  0 part /
######### The bottom one is the newly added two pieces EBS volume #########
xvdf    202:80   0    2G  0 disk
xvdg    202:96   0    2G  0 disk

secondly , We use pvcreate The order will Physical hard disk (disk) Initialize to Physical volume (Physical Volume).

[email protected]:~$ sudo  pvcreate /dev/xvdf /dev/xvdg
  Physical volume "/dev/xvdf" successfully created.
  Physical volume "/dev/xvdg" successfully created.

Use sudo pvscan -n Available physical volumes can be viewed

[email protected]:~$ sudo pvscan -n
  WARNING: only considering physical volumes in no volume group
  PV /dev/xvdf                      lvm2 [2.00 GiB]
  PV /dev/xvdg                      lvm2 [2.00 GiB]

Again using pvdisplay You can see , Two new ones “/dev/xvdf” and “/dev/xvdg” It doesn't belong to any one yet Volume Group.(VG Name It's empty )

[email protected]:~$ sudo pvdisplay
  "/dev/xvdf" is a new physical volume of "2.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/xvdf
  VG Name
  PV Size               2.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               Cxcn22-4gxj-eJP1-E94R-MiPs-Rrm0-a4leFY

  "/dev/xvdg" is a new physical volume of "2.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/xvdg
  VG Name
  PV Size               2.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               giplCK-pQjh-cN66-d8Vr-J2lN-dDyp-cHdCQa

pvscan、pvcreate、pvdisplay It's all about pv start , Said to Physical Volume The operation of

Use vgdisplay Can show all of Volume Group Group information .( Since no new logical volume group has been created , So it's empty )

[email protected]:~$ sudo vgdisplay
[email protected]:~$

Use vgcreate Command to create Volume Group. For example, create a new one named vgdata Logical volume group of , And the physical volume /dev/xvdf Add to volume group .

[email protected]:~$ sudo vgcreate vgdata /dev/xvdf
  Volume group "vgdata" successfully created
[email protected]:~$ sudo vgdisplay
  --- Volume group ---
  VG Name               vgdata
  System ID
  Format                lvm2
  Metadata Areas        1
  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                1
  Act PV                1
  VG Size               <2.00 GiB
  PE Size               4.00 MiB
  Total PE              511
  Alloc PE / Size       0 / 0
  Free  PE / Size       511 / <2.00 GiB
  VG UUID               gW1oil-0JwD-YD16-BO8m-oPo5-1Fe3-tLP2rr

How do we want to dynamically add physical volumes to volume groups , You can use commands vgextend. such as , I want to initialize the newly initialized physical volume /dev/xvdg Also join the newly created logical volume group (vgdata) in .

[email protected]:~$ sudo vgextend vgdata /dev/xvdg
  Volume group "vgdata" successfully extended
[email protected]:~$ sudo vgdisplay
  --- Volume group ---
  VG Name               vgdata
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  2
  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               gW1oil-0JwD-YD16-BO8m-oPo5-1Fe3-tLP2rr

You can see VG Size It has been expanded by half . But now the whole path

The biggest benefit of this expansion is , Without interrupting application access to logical volumes . The whole extension process , For applications , It's completely transparent .

Now comes logic Volume The operation of . You need to generate logical volumes based on logical volume groups . such as :

sudo lvcreate -n lvname1 -L 512M vgdata
sudo lvcreate -n lvname2 -l 100%FREE vgdata

among :

  • -n Indicates the specified... Of the display logic volume name;
  • -L Indicates the allocated size , use M、G Equivalent unit of measure ;
  • -l It also indicates the allocated size , But as a percentage ;
  • vgdata Indicates the logical volume group to which it belongs

see logic Volume Summary information for ( In the same way pvs、gvs、lvs)

[email protected]:~$ sudo lvs
  LV      VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lvname1 vgdata -wi-a----- 512.00m
  lvname2 vgdata -wi-a-----   3.49g

Created Logic Volume after , You also need to specify File System. such as , Designated as ext4 File format .

[email protected]:~$ sudo mkfs.ext4 /dev/vgdata/lvname1
mke2fs 1.44.1 (24-Mar-2018)
Creating filesystem with 131072 4k blocks and 32768 inodes
Filesystem UUID: 27ee34cc-d587-45a9-8e83-1c928ea8d532
Superblock backups stored on blocks:
	32768, 98304

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

Resizing Logical Volumes and Extending Volume Groups

Reduce or increase logic Volume The capacity of

sudo lvreduce -L -2.5G -r /dev/vgdata/lvname2
sudo lvextend -l +100%FREE -r /dev/vgdata/lvname1

Execution results

[email protected]:~$ sudo lvreduce -L -2.5G -r /dev/vgdata/lvname2
fsck from util-linux 2.31.1
/dev/mapper/vgdata-lvname2: clean, 11/228928 files, 34338/915456 blocks
resize2fs 1.44.1 (24-Mar-2018)
Resizing the filesystem on /dev/mapper/vgdata-lvname2 to 260096 (4k) blocks.
The filesystem on /dev/mapper/vgdata-lvname2 is now 260096 (4k) blocks long.

  Size of logical volume vgdata/lvname2 changed from 3.49 GiB (894 extents) to 1016.00 MiB (254 extents).
  Logical volume vgdata/lvname2 successfully resized.
[email protected]:~$ sudo lvextend -l +100%FREE -r /dev/vgdata/lvname1
fsck from util-linux 2.31.1
/dev/mapper/vgdata-lvname1: clean, 11/32768 files, 6353/131072 blocks
  Size of logical volume vgdata/lvname1 changed from 512.00 MiB (128 extents) to 3.00 GiB (768 extents).
  Logical volume vgdata/lvname1 successfully resized.
resize2fs 1.44.1 (24-Mar-2018)
Resizing the filesystem on /dev/mapper/vgdata-lvname1 to 786432 (4k) blocks.
The filesystem on /dev/mapper/vgdata-lvname1 is now 786432 (4k) blocks long.

Mounting Logical Volumes on Boot and on Demand

First get each Logic Volume Of UUID. Use blkid command ,( Here I found that I would do a mapping , take /dev/vgdata/lvname1 → /dev/mapper/vgdata-lvname1)

[email protected]:~$ sudo blkid /dev/vgdata/lvname1
/dev/vgdata/lvname1: UUID="27ee34cc-d587-45a9-8e83-1c928ea8d532" TYPE="ext4"
[email protected]:~$ sudo blkid /dev/vgdata/lvname2
/dev/vgdata/lvname2: UUID="0dbc839f-a67a-4861-afe9-37b2e627f70e" TYPE="ext4"

For each Logic Volume Create mount point ( I only have one attached here )

sudo mkdir /padata

Insert the corresponding entry into /etc/fstab in .

LABEL=cloudimg-rootfs   /        ext4   defaults,discard        0 0
UUID=27ee34cc-d587-45a9-8e83-1c928ea8d532       /pgdata  ext4   defaults        0 0

Save the file above , And start mounting LV

[email protected]:~$ sudo mount -a
[email protected]:~$ sudo mount | grep pgdata
/dev/mapper/vgdata-lvname1 on /pgdata type ext4 (rw,relatime,data=ordered)

Deleting a Logic Volume

Before deleting a logical volume , We need to make sure we delete Volume Has been Unmounted. And then use lvremove.

[email protected]:~$ sudo lvremove /dev/vgdata/lvname2
Do you really want to remove and DISCARD active logical volume vgdata/lvname2? [y/n]: y
  Logical volume "lvname2" successfully removed

Space to be deleted , Add to logical volume lvname1 On .

[email protected]:~$ sudo lvextend -l+100%FREE /dev/vgdata/lvname1
  Size of logical volume vgdata/lvname1 changed from 3.00 GiB (768 extents) to 3.99 GiB (1022 extents).
  Logical volume vgdata/lvname1 successfully resized.

Reference material :

原网站

版权声明
本文为[Exclusive rainy days]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241321403896.html