上一篇讲了软件安装与管理,这一篇实现扩容。
磁盘扩容
总会有一天你的硬盘容量会被耗尽,那么这个时候我们装系统的时候勾选LVM
技术就派上用处了。
我们的目标就是将30G的硬盘扩容为35G
# 查看挂载
df -h
# 输出
Filesystem Size Used Avail Use% Mounted on
udev 955M 0 955M 0% /dev
tmpfs 198M 1.2M 196M 1% /run
# 磁盘
/dev/mapper/ubuntu--vg-ubuntu--lv 29G 4.0G 23G 15% /
tmpfs 986M 0 986M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 986M 0 986M 0% /sys/fs/cgroup
/dev/loop0 90M 90M 0 100% /snap/core/8268
/dev/loop1 89M 89M 0 100% /snap/core/7270
/dev/sda2 976M 77M 832M 9% /boot
tmpfs 198M 0 198M 0% /run/user/0
我们先将虚拟机关机shutdown -h now
添加一块硬盘
我们扩充5个G
一直到结束
添加完成以后重新开机
首先查看lvdisplay
(当前逻辑卷)
--- Logical volume ---
LV Path /dev/ubuntu-vg/ubuntu-lv
LV Name ubuntu-lv # 逻辑卷的名字
VG Name ubuntu-vg # 所属卷组
LV UUID TeGmGX-TWK2-8pn2-raZr-pB4C-Q1XO-pTb3X9
LV Write Access read/write
LV Creation host, time ubuntu-server, 2020-02-06 07:53:08 +0000
LV Status available
# open 1
LV Size <29.00 GiB
Current LE 7423
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
查看当前卷组 vgdisplay
--- Volume group ---
VG Name ubuntu-vg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size <29.00 GiB
PE Size 4.00 MiB
Total PE 7423
Alloc PE / Size 7423 / <29.00 GiB
Free PE / Size 0 / 0
VG UUID isWhaA-qG9B-QeCa-0NCT-6gNf-vqbY-Lv8FjO
查看物理卷pvdisplay
--- Physical volume ---
PV Name /dev/sda3
VG Name ubuntu-vg
PV Size <29.00 GiB / not usable 0
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 7423
Free PE 0
Allocated PE 7423
PV UUID oe9BY7-7tTh-rdYY-iDyf-gtyf-G4Ar-IEMtgh
开始扩容
fdisk -l
# 输出
Disk /dev/loop0: 89.1 MiB, 93417472 bytes, 182456 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/loop1: 88.5 MiB, 92778496 bytes, 181208 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
# 第一块硬盘30G
Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 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
Disklabel type: gpt
Disk identifier: 58DC1F38-9F3D-4E44-88A5-D8FDB1D74E43
Device Start End Sectors Size Type
/dev/sda1 2048 4095 2048 1M BIOS boot
/dev/sda2 4096 2101247 2097152 1G Linux filesystem
/dev/sda3 2101248 62912511 60811264 29G Linux filesystem
# 第二块硬盘 5G
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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/ubuntu--vg-ubuntu--lv: 29 GiB, 31134318592 bytes, 60809216 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
然后我们需要创建分区fdisk /dev/sdb
fdisk /dev/sdb
# 输出
Welcome to fdisk (util-linux 2.31.1).
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.
Created a new DOS disklabel with disk identifier 0x7655aa1b.
# 这里需要输入 n
Command (m for help): n
# p 是主分区,e 是扩展分区
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
# 需要多少个分区
Partition number (1-4, default 1): 1
First sector (2048-10485759, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-10485759, default 10485759):
Created a new partition 1 of type 'Linux' and of size 5 GiB.
# 输入w写入
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
格式化
fdisk -l |grep '/dev'
# 输出
Disk /dev/loop0: 89.1 MiB, 93417472 bytes, 182456 sectors
Disk /dev/loop1: 88.5 MiB, 92778496 bytes, 181208 sectors
Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
/dev/sda1 2048 4095 2048 1M BIOS boot
/dev/sda2 4096 2101247 2097152 1G Linux filesystem
/dev/sda3 2101248 62912511 60811264 29G Linux filesystem
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 sectors
# 新分区 /dev/sdb1
/dev/sdb1 2048 10485759 10483712 5G 83 Linux
Disk /dev/mapper/ubuntu--vg-ubuntu--lv: 29 GiB, 31134318592 bytes, 60809216 sectors
# 格式化 使用ext4 文件系统
mkfs -t ext4 /dev/sdb1
# 输出
mke2fs 1.44.1 (24-Mar-2018)
Creating filesystem with 1310464 4k blocks and 327680 inodes
Filesystem UUID: d28b4f10-8ae7-488b-9baf-382b08428924
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
创建pv
物理卷
pvcreate /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.
扩容vg
卷组
vgextend ubuntu-vg /dev/sdb1
# 输出
Volume group "ubuntu-vg" successfully extended
扩容lv
逻辑卷
# /dev/ubuntu-vg/ubuntu-lv 在 lvdisplay 命令输出的 LV Path
lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
# 输出
Size of logical volume ubuntu-vg/ubuntu-lv changed from <29.00 GiB (7423 extents) to 33.99 GiB (8702 extents). # 已经变成33.99G了
Logical volume ubuntu-vg/ubuntu-lv successfully resized.
# 手动刷新一下
resize2fs /dev/ubuntu-vg/ubuntu-lv
# 输出
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 5
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 8910848 (4k) blocks long.
使用df -h
可以看到已经变成34G了。