如何在扩展分区内调整 LVM 分区的大小

在 Linux 中调整逻辑卷的大小并不是很困难,可以通过非常简单的方法来实现。 以下是通常的步骤:

  1. 在硬盘上创建一个新分区。
  2. 将刚刚创建的分区添加为物理卷。
  3. 将新的物理卷添加到卷组。
  4. 将卷组中的空间分配给逻辑卷。
  5. 调整文件系统的大小。

但在这种情况下,您拥有根文件系统(作为 LVM 分区)安装在扩展分区下,而不是主分区内。 您只有一个安装在 /boot 上的主分区,其余所有空间都是该扩展分区的一部分。

听起来很麻烦? 让我向您展示如何在扩展分区内调整 LVM 的大小。

在扩展分区内调整 LVM 分区大小

在本教程中,我使用的是安装在虚拟机中的 Linux。

请记住,在处理磁盘分区时应该非常小心。

第 1 步:关闭 VM 并增加磁盘大小

首先,关闭您的虚拟机并增加磁盘大小。 在这里,我将磁盘 /dev/sda 的大小增加了 20 GB 到大约 40 GB。 然后启动您的虚拟机并转到控制台。

看看我们的磁盘分区。

[email protected]:~# fdisk -l

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders, total 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 identifier: 0x000a975f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      499711      248832   83  Linux
/dev/sda2          501758    41940991    20719617    5  Extended
/dev/sda5          501760    41940991    20719616   8e  Linux LVM

如果您使用 df 命令分析磁盘空间,这就是它为我显示的内容:

[email protected]:~# df -hT
Filesystem     Type      Size  Used Avail Use% Mounted on
udev           devtmpfs  989M  4.0K  989M   1% /dev
tmpfs          tmpfs     201M  716K  200M   1% /run
/dev/dm-0      ext4       19G  1.5G   16G   9% /
none           tmpfs     4.0K     0  4.0K   0% /sys/fs/cgroup
none           tmpfs     5.0M     0  5.0M   0% /run/lock
none           tmpfs    1001M     0 1001M   0% /run/shm
none           tmpfs     100M     0  100M   0% /run/user
/dev/sda1      ext2      236M   40M  184M  18% /boot

这里的目的是增加挂载在 /dev/sda5 上的分区 /dev/dm-0 的大小。

让我也显示物理卷、卷组和逻辑卷的当前状态:

[email protected]:~# lvs
  LV     VG          Attr      LSize    Pool Origin Data%  Move Log Copy%  Convert
  root   ubuntu14-vg -wi-ao---   18.74g                                           
  swap_1 ubuntu14-vg -wi-ao--- 1020.00m   
  
[email protected]:~# pvs
  PV         VG          Fmt  Attr PSize  PFree 
  /dev/sda5  ubuntu14-vg lvm2 a--  19.76g 20.00m
  
[email protected]:~# vgs
  VG          #PV #LV #SN Attr   VSize  VFree 
  ubuntu14-vg   1   2   0 wz--n- 19.76g 20.00m

它们都分配了大约 20 GB 的存储空间。

步骤 2:开始 LVM 大小调整过程

以下是调整 LVM 分区大小的步骤:

打开 fdisk 实用程序并查看分区:

[email protected]:~# fdisk /dev/sda

Command (m for help): p

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders, total 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 identifier: 0x000a975f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      499711      248832   83  Linux
/dev/sda2          501758    41940991    20719617    5  Extended
/dev/sda5          501760    41940991    20719616   8e  Linux LVM

使用命令删除扩展分区 (/dev/sda2) d,这将自动删除底层 LVM 分区,即 /dev/sda5。

Command (m for help): d
Partition number (1-5): 2

使用命令再次创建一个新分区作为扩展 n 具有默认的开始和结束柱面值。

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): e
Partition number (1-4, default 2): 
Using default value 2
First sector (499712-83886079, default 499712): 
Using default value 499712
Last sector, +sectors or +size{K,M,G} (499712-83886079, default 83886079): 
Using default value 83886079

Command (m for help): p

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders, total 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 identifier: 0x000a975f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      499711      248832   83  Linux
/dev/sda2          499712    83886079    41693184    5  Extended

使用默认的开始和结束柱面值创建逻辑分区 (dev/sda5)。

Command (m for help): n
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (501760-83886079, default 501760): 
Using default value 501760
Last sector, +sectors or +size{K,M,G} (501760-83886079, default 83886079): 
Using default value 83886079

按下切换到专家模式 x.

Command (m for help): x

运行专家命令 b 调整分区的开头(这会更改分区大小,而不是结束位置)。 Enter 删除分区之前的起始值。 这里是 501760。

Expert command (m for help): b
Partition number (1-5): 5
New beginning of data (499713-83886079, default 501760): 501760

然后运行 r 返回主菜单。

