龙空技术网

教你如何给Linux扩容

攻城狮华仔 93

前言:

目前同学们对“ubuntu扩展分区大小”可能比较关怀,大家都想要了解一些“ubuntu扩展分区大小”的相关内容。那么小编在网上网罗了一些对于“ubuntu扩展分区大小””的相关知识,希望朋友们能喜欢,大家快快来学习一下吧!

如果服务器的磁盘空间不够怎么办?没关系,我们可以使用LVM技术来对服务器进行扩容。

LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制,它由Heinz Mauelshagen在Linux 2.4内核上实现,目前最新版本为:稳定版1.0.5,开发版 1.1.0-rc2,以及LVM2开发版。LVM是在磁盘分区和文件系统之间添加的一个逻辑层,来为文件系统屏蔽下层磁盘分区布局,提供一个抽象的存储卷,在存储卷上建立文件系统。首先我们讨论以下几个LVM术语:

● 物理卷Physical Volume (PV):可以在 上面建立卷组的媒介,可以是硬盘分区,也可以是硬盘本身或者回环文件(loopbackfle) .物理卷包括一个特殊的 header,其余部分被切割为-块块物理区域 (physicalextents)

●卷组Volume group (VG):将-组物理卷收集为- 个管理单元

●逻辑卷Logical volume (LV):虚拟分区, 由物理区域(physical extents)组成

●物理区域Physical extent (PE):硬盘可供指派给逻辑卷的最小单位(通常为4MB)

LVM抽象模型

理解了以上概念后,我们来实际操作体验一下:

首先,在虚拟机上增加一块40G的磁盘,如下图:

增加一块40G的硬盘

然后,我们按以下操作步骤进行扩容

查看挂载点df -h

Filesystem Size Used Avail Use% Mounted on

udev 955M 0 955M 0% /dev

tmpfs 198M 904K 197M 1% /run

/dev/mapper/ubuntu--vg-ubuntu--lv 19G 3.8G 14G 22% /

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/sda2 976M 78M 832M 9% /boot

tmpfs 198M 0 198M 0% /run/user/1000

显示当前的Logical volume (LV): lvdisplay

root@ubuntu:~# lvdisplay  --- Logical volume ---  LV Path                /dev/ubuntu-vg/ubuntu-lv  LV Name                ubuntu-lv  VG Name                ubuntu-vg  LV UUID                qPS9DH-4ceg-o56l-qWdh-XhXI-mg2v-ZSC4R3  LV Write Access        read/write  LV Creation host, time ubuntu-server, 2020-11-26 10:45:50 +0800  LV Status              available  # open                 1  LV Size                <19.00 GiB  Current LE             4863  Segments               1  Allocation             inherit  Read ahead sectors     auto  - currently set to     256  Block device           253:0​
显示当前的Volume group (VG):vgdisplay
root@ubuntu:~# 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               <19.00 GiB  PE Size               4.00 MiB  Total PE              4863  Alloc PE / Size       4863 / <19.00 GiB  Free  PE / Size       0 / 0     VG UUID               1dYOYx-Z0T7-nJ0I-A8hV-OPwr-h5dj-Ep2le7​
显示当前的Physical Volume (PV):pvdisplayroot@ubuntu:~# pvdisplay

--- Physical volume ---

PV Name /dev/sda3

VG Name ubuntu-vg

PV Size <19.00 GiB / not usable 0

Allocatable yes (but full)

PE Size 4.00 MiB

Total PE 4863

Free PE 0

Allocated PE 4863

PV UUID y4Zxi2-al2M-6wOf-SrnQ-L1vT-fuls-HIRf2h

开始LVM扩容

查看fdiskfdisk -l

root@ubuntu:~# fdisk -l

Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 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: C57D49AC-9DB7-42B8-BE6A-7FA61C9C868E

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 41940991 39839744 19G Linux filesystem

Disk /dev/sdb: 40 GiB, 42949672960 bytes, 83886080 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: 19 GiB, 20396900352 bytes, 39837696 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

root@ubuntu:~#

​查看所有储存设备fdisk -l |grep '/dev'

root@ubuntu:~# fdisk -l |grep '/dev'

Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors

/dev/sda1 2048 4095 2048 1M BIOS boot

/dev/sda2 4096 2101247 2097152 1G Linux filesystem

/dev/sda3 2101248 41940991 39839744 19G Linux filesystem

Disk /dev/sdb: 40 GiB, 42949672960 bytes, 83886080 sectors

Disk /dev/mapper/ubuntu--vg-ubuntu--lv: 19 GiB, 20396900352 bytes, 39837696 sectors创建sdb分区fdisk /dev/sdb

root@ubuntu:~# 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 0x41160c4b.

Command (m for help):

Command (m for help): n

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):

First sector (2048-83886079, default 2048):

Last sector, +sectors or +size{K,M,G,T,P} (2048-83886079, default 83886079):

Created a new partition 1 of type 'Linux' and of size 40 GiB.

Command (m for help): w

The partition table has been altered.

Calling ioctl() to re-read partition table.

Syncing disks.

​格式化mkfs -t ext4 /dev/sdb1

root@ubuntu:~# mkfs -t ext4 /dev/sdb1

mke2fs 1.44.1 (24-Mar-2018)

Creating filesystem with 10485504 4k blocks and 2621440 inodes

Filesystem UUID: ab33e7a7-0348-48a4-9a96-a2c19876dd98

