`

调整 VirtualBox 虚拟机的磁盘大小

阅读更多

调整 VirtualBox 虚拟机的磁盘大小

使用 VM VirtualBox 创建了一个 CentOS 虚拟机,划分了 40GB 的磁盘空间,使用了动态分配存储的方式,文件系统为 LVM。折腾了一段时间之后,却发现磁盘容量不够了。读了一些资料之后,终于给 VirtualBox 的虚拟机增加了磁盘容量,并让 CentOS 的 LVM 将其利用上。

先看看虚拟机中虚拟磁盘的的分区表:

 

[root@10 ~]# fdisk -l

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 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: 0x0000698e

   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
/dev/sda3            1045        5221    33549074+  8e  Linux LVM

Disk /dev/mapper/vg_yuxuechengcentos6-lv_root: 40.3 GB, 40298872832 bytes
255 heads, 63 sectors/track, 4899 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/vg_yuxuechengcentos6-lv_swap: 2113 MB, 2113929216 bytes
255 heads, 63 sectors/track, 257 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

[root@10 ~]# 

 

VirtualBox 的磁盘管理

调整虚拟磁盘大小的时候,虚拟机必须关闭!

下面使用 VirtualBox 的命令行将该虚拟机的磁盘分配从 40GB 调整为 50GB。

VirtualBox 的说明文档中明确给出了调整虚拟机磁盘大小的方法。

modifymedium 指令就是用来调整磁盘大小的。对象可以是虚拟机的 uuid,也可以直接指定要调整大小的虚拟机磁盘文件名(全路径)。需要注意的是,如果安装 VirtualBox 的时候没有将其路径添加到系统路径之中,运行命令行的时候需要指定路径或切换到安装路径,如,

上述指令将 F:\VirtualBoxImgs目录下的 CentOS6.3.vdi 这个虚拟机磁盘调整为(参数 –resize)51200,单位是 MB。

如果想查看对应的 uuid,可以使用指令:

以上指令在Win10+VirtualBox 5.0.10版本上验证可行。

2. CentOS 的 LVM 管理

 

[root@10 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_yuxuechengcentos6-lv_root
                       37G   12G   26G  31% /
tmpfs                 499M   76K  499M   1% /dev/shm
/dev/sda1             477M   51M  397M  12% /boot
/dev/sr0               57M   57M     0 100% /media/VBOXADDITIONS_5.0.10_104061
[root@10 ~]# 

 重新启动该虚拟机。再来看看磁盘情况:

 

 

[root@10 ~]# fdisk -l

Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 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: 0x0000698e

   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
/dev/sda3            1045        5221    33549074+  8e  Linux LVM

Disk /dev/mapper/vg_yuxuechengcentos6-lv_root: 40.3 GB, 40298872832 bytes
255 heads, 63 sectors/track, 4899 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/vg_yuxuechengcentos6-lv_swap: 2113 MB, 2113929216 bytes
255 heads, 63 sectors/track, 257 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

[root@10 ~]# 

 现在,磁盘大小为 53680MB。磁盘占用情况为,可以看到总大小还是40G的配置。

 

 

[root@10 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_yuxuechengcentos6-lv_root
                       37G   12G   26G  31% /
tmpfs                 499M   76K  499M   1% /dev/shm
/dev/sda1             477M   51M  397M  12% /boot
/dev/sr0               57M   57M     0 100% /media/VBOXADDITIONS_5.0.10_104061
[root@10 ~]# 

 试试看用dd命令创建一个30G大小的块文件:

 

[root@10 ~]# dd if=/dev/zero of=1.0G.img bs=1M count=30000
dd: writing `1.0G.img': No space left on device
26223+0 records in
26222+0 records out
27496136704 bytes (27 GB) copied, 104.717 s, 263 MB/s
[root@10 ~]# 

 

提示说磁盘空间不足(No space left on device)。虽然我们给了虚拟机足够的空间,但是虚拟机里的系统(这里是 CentOS)并没有将这一部分空间利用上。可以看到,虚拟机里的磁盘空间只有 37G,原始分配的 50G 没有用完。这同时也说明,即使创建虚拟机的时候使用动态分配磁盘大小,虚拟机里的系统也并不会自动修改自己的磁盘。如果是正常使用,即使虚拟机的虚拟磁盘 足够大,还是可能会在虚拟机中遇到磁盘空间不足的警告。

 

下面调整虚拟机操作系统的文件系统,使之将所有空间利用上。

将空余磁盘创建为 sda4

重新启动虚拟机,登入 CentOS,使用 fdisk 将虚拟磁盘的空闲空间创建为一个新的分区。注意要使用代表 Linux LVM 的分区号 8e 来作为 ID。

 

[root@10 ~]# 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): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Selected partition 4
First cylinder (5222-6527, default 5222): 
Using default value 5222
Last cylinder, +cylinders or +size{K,M,G} (5222-6527, default 6527): 
Using default value 6527

