Linux

增加 LVM 以使用 virtualbox/debian 上的所有可用空間

  • September 25, 2014

我已經為 VirtualBox 複製並調整了磁碟映像的大小,但我找不到使用所有新空間的方法。這就是我開始的方式:

磁碟:

Disk /dev/sda: 80.5 GB, 80530636800 bytes
255 heads, 63 sectors/track, 9790 cylinders, total 157286400 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: 0x00034aae

  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

lv顯示:

 --- Logical volume ---
 LV Path                /dev/localhost/root
 LV Name                root
 VG Name                localhost
 LV UUID                tbJqka-ZlYQ-nkq4-Cufz-Yl7t-peSL-z1FGpR
 LV Write Access        read/write
 LV Creation host, time localhost, 2014-04-16 21:15:12 +0200
 LV Status              available
 # open                 1
 LV Size                18.91 GiB
 Current LE             4840
 Segments               1
 Allocation             inherit
 Read ahead sectors     auto
 - currently set to     256
 Block device           254:0

 --- Logical volume ---
 LV Path                /dev/localhost/swap_1
 LV Name                swap_1
 VG Name                localhost
 LV UUID                3yEDBL-Nc0G-QuVU-M9iR-Pe3u-r6E4-A5egVb
 LV Write Access        read/write
 LV Creation host, time localhost, 2014-04-16 21:15:12 +0200
 LV Status              available
 # open                 2
 LV Size                872.00 MiB
 Current LE             218
 Segments               1
 Allocation             inherit
 Read ahead sectors     auto
 - currently set to     256
 Block device           254:1

所以,我現在已經刪除了 sda2 分區並創建了一個新分區,如下所示(這是在重新啟動後):

Disk /dev/sda: 80.5 GB, 80530636800 bytes
255 heads, 63 sectors/track, 9790 cylinders, total 157286400 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: 0x00034aae

  Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      499711      248832   83  Linux
/dev/sda2          499712   157286399    78393344    5  Extended
/dev/sda5          501760   157286399    78392320   8e  Linux LVM

那麼,我現在應該怎麼做才能增加 LVM 組,然後將大小調整為 100%FREE?

謝謝!

在上述之後,我自己找到了解決方案:

vgdisplay
 --- Volume group ---
 VG Name               localhost
 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               19.76 GiB
 PE Size               4.00 MiB
 Total PE              5058
 Alloc PE / Size       5058 / 19.76 GiB
 Free  PE / Size       0 / 0
 VG UUID               XgGBDt-lHmJ-3Tci-hKW6-IRFU-PxN2-wsJmek

進而:

pvresize /dev/sda5
 Physical volume "/dev/sda5" changed
 1 physical volume(s) resized / 0 physical volume(s) not resized
root@host:/home/vagrant# vgdisplay
 --- Volume group ---
 VG Name               localhost
 System ID
 Format                lvm2
 Metadata Areas        1
 Metadata Sequence No  4
 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               74.76 GiB
 PE Size               4.00 MiB
 Total PE              19138
 Alloc PE / Size       5058 / 19.76 GiB
 Free  PE / Size       14080 / 55.00 GiB
 VG UUID               XgGBDt-lHmJ-3Tci-hKW6-IRFU-PxN2-wsJmek

root@host:/home/vagrant# vgdisplay
 --- Volume group ---
 VG Name               localhost
 System ID
 Format                lvm2
 Metadata Areas        1
 Metadata Sequence No  4
 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               74.76 GiB
 PE Size               4.00 MiB
 Total PE              19138
 Alloc PE / Size       5058 / 19.76 GiB
 Free  PE / Size       14080 / 55.00 GiB
 VG UUID               XgGBDt-lHmJ-3Tci-hKW6-IRFU-PxN2-wsJmek

root@host:/home/vagrant# lvextend -l +100%FREE /dev/localhost/root
 Extending logical volume root to 73.91 GiB
 Logical volume root successfully resized

root@host:/home/vagrant# resize2fs /dev/localhost/root
resize2fs 1.42.5 (29-Jul-2012)
Filesystem at /dev/localhost/root is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 5
Performing an on-line resize of /dev/localhost/root to 19374080 (4k) blocks.
The filesystem on /dev/localhost/root is now 19374080 blocks long.

root@host:/home/vagrant# df -lah
Filesystem                  Size  Used Avail Use% Mounted on
rootfs                       73G   18G   52G  26% /

引用自:https://serverfault.com/questions/631152