Superblock backups stored on blocks:

32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,

4096000, 7962624

Allocating group tables: done

Writing inode tables: done

Creating journal (65536 blocks): done

Writing superblocks and filesystem accounting information: done创建PVpvcreate /dev/sdb1

root@ubuntu:~# 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.

​查看卷组pvscan

root@ubuntu:~# pvscan

PV /dev/sda3 VG ubuntu-vg lvm2 [<19.00 GiB / 0 free]

PV /dev/sdb1 lvm2 [<40.00 GiB]

Total: 2 [<59.00 GiB] / in use: 1 [<19.00 GiB] / in no VG: 1 [<40.00 GiB]

​扩容VG查看VGvgdisplay

--- 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 <19.00 GiB

PE Size 4.00 MiB

Total PE 4863

Alloc PE / Size 4863 / <19.00 GiB

Free PE / Size 0 / 0

VG UUID 1dYOYx-Z0T7-nJ0I-A8hV-OPwr-h5dj-Ep2le7

​扩容VGvgextend ubuntu-vg /dev/sdb1

root@ubuntu:~# vgextend ubuntu-vg /dev/sdb1

Volume group "ubuntu-vg" successfully extended

root@ubuntu:~# pvscan

PV /dev/sda3 VG ubuntu-vg lvm2 [<19.00 GiB / 0 free]

PV /dev/sdb1 VG ubuntu-vg lvm2 [<40.00 GiB / <40.00 GiB free]

Total: 2 [58.99 GiB] / in use: 2 [58.99 GiB] / in no VG: 0 [0 ]

root@ubuntu:~# vgdisplay

--- Volume group ---

VG Name ubuntu-vg

System ID

Format lvm2

Metadata Areas 2

Metadata Sequence No 3

VG Access read/write

VG Status resizable

MAX LV 0

Cur LV 1

Open LV 1

Max PV 0

Cur PV 2

Act PV 2

VG Size 58.99 GiB

PE Size 4.00 MiB

Total PE 15102

Alloc PE / Size 4863 / <19.00 GiB

Free PE / Size 10239 / <40.00 GiB

VG UUID 1dYOYx-Z0T7-nJ0I-A8hV-OPwr-h5dj-Ep2le7

root@ubuntu:~# lvdisplay

--- Logical volume ---

LV Path /dev/ubuntu-vg/ubuntu-lv

LV Name ubuntu-lv

VG Name ubuntu-vg

LV UUID qPS9DH-4ceg-o56l-qWdh-XhXI-mg2v-ZSC4R3

LV Write Access read/write

LV Creation host, time ubuntu-server, 2020-11-26 10:45:50 +0800

LV Status available

# open 1

LV Size <19.00 GiB

Current LE 4863

Segments 1

Allocation inherit

Read ahead sectors auto

- currently set to 256

Block device 253:0

​扩容LV#按固定大小追加

lvextend -L +10G /dev/ubuntu-vg/ubuntu-lv

#按百分比追加

lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv

root@ubuntu:~# lvextend -L +10G /dev/ubuntu-vg/ubuntu-lv

Size of logical volume ubuntu-vg/ubuntu-lv changed from <19.00 GiB (4863 extents) to <29.00 GiB (7423 extents).

Logical volume ubuntu-vg/ubuntu-lv successfully resized.

root@ubuntu:~# 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 58.99 GiB (15102 extents).

Logical volume ubuntu-vg/ubuntu-lv successfully resized.刷新分区resize2fs /dev/ubuntu-vg/ubuntu-lv

root@ubuntu:~# 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 = 3, new_desc_blocks = 4

The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 7601152 (4k) blocks long.

​验证是否成功lvdisplay

root@ubuntu:~# lvdisplay

--- Logical volume ---

LV Path /dev/ubuntu-vg/ubuntu-lv

LV Name ubuntu-lv

VG Name ubuntu-vg

LV UUID qPS9DH-4ceg-o56l-qWdh-XhXI-mg2v-ZSC4R3

LV Write Access read/write

LV Creation host, time ubuntu-server, 2020-11-26 10:45:50 +0800

LV Status available

# open 1

LV Size 58.99 GiB

Current LE 15102

Segments 2

Allocation inherit

Read ahead sectors auto

- currently set to 256

Block device 253:0

root@ubuntu:~# vgdisplay

--- Volume group ---

VG Name ubuntu-vg

System ID

Format lvm2

Metadata Areas 2

Metadata Sequence No 5

VG Access read/write

VG Status resizable

MAX LV 0

Cur LV 1

Open LV 1

Max PV 0

Cur PV 2

Act PV 2

VG Size 58.99 GiB

PE Size 4.00 MiB

Total PE 15102

Alloc PE / Size 15102 / 58.99 GiB

Free PE / Size 0 / 0

VG UUID 1dYOYx-Z0T7-nJ0I-A8hV-OPwr-h5dj-Ep2le7

df -h

root@ubuntu:~# df -h

Filesystem Size Used Avail Use% Mounted on

udev 955M 0 955M 0% /dev

tmpfs 198M 908K 197M 1% /run

/dev/mapper/ubuntu--vg-ubuntu--lv 58G 3.9G 52G 7% /

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/sda2 976M 78M 832M 9% /boot

tmpfs 198M 0 198M 0% /run/user/0

最后,我们成功的扩展了40G磁盘空间。

标签: #ubuntu扩展分区大小