Expert command (m for help): r

检查分区号以确保。

Command (m for help): p

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders, total 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 identifier: 0x000a975f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      499711      248832   83  Linux
/dev/sda2          499712    83886079    41693184    5  Extended
/dev/sda5          501760    83886079    41692160   83  Linux

现在通过按将分区类型更改为 LVM t 命令并选择类型 8e.

Command (m for help): t
Partition number (1-5): 5
Hex code (type L to list codes): 8e
Changed system type of partition 5 to 8e (Linux LVM)

w 将所有更改写入磁盘。

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

第 3 步:手动更改物理和逻辑卷

运行 partprobe 命令以通知操作系统有关分区表的更改:

[email protected]:~# partprobe /dev/sda

运行 lsblk 命令以查看 /dev/sda5 现在大小约为 40 GB(对我而言)。

[email protected]:~# lsblk 
NAME                           MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda                              8:0    0    40G  0 disk 
├─sda1                           8:1    0   243M  0 part /boot
├─sda2                           8:2    0     1K  0 part 
└─sda5                           8:5    0  39.8G  0 part 
  ├─ubuntu14--vg-root (dm-0)   252:0    0  18.8G  0 lvm  /
  └─ubuntu14--vg-swap_1 (dm-1) 252:1    0  1020M  0 lvm  [SWAP]
sr0                             11:0    1  1024M  0 rom  

运行 df 命令,您会注意到 /dev/dm-0 仍然显示旧的大小详细信息:

[email protected]:~# df -hT
Filesystem     Type      Size  Used Avail Use% Mounted on
udev           devtmpfs  989M  4.0K  989M   1% /dev
tmpfs          tmpfs     201M  716K  200M   1% /run
/dev/dm-0      ext4       19G  1.5G   16G   9% /
none           tmpfs     4.0K     0  4.0K   0% /sys/fs/cgroup
none           tmpfs     5.0M     0  5.0M   0% /run/lock
none           tmpfs    1001M     0 1001M   0% /run/shm
none           tmpfs     100M     0  100M   0% /run/user
/dev/sda1      ext2      236M   40M  184M  18% /boot

物理卷、卷组和逻辑卷也是如此:

[email protected]:~# lvs
  LV     VG          Attr      LSize    Pool Origin Data%  Move Log Copy%  Convert
  root   ubuntu14-vg -wi-ao---   18.74g                                           
  swap_1 ubuntu14-vg -wi-ao--- 1020.00m   
  
[email protected]:~# pvs
  PV         VG          Fmt  Attr PSize  PFree 
  /dev/sda5  ubuntu14-vg lvm2 a--  19.76g 20.00m
  
[email protected]:~# vgs
  VG          #PV #LV #SN Attr   VSize  VFree 
  ubuntu14-vg   1   2   0 wz--n- 19.76g 20.00m

你必须在这里做一些手动工作。

调整物理卷的大小:

[email protected]:~# pvresize /dev/sda5 
Physical volume "/dev/sda5" changed
1 physical volume(s) resized / 0 physical volume(s) not resized

现在检查物理卷和卷组状态,看看它是否正确显示了新大小:

[email protected]:~# pvs
PV         VG          Fmt  Attr PSize  PFree 
/dev/sda5  ubuntu14-vg lvm2 a--  39.76g 20.02g

[email protected]:~# vgs
VG          #PV #LV #SN Attr   VSize  VFree 
ubuntu14-vg   1   2   0 wz--n- 39.76g 20.02g

同样,调整逻辑卷的大小:

[email protected]:~# lvextend -l +100%FREE /dev/ubuntu14-vg/root
  Extending logical volume root to 38.76 GiB
  Logical volume root successfully resized

最后,调整文件系统的大小:

[email protected]:~# resize2fs /dev/ubuntu14-vg/root
resize2fs 1.42.9 (4-Feb-2014)
Filesystem at /dev/ubuntu14-vg/root is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/ubuntu14-vg/root is now 10161152 blocks long.

验证磁盘状态并查看 LVM 现在已正确调整大小:

[email protected]:~# df -hT
Filesystem     Type      Size  Used Avail Use% Mounted on
udev           devtmpfs  989M  4.0K  989M   1% /dev
tmpfs          tmpfs     201M  716K  200M   1% /run
/dev/dm-0      ext4       39G  1.5G   35G   4% /
none           tmpfs     4.0K     0  4.0K   0% /sys/fs/cgroup
none           tmpfs     5.0M     0  5.0M   0% /run/lock
none           tmpfs    1001M     0 1001M   0% /run/shm
none           tmpfs     100M     0  100M   0% /run/user
/dev/sda1      ext2      236M   40M  184M  18% /boot

就这样! 您已成功调整扩展分区内的 LVM 分区的大小。

问题或建议? 在下面发表评论。