本篇文章主要分享如何在Linux扩展分区内调整LVM分区的大小。
在Linux中调整逻辑卷的大小不是很困难,可以通过非常简单的方法来实现。这是通常的步骤:
- 在硬盘上创建新分区。
- 将刚创建的分区添加为物理卷。
- 将新的物理卷添加到卷组。
- 将空间从卷组分配到逻辑卷。
- 调整文件系统的大小。
但是在这种情况下,您将根文件系统(作为LVM分区)安装在扩展分区下,而不是在主分区内。您只有一个主分区安装在/ boot上,其余所有空间都属于该扩展分区。
听起来很麻烦?让我向您展示如何在扩展分区内调整LVM的大小。
在扩展分区内调整LVM分区的大小
在本教程中,我正在使用虚拟机中安装的Linux。
步骤1:关闭虚拟机并增加磁盘大小
首先,关闭您的VM并增加磁盘大小。在这里,我将磁盘/dev/sda的大小增加了20 GB,增加到40 GB左右。然后启动您的VM并进入控制台。
看看我们的磁盘分区。
root@Ubuntu14:~# fdisk -lDisk /dev/sda: 42.9 GB, 42949672960 bytes255 heads, 63 sectors/track, 5221 cylinders, total 83886080 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk 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命令分析磁盘空间,则对我显示的内容是:
root@Ubuntu14:~# df -hTFilesystem Type Size Used Avail Use% Mounted onudev devtmpfs 989M 4.0K 989M 1% /devtmpfs 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/cgroupnone tmpfs 5.0M 0 5.0M 0% /run/locknone tmpfs 1001M 0 1001M 0% /run/shmnone tmpfs 100M 0 100M 0% /run/user/dev/sda1 ext2 236M 40M 184M 18% /boot
在这里,目的是增加安装在/ dev / sda5上的/ dev / dm-0分区的大小。
让我也显示物理卷,卷组和逻辑卷的当前状态:
root@Ubuntu14:~# 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 root@Ubuntu14:~# pvs PV VG Fmt Attr PSize PFree /dev/sda5 ubuntu14-vg lvm2 a-- 19.76g 20.00m root@Ubuntu14:~# 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实用程序,然后查看分区:
root@Ubuntu14:~# fdisk /dev/sdaCommand (m for help): pDisk /dev/sda: 42.9 GB, 42949672960 bytes255 heads, 63 sectors/track, 5221 cylinders, total 83886080 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk 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
使用command删除扩展分区(/dev/sda2)d,这将自动删除基础LVM分区/dev/sda5。
Command (m for help): dPartition number (1-5): 2
使用n默认的开始和结束柱面值,使用命令扩展来再次创建一个新分区。
Command (m for help): nPartition type: p primary (1 primary, 0 extended, 3 free) e extendedSelect (default p): ePartition number (1-4, default 2): Using default value 2First sector (499712-83886079, default 499712): Using default value 499712Last sector, +sectors or +size{K,M,G} (499712-83886079, default 83886079): Using default value 83886079Command (m for help): pDisk /dev/sda: 42.9 GB, 42949672960 bytes255 heads, 63 sectors/track, 5221 cylinders, total 83886080 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk 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): nPartition type: p primary (1 primary, 1 extended, 2 free) l logical (numbered from 5)Select (default p): lAdding logical partition 5First sector (501760-83886079, default 501760): Using default value 501760Last sector, +sectors or +size{K,M,G} (501760-83886079, default 83886079): Using default value 83886079
按切换到专家模式x。
Command (m for help): x
运行专家命令b以调整分区的开头(这将更改分区大小,而不是结束位置)。输入删除分区之前的开始值。这是501760。
Expert command (m for help): bPartition number (1-5): 5New beginning of data (499713-83886079, default 501760): 501760
然后运行r以返回主菜单。
Expert command (m for help): r
检查分区号只是为了确保。
Command (m for help): pDisk /dev/sda: 42.9 GB, 42949672960 bytes255 heads, 63 sectors/track, 5221 cylinders, total 83886080 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk 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
现在通过按t命令将分区类型更改为LVM并选择type 8e。
Command (m for help): tPartition number (1-5): 5Hex code (type L to list codes): 8eChanged system type of partition 5 to 8e (Linux LVM)
按w将所有更改写入磁盘。
Command (m for help): wThe 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 atthe next reboot or after you run partprobe(8) or kpartx(8)Syncing disks.
步骤3:对物理和逻辑卷进行手动更改
运行partprobe命令以通知OS有关分区表的更改:
root@Ubuntu14:~# partprobe /dev/sda
运行lsblk命令以查看/ dev / sda5现在的大小约为40 GB(对我而言)。
root@Ubuntu14:~# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTsda 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仍然显示旧的大小详细信息:
root@Ubuntu14:~# df -hTFilesystem Type Size Used Avail Use% Mounted onudev devtmpfs 989M 4.0K 989M 1% /devtmpfs 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/cgroupnone tmpfs 5.0M 0 5.0M 0% /run/locknone tmpfs 1001M 0 1001M 0% /run/shmnone tmpfs 100M 0 100M 0% /run/user/dev/sda1 ext2 236M 40M 184M 18% /boot
物理卷,卷组和逻辑卷也是如此:
root@Ubuntu14:~# 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 root@Ubuntu14:~# pvs PV VG Fmt Attr PSize PFree /dev/sda5 ubuntu14-vg lvm2 a-- 19.76g 20.00m root@Ubuntu14:~# vgs VG #PV #LV #SN Attr VSize VFree ubuntu14-vg 1 2 0 wz--n- 19.76g 20.00m
您必须在此处进行一些手动操作。
调整物理体积:
root@Ubuntu14:~# pvresize /dev/sda5 Physical volume "/dev/sda5" changed1 physical volume(s) resized / 0 physical volume(s) not resized
现在,检查“物理卷”和“卷组”状态,看是否正确显示了新的大小:
root@Ubuntu14:~# pvsPV VG Fmt Attr PSize PFree /dev/sda5 ubuntu14-vg lvm2 a-- 39.76g 20.02groot@Ubuntu14:~# vgsVG #PV #LV #SN Attr VSize VFree ubuntu14-vg 1 2 0 wz--n- 39.76g 20.02g
同样,调整逻辑卷的大小:
root@Ubuntu14:~# lvextend -l +100%FREE /dev/ubuntu14-vg/root Extending logical volume root to 38.76 GiB Logical volume root successfully resized
最后,调整文件系统的大小:
root@Ubuntu14:~# resize2fs /dev/ubuntu14-vg/rootresize2fs 1.42.9 (4-Feb-2014)Filesystem at /dev/ubuntu14-vg/root is mounted on /; on-line resizing requiredold_desc_blocks = 2, new_desc_blocks = 3The filesystem on /dev/ubuntu14-vg/root is now 10161152 blocks long.
验证磁盘状态,然后查看LVM现在是否已正确调整大小:
root@Ubuntu14:~# df -hTFilesystem Type Size Used Avail Use% Mounted onudev devtmpfs 989M 4.0K 989M 1% /devtmpfs 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/cgroupnone tmpfs 5.0M 0 5.0M 0% /run/locknone tmpfs 1001M 0 1001M 0% /run/shmnone tmpfs 100M 0 100M 0% /run/user/dev/sda1 ext2 236M 40M 184M 18% /boot
就这样!您已成功调整了扩展分区内LVM分区的大小。
VPS排行榜
评论前必须登录!
注册