Command (m for help): t
Partition number (1-4): 4
Hex code (type L to list codes): 8e
Changed system type of partition 4 to 8e (Linux LVM)

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.
[root@10 ~]#

 上述命令执行后,可以使用fdisk -l /dev/sda来查看结果,来查看 sda4 是否成功创建并标记为 LVM,如果没有,则需要重新启动。

 

 

[root@10 ~]# fdisk -l /dev/sda

Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 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: 0x0000698e

   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
/dev/sda3            1045        5221    33549074+  8e  Linux LVM
/dev/sda4            5222        6527    10490445   8e  Linux LVM
[root@10 ~]# 

 执行完后重启。

 

调整 LVM 大小

先看看当前 Volume Group:

 

[root@10 ~]# vgdisplay 
  --- Volume group ---
  VG Name               vg_yuxuechengcentos6
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               39.50 GiB
  PE Size               4.00 MiB
  Total PE              10112
  Alloc PE / Size       10112 / 39.50 GiB
  Free  PE / Size       0 / 0   
  VG UUID               8oim1v-kbuK-5RjL-MchZ-PLqp-3Uco-OCTXMu
   
[root@10 ~]# 

名称为 vg_yuxuechengcentos6,可调整大小(resizable),当前大小为39.50GB。

查看活动的 LVM 卷:

[root@10 ~]# lvscan 
  ACTIVE            '/dev/vg_yuxuechengcentos6/lv_root' [37.53 GiB] inherit
  ACTIVE            '/dev/vg_yuxuechengcentos6/lv_swap' [1.97 GiB] inherit
[root@10 ~]# 

 先将分配过来的新磁盘空间创建为一个新的物理卷:

[root@localhost ~]# pvcreate /dev/sda4 
  Writing physical volume data to disk "/dev/sda4"
  Physical volume "/dev/sda4" successfully created
[root@localhost ~]# 

 然后使用新的物理卷来扩展 LVM 的 vg_yuxuechengcentos6:

[root@localhost ~]# vgextend vg_yuxuechengcentos6 /dev/sda4 
  Volume group "vg_yuxuechengcentos6" successfully extended
[root@localhost ~]# 

 然后扩展 LVM 的逻辑卷 vg_yuxuechengcentos6-lv_root:

[root@localhost ~]# lvextend /dev/mapper/vg_yuxuechengcentos6-lv_root /dev/sda4 
  Extending logical volume lv_root to 47.53 GiB
  Logical volume lv_root successfully resized
[root@localhost ~]# 

 最后,调整逻辑卷文件系统的大小,

[root@localhost ~]# resize2fs /dev/mapper/vg_yuxuechengcentos6-lv_root 
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg_yuxuechengcentos6-lv_root is mounted on /; on-line resizing required
old desc_blocks = 3, new_desc_blocks = 3
Performing an on-line resize of /dev/mapper/vg_yuxuechengcentos6-lv_root to 12460032 (4k) blocks.
The filesystem on /dev/mapper/vg_yuxuechengcentos6-lv_root is now 12460032 blocks long.

[root@localhost ~]# 

在CentOS7上执行resize2fs会报错,如下所示,这个的/dev/centos/root和前面不一样,因为这是后面添加的,这是在CentOS7上处理的记录。

[root@localhost linus_dev]# resize2fs /dev/centos/root 
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/centos/root
Couldn't find valid filesystem superblock.

 找不到的原因是CentOS 7中默认的文件系统是XFS,与CentOS 6不同,所以这里需要使用的命令是xfs_growfs,而不是resize2fs,再CentOS执行成功记录如下:

[root@localhost linus_dev]# xfs_growfs /dev/centos/root 
meta-data=/dev/mapper/centos-root isize=256    agcount=4, agsize=1799936 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=7199744, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=3515, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 7199744 to 12441600
[root@localhost linus_dev]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   48G   16G   32G  34% /
devtmpfs                 488M     0  488M   0% /dev
tmpfs                    497M     0  497M   0% /dev/shm
tmpfs                    497M  6.7M  491M   2% /run
tmpfs                    497M     0  497M   0% /sys/fs/cgroup
/dev/sda1                497M  163M  335M  33% /boot
[root@localhost linus_dev]# 

调整完后,看看效果:

[root@localhost ~]# lvscan 
  ACTIVE            '/dev/vg_yuxuechengcentos6/lv_root' [47.53 GiB] inherit
  ACTIVE            '/dev/vg_yuxuechengcentos6/lv_swap' [1.97 GiB] inherit
[root@localhost ~]# 

 

另外,几个有用的 LVM 指令(lvm2):

 

  • lvmdiskscan
  • vgdisplay
  • pvdisplay
  • lvdispay
  • vgcreate
  • pvcreate
  • lvscan
  • pvscan
  • vgscan

我这里用的是 CentOS 系统。如果是 Windows 就更简单了,可通过 Windows 的磁盘管理来重新调整分区大小以占用所有可用磁盘空间。

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics