在我们安装ubuntu时,如果选择的是自动分区,就会按照逻辑卷的形式来分区,并且只分配100G其余的并不会被分配
这对我们大多数情况来说都是不合理的,所以,如何扩充呢
下面以一个小的案例来说明如何扩充
问题
磁盘空间已满
Usage of /: 99.8% of 9.75GB
df、fdisk查看磁盘占用情况
df -h
Filesystem Size Used Avail Use% Mounted on
tmpfs 197M 1.4M 196M 1% /run
/dev/mapper/ubuntu–vg-ubuntu–lv 9.8G 9.3G 0 100% /
tmpfs 983M 0 983M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda2 1.7G 253M 1.4G 16% /boot
tmpfs 197M 4.0K 197M 1% /run/user/1000
fdisk -l
Disk /dev/sda: 25 GiB, 26843545600 bytes, 52428800 sectors
Disk model: Virtual disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Device Start End Sectors Size Type
/dev/sda1 2048 4095 2048 1M BIOS boot
/dev/sda2 4096 3674111 3670016 1.8G Linux filesystem
/dev/sda3 3674112 33552383 29878272 14.2G Linux filesystem
查看磁盘挂载情况
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 25G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 1.8G 0 part /boot
└─sda3 8:3 0 14.2G 0 part
└─ubuntu–vg-ubuntu–lv 253:0 0 9.8G 0 lvm /
可以看到sda3有15G,但是只分配了10G。
命令vgdisplay可以查看lvm卷组的信息;
vgdisplay
如果看到 Free PE / Size > 0,表示还有扩容空间。
解决
这里分两种情况进行处理:
LV(Logical Volume) 没有用尽 VG(Volume Group)。
VG(Volume Group) 没有用尽 PV(Physical Volume),或者 PV(Physical Volume) 没有关联物理分区。
LV 使用全部 VG
使用lvresize扩容剩下所有空间。
#lvextend -L 10G /dev/mapper/ubuntu–vg-ubuntu–lv //增大或减小至19G
#lvextend -L +10G /dev/mapper/ubuntu–vg-ubuntu–lv //增加10G
#lvreduce -L -10G /dev/mapper/ubuntu–vg-ubuntu–lv //减小10G
#lvresize -l +100%FREE /dev/mapper/ubuntu–vg-ubuntu–lv //按百分比扩
lvresize -l +100%FREE /dev/mapper/ubuntu–vg-ubuntu–lv
执行resize2fs使扩容生效 【注意:不执行这个命令以上修改是不会生效的!】
resize2fs /dev/mapper/ubuntu–vg-ubuntu–lv
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/mapper/ubuntu–vg-ubuntu–lv is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 2
The filesystem on /dev/mapper/ubuntu–vg-ubuntu–lv is now 3734528 (4k) blocks long.
查看结果
vgdisplay
— Volume group —
VG Name ubuntu-vg
System ID
Format lvm2
VG Size <14.25 GiB
PE Size 4.00 MiB
Total PE 3647
Alloc PE / Size 3647 / <14.25 GiB
Free PE / Size 0 / 0
VG 关联物理分区进行扩容
补充:本系统使用的是虚拟机,所以需要在虚拟机设置中先扩容“物理”硬盘空间。
接下来采用的方法是:不修改原有分区,在新的空间上新建分区,并添加到LVM的LV中。
创建分区 sda4,并设置为 lvm 类型:
修改前分区状态
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 25G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 1.8G 0 part /boot
└─sda3 8:3 0 14.2G 0 part
└─ubuntu–vg-ubuntu–lv 253:0 0 14.2G 0 lvm /
开始分区
$ sudo fdisk /dev/sda
Command (m for help): m #查看帮助
Help:
GPT
M enter protective/hybrid MBR
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition
Misc
m print this menu
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
Command (m for help): p #打印分区
Disk /dev/sda: 25 GiB, 26843545600 bytes, 52428800 sectors
Disk model: Virtual disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Device Start End Sectors Size Type
/dev/sda1 2048 4095 2048 1M BIOS boot
/dev/sda2 4096 3674111 3670016 1.8G Linux filesystem
/dev/sda3 3674112 33552383 29878272 14.2G Linux filesystem
Command (m for help): n # 新建分区
Partition number (4-128, default 4): # 使用默认的序号4
First sector (33552384-52428766, default 33552384): #使用默认的开始
Last sector, +/-sectors or +/-size{K,M,G,T,P} (33552384-52428766, default 52428766): #使用默认的结束 也就也就意味着使用剩下的全部
Created a new partition 4 of type ‘Linux filesystem’ and of size 9 GiB.
Command (m for help): p # 打印分区
Disk /dev/sda: 25 GiB, 26843545600 bytes, 52428800 sectors
Disk model: Virtual disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Device Start End Sectors Size Type
/dev/sda1 2048 4095 2048 1M BIOS boot
/dev/sda2 4096 3674111 3670016 1.8G Linux filesystem
/dev/sda3 3674112 33552383 29878272 14.2G Linux filesystem
/dev/sda4 33552384 52428766 18876383 9G Linux filesystem
Command (m for help): t #改变分区类型
Partition number (1-4, default 4): #默认选择刚刚的4
Partition type or alias (type L to list all): lvm # 改编成lvm
Changed type of partition ‘Linux filesystem’ to ‘Linux LVM’.
Command (m for help): p
Disk /dev/sda: 25 GiB, 26843545600 bytes, 52428800 sectors
Disk model: Virtual disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Device Start End Sectors Size Type
/dev/sda1 2048 4095 2048 1M BIOS boot
/dev/sda2 4096 3674111 3670016 1.8G Linux filesystem
/dev/sda3 3674112 33552383 29878272 14.2G Linux filesystem
/dev/sda4 33552384 52428766 18876383 9G Linux LVM
Command (m for help): w # 写入保存并退出
The partition table has been altered.
Syncing disks.
建物理分区并关联到LVM
$ sudo lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
…
sda 8:0 0 25G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 1.8G 0 part /boot
├─sda3 8:3 0 14.2G 0 part
│ └─ubuntu–vg-ubuntu–lv 253:0 0 14.2G 0 lvm /
└─sda4 8:4 0 9G 0 part
创建物理卷PV
$ sudo pvcreate /dev/sda4
Physical volume “/dev/sda4” successfully created.
查看物理卷PV
$ sudo pvdisplay
— Physical volume —
PV Name /dev/sda3
VG Name ubuntu-vg
PV Size <14.25 GiB / not usable 0
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 3647
Free PE 0
Allocated PE 3647
“/dev/sda4” is a new physical volume of “9.00 GiB”
— NEW Physical volume —
PV Name /dev/sda4
VG Name
PV Size 9.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
查看卷组VG
$ sudo vgdisplay
— Volume group —
VG Name ubuntu-vg
System ID
Format lvm2
VG Size <14.25 GiB
PE Size 4.00 MiB
Total PE 3647
Alloc PE / Size 3647 / <14.25 GiB
Free PE / Size 0 / 0
扩容卷组VG
$ sudo vgextend ubuntu-vg /dev/sda4
Volume group “ubuntu-vg” successfully extended
查看卷组VG
$ sudo vgdisplay
— Volume group —
VG Name ubuntu-vg
System ID
Format lvm2
VG Size 23.24 GiB
PE Size 4.00 MiB
Total PE 5950
Alloc PE / Size 3647 / <14.25 GiB
Free PE / Size 2303 / <9.00 GiB
扩容逻辑卷LV
$ sudo lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
Size of logical volume ubuntu-vg/ubuntu-lv changed from <14.25 GiB (3647 extents) to 23.24 GiB (5950 extents).
Logical volume ubuntu-vg/ubuntu-lv successfully resized.
查看卷组VG
$ sudo vgdisplay
— Volume group —
VG Name ubuntu-vg
System ID
Format lvm2
VG Size 23.24 GiB
PE Size 4.00 MiB
Total PE 5950
Alloc PE / Size 5950 / 23.24 GiB
Free PE / Size 0 / 0
执行修改
$ sudo resize2fs /dev/ubuntu-vg/ubuntu-lv
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 6092800 (4k) blocks long.
查看结果
$ df -h
Filesystem Size Used Avail Use% Mounted on
tmpfs 197M 1.6M 195M 1% /run
可以看到/已经变成了扩容后的容量
/dev/mapper/ubuntu–vg-ubuntu–lv 23G 9.2G 13G 43% /
tmpfs 983M 0 983M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda2 1.7G 253M 1.4G 16% /boot
tmpfs 197M 4.0K 197M 1% /run/user/1000
$ sudo lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 25G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 1.8G 0 part /boot
├─sda3 8:3 0 14.2G 0 part
│ └─ubuntu–vg-ubuntu–lv 253:0 0 23.2G 0 lvm /
└─sda4 8:4 0 9G 0 part
└─ubuntu–vg-ubuntu–lv 253:0 0 23.2G 0 lvm /
完成