自从使用虚拟机以后,经常会遇到虚拟机硬盘不够的情况,本文在借鉴其他文章基础上,修正一些不完善的步骤,本着利己利人的思想,在此整理一下全部步骤,并就LVM容易出错的地方给予一些建议。
在此之前,我们先普及一下硬盘扩容为什么要做那么多操作?
这个问题和计算机操作系统的分层管理有关。我们知道硬盘是最底层的硬件,在这之上,我们可以创建分区,什么启动分区,逻辑分区之类。然后才是在分区上面创建文件系统。最终用户的操作都是在文件系统上面。好了,问题来了,当我们加一盘硬盘后或是硬盘扩容,只是增大了硬盘的容量。在我们没有相应地增加分区,增大文件系统容量之前,Linux是不会让我们利用新的容量的。
就这一点来看,和windows相比,Linux还是有不少的改进空间,虽然Linux也有分区工具,但明显没windows好用。
既然用Linux,说明你想提高动手能力,那就开始练习吧!希望下面能帮到你。
0.硬件环境
宿主机:Windows 10,Hyper-V
虚拟机:Ubuntu16.04,默认使用LVM管理所有扩展分区
问题:Ubuntu安装时,只设定了16G硬盘,后来发现跑Docker不够,想扩大到32G。
1.VHD硬盘扩容
Hyper-V的vhd可以很方便的扩容,见下图。其他虚拟机用户可以跳过这一步。注意:扩容不是live操作,VM需要关机,硬盘扩容后再重启VM。
经过扩容,重启Ubuntu,用fdisk -l查看,硬盘容量已经增大:
Disk /dev/sda: 32 GiB, 34359738368 bytes, 67108864 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
2.Linux虚拟机增大扩展分区(parted)
把新增加的容量添加到扩展分区里面,这里用命令行工具parted来介绍如何增大分区,运行parted /dev/sda,进入交互式模式,
/dev/sda2 1001470 67108863 66107394 31.5G 5 Extended
/dev/sda5 1001472 31455231 30453760 14.5G 8e Linux LVM
Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 6
First sector (31457280-67108863, default 31457280):
Last sector, +sectors or +size{K,M,G,T,P} (31457280-67108863, default 67108863):
Created a new partition 6 of type 'Linux' and of size 17 GiB.
为了用LVM管理,我们把刚添加的逻辑分区改为Linux LVM类型。输入t,选择默认分区号,列出所有类型,输入8e
Command (m for help): t
Partition number (1,2,5,6, default 6): 6
Partition type (type L to list all types): L
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden or c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi ea Rufus alignment
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD eb BeOS fs
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ee GPT
10 OPUS 55 EZ-Drive a7 NeXTSTEP ef EFI (FAT-12/16/
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f0 Linux/PA-RISC b
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f1 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f4 SpeedStor
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ f2 DOS secondary
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fb VMware VMFS
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fc VMware VMKCORE
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fd Linux raid auto
1c Hidden W95 FAT3 75 PC/IX bc Acronis FAT32 L fe LANstep
1e Hidden W95 FAT1 80 Old Minix be Solaris boot ff BBT
Partition type (type L to list all types): 8e
Changed type of partition 'Linux' to 'Linux LVM'.
写入修改:输入w,让修改生效,就可以退出fdisk了。
3.扩展一个LVM Volume
LVM自己也定义了很多层的概念,这就要求我们一层一层地扩容:Physical Volumes (PVs) --> Volume Groups (VGs) --> Logical Volumes (LVs) --> Filesystems相应的,我们会发现分别有pv开头,vg开头和lv开头的三组命令集,长得都很相似。这就是后面扩容分别需要用到的3个层面的命令。
root@ubuntu:~# pv
pvchange pvcreate pvmove pvresize pvscan
pvck pvdisplay pvremove pvs
root@ubuntu:~# vg
vgcfgbackup vgconvert vgextend vgmknodes vgs
vgcfgrestore vgcreate vgimport vgreduce vgscan
vgchange vgdisplay vgimportclone vgremove vgsplit
vgck vgexport vgmerge vgrename
root@ubuntu:~# lv
lvchange lvextend lvmconfig lvmpolld lvremove lvscan
lvconvert lvm lvmdiskscan lvmsadc lvrename
lvcreate lvmchange lvmdump lvmsar lvresize
lvdisplay lvmconf lvmetad lvreduce lvs
LVM物理分区
先查看一下LVM眼里的物理分区:
root@ubuntu:~# pvdisplay
--- Physical volume ---
PV Name /dev/sda5
VG Name ubuntu-vg
PV Size 14.52 GiB / not usable 2.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 3717
Free PE 0
Allocated PE 3717
PV UUID VMRYTM-LMxz-V7wd-uGvM-4mYF-vZho-WQ7WZY
我们要用pvcreate创建一个新的物理分区。这里有一个坑,有可能你会遇到下面的错误:
root@ubuntu:~# pvcreate /dev/sda6
Device /dev/sda6 not found (or ignored by filtering).
原因是需要通知OS分区表变了(为什么自己不能检测到呢?)
root@ubuntu:~# partprobe /dev/sda
然后再执行pvcreate /dev/sda6,就一切正常了。
root@ubuntu:~# partprobe /dev/sda
root@ubuntu:~# pvcreate /dev/sda6
Physical volume "/dev/sda6" successfully created
再查看一下,发现多了一个分区:
root@ubuntu:~# pvdisplay
--- Physical volume ---
PV Name /dev/sda5
VG Name ubuntu-vg
PV Size 14.52 GiB / not usable 2.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 3717
Free PE 0
Allocated PE 3717
PV UUID VMRYTM-LMxz-V7wd-uGvM-4mYF-vZho-WQ7WZY
--- Physical volume ---
PV Name /dev/sda6
VG Name ubuntu-vg
PV Size 17.00 GiB / not usable 4.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4351
Free PE 0
Allocated PE 4351
PV UUID vte0Qd-a8Ls-vtfN-mqaE-2OOe-Lgji-ejMHmv
Volume分组
用vgdisplya和vgs查看一下当前状态,找到VG Name,同时可以看到#PV还是1。
root@ubuntu:~# vgdisplay
--- Volume group ---
VG Name ubuntu-vg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 14.52 GiB
PE Size 4.00 MiB
Total PE 3717
Alloc PE / Size 3717 / 14.52 GiB
Free PE / Size 0 / 0
VG UUID nN8qv2-WxTS-nU1M-UAND-H0oO-BR9N-m6G2h8
root@ubuntu:~# vgs
VG #PV #LV #SN Attr VSize VFree
ubuntu-vg 1 2 0 wz--n- 14.52g 0
扩展Volumne group,命令格式:vgextend <VG Name> /dev/XXX
root@ubuntu:~# vgextend ubuntu-vg /dev/sda6
Volume group "ubuntu-vg" successfully extended
root@ubuntu:~# vgs
VG #PV #LV #SN Attr VSize VFree
ubuntu-vg 2 2 0 wz--n- 31.52g 17.00g
可以看到#PV增加了。
增大LVM逻辑分区
用lvdisplay查看LVM逻辑分区的大小:
root@ubuntu:~# lvdisplay
--- Logical volume ---
LV Path /dev/ubuntu-vg/root
LV Name root
VG Name ubuntu-vg
LV UUID JAEiLa-DAeu-A3sO-KFED-QsEg-u8JQ-7KPd2z
LV Write Access read/write
LV Creation host, time ubuntu, 2017-02-14 09:48:38 +0800
LV Status available
# open 1
LV Size 12.52 GiB
Current LE 3205
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:0
--- Logical volume ---
LV Path /dev/ubuntu-vg/swap_1
LV Name swap_1
VG Name ubuntu-vg
LV UUID xW4yE3-DMW1-rOK4-qhNE-DDOk-hjTu-w6uu3f
LV Write Access read/write
LV Creation host, time ubuntu, 2017-02-14 09:48:38 +0800
LV Status available
# open 2
LV Size 2.00 GiBroot@ubuntu:~# parted /dev/sda
GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)
输入p,查看分区情况。输入help,可以查看所有命令。
Model: Msft Virtual Disk (scsi)
Disk /dev/sda: 34.4GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 512MB 511MB primary ext2 boot
2 513MB 16.1GB 15.6GB extended
5 513MB 16.1GB 15.6GB logical lvm
可以看到扩展分区只有15.6GB,而且扩展分区全部用来做逻辑分区,参见Number 5。我们用resizepart来调整扩展分区,注意resize命令已经在parted3.0版本移除。resizepart接受两个参数,第一个是Number号,第二个是End。
(parted) resize 2 34.4GB
(parted) p
Model: Msft Virtual Disk (scsi)
Disk /dev/sda: 34.4GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 512MB 511MB primary ext2 boot
2 513MB 34.4GB 33.8GB extended
5 513MB 16.1GB 15.6GB logical lvm
至此,我们已经增大扩展分区容量。要想利用这个分区,我们还得相应的增大逻辑分区的容量。但是,请注意,不要直接在这里增大Number 5,原因是LVM不支持自动扩容,我们只能创建一个新的逻辑分区,然后加入Volume Group。
用quit退出parted。
4..创建Linux LVM逻辑分区
用fdisk /dev/sda进入交互式模式,在刚刚扩容的分区上创建新分区。用m查看帮助,用n创建新分区,默认会选择新增逻辑分区,编号自动产生,接着设置起始、结束位置,默认用完全部分区。
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 999423 997376 487M 83 Linux
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:1
使用lvresize命令扩容,lvresize -l +100%FREE <LV Path>
root@ubuntu:~# lvresize -l +100%FREE /dev/ubuntu-vg/root
Size of logical volume ubuntu-vg/root changed from 12.52 GiB (3205 extents) to 29.52 GiB (7556 extents).
Logical volume root successfully resized.
root@ubuntu:~# lvdisplay
--- Logical volume ---
LV Path /dev/ubuntu-vg/root
LV Name root
VG Name ubuntu-vg
LV UUID JAEiLa-DAeu-A3sO-KFED-QsEg-u8JQ-7KPd2z
LV Write Access read/write
LV Creation host, time ubuntu, 2017-02-14 09:48:38 +0800
LV Status available
# open 1
LV Size 29.52 GiB
Current LE 7556
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:0
--- Logical volume ---
LV Path /dev/ubuntu-vg/swap_1
LV Name swap_1
VG Name ubuntu-vg
LV UUID xW4yE3-DMW1-rOK4-qhNE-DDOk-hjTu-w6uu3f
LV Write Access read/write
LV Creation host, time ubuntu, 2017-02-14 09:48:38 +0800
LV Status available
# open 2
LV Size 2.00 GiB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:1
5.增大文件系统容量
最后,需要用resize2fs增大文集系统容量,在此之前,先用df查看/分区的名字。
root@ubuntu:~# df -h
Filesystem Size Used Avail Use% Mounted on
udev 473M 0 473M 0% /dev
tmpfs 99M 6.9M 92M 7% /run
/dev/mapper/ubuntu--vg-root 13G 7.7G 4.0G 67% /
tmpfs 492M 0 492M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 492M 0 492M 0% /sys/fs/cgroup
/dev/sda1 472M 103M 346M 23% /boot
tmpfs 100K 0 100K 0% /run/lxcfs/controllers
tmpfs 99M 0 99M 0% /run/user/1000
root@ubuntu:~# resize2fs -p /dev/mapper/ubuntu--vg-root
resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/mapper/ubuntu--vg-root is mounted on /; on-line resizing requ ired
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/mapper/ubuntu--vg-root is now 7737344 (4k) blocks long.