设为首页 收藏本站
查看: 1156|回复: 0

[经验分享] 调整 VirtualBox 虚拟机的磁盘大小

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-1-10 14:19:07 | 显示全部楼层 |阅读模式
调整 VirtualBox 虚拟机的磁盘大小
  使用 VM VirtualBox 创建了一个 CentOS 虚拟机,划分了 40GB 的磁盘空间,使用了动态分配存储的方式,文件系统为 LVM。折腾了一段时间之后,却发现磁盘容量不够了。读了一些资料之后,终于给 VirtualBox 的虚拟机增加了磁盘容量,并让 CentOS 的 LVM 将其利用上。
  先看看虚拟机中虚拟磁盘的的分区表:
  

[iyunv@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
[iyunv@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 管理
  

[iyunv@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
[iyunv@10 ~]#
  重新启动该虚拟机。再来看看磁盘情况:
  
  

[iyunv@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
[iyunv@10 ~]#
  现在,磁盘大小为 53680MB。磁盘占用情况为,可以看到总大小还是40G的配置。
  
  

[iyunv@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
[iyunv@10 ~]#
  试试看用dd命令创建一个30G大小的块文件:
  

[iyunv@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
[iyunv@10 ~]#
  
  提示说磁盘空间不足(No space left on device)。虽然我们给了虚拟机足够的空间,但是虚拟机里的系统(这里是 CentOS)并没有将这一部分空间利用上。可以看到,虚拟机里的磁盘空间只有 37G,原始分配的 50G 没有用完。这同时也说明,即使创建虚拟机的时候使用动态分配磁盘大小,虚拟机里的系统也并不会自动修改自己的磁盘。如果是正常使用,即使虚拟机的虚拟磁盘 足够大,还是可能会在虚拟机中遇到磁盘空间不足的警告。
  
  下面调整虚拟机操作系统的文件系统,使之将所有空间利用上。

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

[iyunv@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.
[iyunv@10 ~]#
  上述命令执行后,可以使用fdisk -l /dev/sda来查看结果,来查看 sda4 是否成功创建并标记为 LVM,如果没有,则需要重新启动。
  
  

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

  执行完后重启。
  

调整 LVM 大小
  先看看当前 Volume Group:
  

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

  名称为 vg_yuxuechengcentos6,可调整大小(resizable),当前大小为39.50GB。
  查看活动的 LVM 卷:

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

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

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

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

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

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

[iyunv@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
[iyunv@localhost ~]#

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

[iyunv@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.
[iyunv@localhost ~]#

  调整完后,看看效果:

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

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


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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-162585-1-1.html 上篇帖子: [转]给VirtualBox虚拟机扩容 下篇帖子: 【转】VirtualBox的数据空间使用方法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表