Hyper-V Linux VM Disk擴容
阿新 • • 發佈:2019-01-04
自從使用虛擬機器以後,經常會遇到虛擬機器硬碟不夠的情況,本文在借鑑其他文章基礎上,修正一些不完善的步驟,本著利己利人的思想,在此整理一下全部步驟,並就LVM容易出錯的地方給予一些建議。
在此之前,我們先普及一下硬碟擴容為什麼要做那麼多操作?
這個問題和計算機作業系統的分層管理有關。我們知道硬碟是最底層的硬體,在這之上,我們可以建立分割槽,什麼啟動分割槽,邏輯分割槽之類。然後才是在分割槽上面建立檔案系統。終端使用者的操作都是在檔案系統上面。好了,問題來了,當我們加一盤硬碟後或是硬碟擴容,只是增大了硬碟的容量。在我們沒有相應地增加分割槽,增大檔案系統容量之前,Linux是不會讓我們利用新的容量的。
就這一點來看,和windows相比,Linux還是有不少的改進空間,雖然Linux也有分割槽工具,但明顯沒windows好用。
既然用Linux,說明你想提高動手能力,那就開始練習吧!希望下面能幫到你。
硬體環境
宿主機:Windows 10,Hyper-V
虛擬機器:Ubuntu16.04,預設使用LVM管理所有擴充套件分割槽
問題:Ubuntu安裝時,只設定了16G硬碟,後來發現跑docker不夠,想擴大到32G。
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
Linux虛擬機器增大擴充套件分割槽(parted)
把新增加的容量新增到擴充套件分割槽裡面,這裡用命令列工具parted來介紹如何增大分割槽,執行parted /dev/sda,進入互動式模式,
為了用LVM管理,我們把剛新增的邏輯分割槽改為Linux LVM型別。輸入t,選擇預設分割槽號,列出所有型別,輸入8e/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.
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了。
建立Linux LVM邏輯分割槽
用fdisk /dev/sda進入互動式模式,在剛剛擴容的分割槽上建立新分割槽。用m檢視幫助,用n建立新分割槽,預設會選擇新增邏輯分割槽,編號自動產生,接著設定起始、結束位置,預設用完全部分割槽。Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 999423 997376 487M 83 Linux
/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了。
擴充套件一個LVM Volume
LVM自己也定義了很多層的概念,這就要求我們一層一層地擴容:Physical Volumes (PVs) --> Volume Groups (VGs) --> Logical Volumes (LVs) --> Filesystems相應的,我們會發現分別有pv開頭,vg開頭和lv開頭的三組命令集,長得都很相似。這就是後面擴容分別需要用到的3個層面的命令。
[email protected]:~# pv
pvchange pvcreate pvmove pvresize pvscan
pvck pvdisplay pvremove pvs
[email protected]:~# vg
vgcfgbackup vgconvert vgextend vgmknodes vgs
vgcfgrestore vgcreate vgimport vgreduce vgscan
vgchange vgdisplay vgimportclone vgremove vgsplit
vgck vgexport vgmerge vgrename
[email protected]:~# lv
lvchange lvextend lvmconfig lvmpolld lvremove lvscan
lvconvert lvm lvmdiskscan lvmsadc lvrename
lvcreate lvmchange lvmdump lvmsar lvresize
lvdisplay lvmconf lvmetad lvreduce lvs
LVM物理分割槽
我們要用pvcreate建立一個新的物理分割槽。這裡有一個坑,有可能你會遇到下面的錯誤:
[email protected]:~# pvcreate /dev/sda6
Device /dev/sda6 not found (or ignored by filtering).
原因是需要通知OS分割槽表變了(為什麼自己不能檢測到呢?),然後再執行pvcreate /dev/sda6,就一切正常了。
[email protected]:~# partprobe /dev/sda
[email protected]:~# pvcreate /dev/sda6
Physical volume "/dev/sda6" successfully created
Volume分組
用vgdisplya和vgs檢視一下當前狀態,找到VG Name,同時可以看到#PV還是1。 擴充套件Volumne group,命令格式:vgextend <VG Name> /dev/XXX,VG Name可以用vgdisplay得到[email protected]:~# vgextend ubuntu-vg /dev/sda6
Volume group "ubuntu-vg" successfully extended
[email protected]:~# vgs
VG #PV #LV #SN Attr VSize VFree
ubuntu-vg 2 2 0 wz--n- 31.52g 17.00g
增大LVM邏輯分割槽
使用lvresize命令擴容,lvresize -l +100%FREE <LV Path>,用lvdisplay得到LV Path[email protected]:~# 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.
增大檔案系統容量
最後,需要用resize2fs增大文集系統容量,在此之前,先用df檢視/分割槽的名字。[email protected]:~# 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
[email protected]:~# 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.
參考
http://ohmystack.com/articles/LVM-in-actionhttps://www.turnkeylinux.org/blog/extending-lvm