參考網頁:https://blog.csdn.net/wzy0623/article/details/50667442
一、CMD下擴容
1.進入VIrtualBox安裝位置:cd E:\SoftWare\VirtualBox
2.若格式不是VDI則將VDH虛擬硬盤轉化成VDI:
VBoxManage clonehd E:\SoftWare\VirtualBoxDisk\linux01.vhd E:\SoftWare\VirtualBoxDisk\linux02.vdi --format VDI
3.VDI擴容
VBoxManage modifyhd E:\SoftWare\VirtualBoxDisk\linux02.vdi --resize 20480
中間存在空格時可使用“”修飾
VBoxManage modifyhd "E:\SoftWare\VirtualBox VMs\centos6.5\linux01.vdi" --resize 20480
二.centos6.5 linux下生效
1.查看掛載情況fdisk -l
[root@localhost home]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0005137c
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 1045 7875584 8e Linux LVM
Disk /dev/mapper/VolGroup-lv_root: 7205 MB, 7205814272 bytes
255 heads, 63 sectors/track, 876 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/mapper/VolGroup-lv_swap: 855 MB, 855638016 bytes
255 heads, 63 sectors/track, 104 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
可以看到:
(1)Linux就一塊虛擬的物理盤/dev/sda,擴容后該盤的空間20G。
(2)該盤已有兩個物理分區/dev/sda1和/dev/sda2。
(3)/dev/sda1 485M,用作boot分區。
(4)/dev/sda2 7.5G,分成兩個邏輯卷,一個用作根6G,一個用作交換1.5G。
下面要做的就是將/dev/sda上未分配的磁盤空間分區,並添加到根分區所屬的邏輯卷上。
2.將/dev/sda上未分配的磁盤空間分區
[root@localhost home]# fdisk /dev/sda
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (1045-2610, default 1045): w
First cylinder (1045-2610, default 1045):
Using default value 1045
Last cylinder, +cylinders or +size{K,M,G} (1045-2610, default 2610):
Using default value 2610
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.重啟虛擬機
4.將新建的分區格式化,建立文件系統mkfs.ext4 /dev/sda3
[root@localhost ~]# mkfs.ext4 /dev/sda3
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
786432 inodes, 3144054 blocks
157202 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=3221225472
96 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 29 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
5.創建物理卷 pvcreate /dev/sda3
[root@localhost ~]# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created
6.執行完后查看物理卷 pvdisplay
[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name VolGroup
PV Size 7.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 1922
Free PE 0
Allocated PE 1922
PV UUID 5UbDaB-Lvxi-i0MV-tjZ4-fRZ1-hxsp-6SLBz5
"/dev/sda3" is a new physical volume of "11.99 GiB"
--- NEW Physical volume ---
PV Name /dev/sda3
VG Name
PV Size 11.99 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID ldePe6-5NF1-6Nsn-QdEW-Evdj-Qjl1-PuGe9a
可以看到新增了一個12G的物理卷
7.查看卷組 vgdisplay
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name VolGroup
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 7.51 GiB
PE Size 4.00 MiB
Total PE 1922
Alloc PE / Size 1922 / 7.51 GiB
Free PE / Size 0 / 0
VG UUID 0JYZxD-Yg23-ytaP-O66f-DOlF-Rm3O-7X0KhH
可以看到只有一個名為VolGroup的卷組,當前大小為7.51G
8.擴展卷組vgextend vg_fab1 /dev/sda3
[root@localhost ~]# vgextend VolGroup /dev/sda3
Volume group "VolGroup" successfully extended
9.查看邏輯卷lvdisplay
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/VolGroup/lv_root
LV Name lv_root
VG Name VolGroup
LV UUID feWkC3-4OpZ-fmQg-kEbq-697P-S7P0-Mp9jKG
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2018-11-21 11:31:09 +0800
LV Status available
# open 1
LV Size 6.71 GiB
Current LE 1718
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
--- Logical volume ---
LV Path /dev/VolGroup/lv_swap
LV Name lv_swap
VG Name VolGroup
LV UUID G1WHul-iCRS-PnA1-YOZh-AbdK-cyEm-JqUNEr
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2018-11-21 11:31:15 +0800
LV Status available
# open 1
LV Size 816.00 MiB
Current LE 204
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1
可以看到用作根的邏輯卷路徑為/dev/VolGroup/lv_root
10.擴展邏輯卷 lvextend
[root@localhost ~]# lvextend -L 18G -n /dev/VolGroup/lv_root
Extending logical volume lv_root to 18.00 GiB
Logical volume lv_root successfully resized
11.調整根邏輯卷大小 resize2fs
當系統為centos7時,調整使用xfs_growfs 替換 resize2fs
[root@localhost ~]# resize2fs /dev/VolGroup/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/VolGroup/lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/VolGroup/lv_root to 4718592 (4k) blocks.
The filesystem on /dev/VolGroup/lv_root is now 4718592 blocks long.
12.再次查看當前操作系統的空間情況df-h
[root@localhost ~]#
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 4.8G 13G 29% /
tmpfs 1.9G 72K 1.9G 1% /dev/shm
/dev/sda1 485M 35M 426M 8% /boot
[root@localhost ~]#