当前位置:网站首页>如何扩展aws主机上的磁盘空间
如何扩展aws主机上的磁盘空间
2022-06-24 15:33:00 【独家雨天】
目的就是:在现有的EC2主机上磁盘空间不足,如何扩展aws主机上的磁盘空间?
Overview of Logic Volume Manager(LVM)
其中看到了LVM(逻辑卷组管理)。使用逻辑卷可以很方便的扩展分区的大小。

- Physical Volume(PV):This can be created on a whole physical disk (think /dev/sda) or a Linux partition.(物理卷–磁盘分区)
- Volume Group (VG): This is made up of at least one or more physical volumes.(逻辑卷组)
- 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.(逻辑卷)
- File System: A file system such as ext4 will be on the logical volume.(文件系统)
一块挂载的磁盘,应该就对应这一个 Physical Volume。
How to increase or expand LVM
首先我们从aws中的控制台准备好新的EBS卷(create Volume),并且挂载(attached)。然后进入终端。
[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 /
#########底下的为新增加的两块EBS卷#########
xvdf 202:80 0 2G 0 disk
xvdg 202:96 0 2G 0 disk
其次,我们使用 pvcreate 命令将物理硬盘(disk)初始化为物理卷(Physical Volume)。
[email protected]:~$ sudo pvcreate /dev/xvdf /dev/xvdg
Physical volume "/dev/xvdf" successfully created.
Physical volume "/dev/xvdg" successfully created.
使用sudo pvscan -n可以查看到可用的物理卷
[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]
再次使用 pvdisplay 可以看到,新增的两个“/dev/xvdf”和“/dev/xvdg” 都还不属于任何一个 Volume Group。(VG Name 为空)
[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 都是以pv开头,表示对Physical Volume 的操作
使用 vgdisplay 可以显示所有的Volume Group 组的信息。(由于没有新建任何的逻辑卷组,所以为空)
[email protected]:~$ sudo vgdisplay
[email protected]:~$
使用 vgcreate命令来创建Volume Group。比如新建一个名为 vgdata 的逻辑卷组,并且将物理卷/dev/xvdf添加到卷组中。
[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
如何我们想动态的添加物理卷到卷组中,可以使用命令vgextend。比如,我想把刚刚初始化好的物理卷/dev/xvdg也加入到刚新建的逻辑卷组(vgdata)中。
[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
可以看到VG Size 已经扩展了一半。但是现在整个路径
这样扩展最大的好处就是,在不中断应用程序对逻辑卷的访问。整个扩展过程,对应用程序来说,完全是透明的。
现在来到了logic Volume 的操作。需要在逻辑卷组的基础上生成逻辑卷。比如:
sudo lvcreate -n lvname1 -L 512M vgdata
sudo lvcreate -n lvname2 -l 100%FREE vgdata
其中:
-n表示显示的指定logic volume name;-L表示分配的大小,用M、G等度量单位;-l也表示分配的大小,但是按照百分比;vgdata表示所属的逻辑卷组
查看logic Volume 的概要信息(同理的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
创建了Logic Volume 之后,还需要对其指定File System。比如,指定为ext4的文件格式。
[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
减少或者增加logic Volume 的容量
sudo lvreduce -L -2.5G -r /dev/vgdata/lvname2
sudo lvextend -l +100%FREE -r /dev/vgdata/lvname1
执行结果
[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
首先得到每个Logic Volume 的UUID。使用blkid命令,(在这里我发现会做一个映射,将/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"
为每个Logic Volume 创建挂载点(我这里只挂载一个)
sudo mkdir /padata
插入对应的条目到/etc/fstab中。
LABEL=cloudimg-rootfs / ext4 defaults,discard 0 0
UUID=27ee34cc-d587-45a9-8e83-1c928ea8d532 /pgdata ext4 defaults 0 0
保存上面的文件,并且开始挂载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
删除一个逻辑卷之前,我们需要先确保删除Volume已经被Unmounted。然后使用 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
将删除掉的空间,加到逻辑卷 lvname1上。
[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.
参考资料:
边栏推荐
- Tencent cloud native intelligent data Lake Conference will be held, revealing the panoramic matrix of Tencent cloud data Lake products for the first time
- Cvpr2022 | domaine compact adaptable
- CIA security model - use PGP to describe privacy and integrity of network security CIA model
- Analysis of dompurify
- Is it safe to open a stock account by mobile phone
- US Senate promotes bipartisan gun safety bill
- The future of robots -- deep space exploration
- golang中Map的并发写入
- Attacked! Cloud development monitoring alarm practice
- How to modify the login user name of easynvr video monitoring system?
猜你喜欢

推荐几款超级实用的数据分析利器

为什么企业实施WMS仓储管理系统很容易失败

Stm32f1 and stm32cubeide programming examples -ws2812b full color LED driver (based on spi+dma)

高速公路服务区智能一体机解决方案

CVPR 2022 - Interpretation of selected papers of meituan technical team

Do you really know the difference between H5 and applet?
Oracle RAC configuration multipathing
![clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]](/img/f0/42f394dbc989d381387c7b953d2a39.jpg)
clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]

还在担心漏测吗?快来使用jacoco统计下代码覆盖率

Wide measuring range of jishili electrometer
随机推荐
Domestic payment system and payment background construction
SF express: please sign for MySQL soul ten
Differential privacy
practice
CVPR2022 | 可精简域适应
In 2021, big companies often ask IOS interview questions -- runloop
leetcode 139. Word break word split (medium)
【我的OpenGL学习进阶之旅】OpenGL的坐标系的学习笔记
Do you really know the difference between H5 and applet?
Special topic of IM code scanning login Technology (III): easy to understand. A detailed principle of IM code scanning login function is enough
CIA security model - use PGP to describe privacy and integrity of network security CIA model
VNC Viewer方式的远程连接树莓派
Sequential representation and implementation of linear table (refer to YanWeiMin version)
A series of problems caused by IPVS connection reuse in kubernetes
The security market has entered a trillion era, and the security B2B online mall system has been accurately connected to deepen the enterprise development path
运营商5G用户渗透远远比4G慢,5G的普及还得看中国广电
QoS Technology in network
中国十大证券app排名 炒股开户安全吗
Wi-Fi 7 来啦,它到底有多强?
This website teaches you to imitate more than 100 well-known websites!