调整 Xen 虚拟硬盘大小

今天有人跑来实验室抱怨说给他们配置的 4GB Xen 虚拟硬盘太小,系统占了 2GB,再装一些工具,比如 jdk/mysql/tomcat/red5/asterisk 之类的空间就不够了。如何增加 Xen 虚拟硬盘的大小呢,也就是说如何扩充 Xen 的镜像文件大小呢?(这里的方法适用于镜像文件在 ext2 和 ext3 文件系统的情况)

关闭虚拟机:

# /usr/sbin/xm shutdown vm01

给虚拟机镜像文件(.img)追加 4GB 空间:

# dd if=/dev/zero bs=1024k count=4096 >> /vm/vm01.img

扫描检查镜像文件:

# /sbin/e2fsck -f /vm/vm01.img

这个时候只是增加了镜像文件(硬盘)的大小,这个镜像文件不是普通的文件,里面包含可 mount/umount 的 loop 文件系统,所以需要调整文件系统大小,不然的话进入虚拟机后 df 会发现硬盘大小没变:

# /sbin/resize2fs /vm/vm01.img

重新启动 Xen 虚拟机:

# /usr/sbin/xm create vm01

进入虚拟机后查看硬盘大小:

# /usr/sbin/xm console vm01
# df -h

这里提到了 mount/umount,说一点题外话,从技术角度来说现在的 VPS 其实都不安全,因为 VPS 服务商随时可以 mount/umount 你的虚拟机文件、分区来读取、甚至写入内容,如果你的虚拟机所在的那台服务器的 root 密码被坏人拿到,那就毫无安全可言了,坏人可以随意在你虚拟机上做手脚。所以说安全性是云计算的一个大问题,尤其是那些搭建在虚拟技术上的云计算,这也可能是企业迟迟不愿意使用公有云的原因吧,一些 Startup 也只是把不重要的数据放在类似 Amazon S3 之类的云存储上,对于机密数据还是自己保存比较好。所以 VPSee 认为私有云会有更大的发展空间。

评论 (10 Comments)

  1. 您好,按照您的操作步骤,在该步出错,请指点:
    /sbin/e2fsck -f /xenos/test2.img
    e2fsck 1.39 (29-May-2006)
    Couldn’t find ext2 superblock, trying backup blocks…
    /sbin/e2fsck: Bad magic number in super-block while trying to open /xenos/test2.img

    The superblock could not be read or does not describe a correct ext2
    filesystem. If the device is valid and it really contains an ext2
    filesystem (and not swap or ufs or something else), then the superblock
    is corrupt, and you might try running e2fsck with an alternate superblock:
    e2fsck -b 8193

  2. 请确定你的 test2.img 里只含有1个分区,并且文件系统是 ext2/ext3 的。

  3. 我的系统是centos5.4的,按你的方法也不行。
    e2fsck -b 8193 ossvr-1.img
    e2fsck 1.39 (29-May-2006)
    e2fsck: Bad magic number in super-block while trying to open ossvr-1.img

    The superblock could not be read or does not describe a correct ext2
    filesystem. If the device is valid and it really contains an ext2
    filesystem (and not swap or ufs or something else), then the superblock
    is corrupt, and you might try running e2fsck with an alternate superblock:
    e2fsck -b 8193

    resize2fs -f ossvr-1.img
    resize2fs 1.39 (29-May-2006)
    resize2fs: Bad magic number in super-block while trying to open ossvr-1.img
    Couldn’t find valid filesystem superblock.

  4. 1. 关闭虚拟机
    $ xm shutdown myvm1.faladay.cublog.cn
    2. 增加虚拟磁盘文件空间(4G)
    $ dd if=/dev/zero bs=1024k count=4096 >> /home/myvm/myvm1.faladay.cublog.cn.img
    3. 重新创建虚拟磁盘分区表
    $ losetup -f
    /dev/loop0
    $ losetup /dev/loop0 /home/myvm/myvm1.faladay.cublog.cn.img
    $ losetup -a
    /dev/loop0: [fd01]:10977283 (/home/myvm/myvm1.faladay.cublog.cn.img)
    $ fdisk /dev/loop0
    下面是fdisk界面的输入和输出信息(摘要):
    Command (m for help): p
    Device Boot Start End Blocks Id System
    /dev/loop0p1 * 1 6 48163+ 83 Linux
    /dev/loop0p2 7 764 6088635 8e Linux LVM
    Command (m for help): d
    Partition number (1-4): 2
    Command (m for help): n
    Command action
    e extended
    p primary partition (1-4)
    p
    Partition number (1-4): 2
    First cylinder (7-1281, default 7):
    Using default value 7
    Last cylinder or +size or +sizeM or +sizeK (7-1281, default 1281):
    Using default value 1281
    Command (m for help): t
    Partition number (1-4): 2
    Hex code (type L to list codes): 8e
    Changed system type of partition 2 to 8e (Linux LVM)
    Command (m for help): p
    /dev/loop0p1 * 1 6 48163+ 83 Linux
    /dev/loop0p2 7 1281 10241437+ 8e Linux LVM
    Command (m for help): w

    4. 以root用户登录虚拟机
    $ xm create myvm1.faladay.cublog.cn
    $ ssh root@myvm1.faladay.cublog.cn

    5. 修改LVM物理分卷大小
    $ pvscan
    PV /dev/xvda2 VG sys lvm2 [5.75 GB / 0 GB free]
    Total: 1 [5.75 GB] / in use: 1 [5.75 GB] / in no VG: 0 [0 ]
    $ pvresize +4096M /dev/xvda2
    $ pvscan
    PV /dev/xvda2 VG sys lvm2 [9.75 GB / 3.97 GB free]
    Total: 1 [9.75 GB] / in use: 1 [9.75 GB] / in no VG: 0 [0 ]
    $ lvscan
    ACTIVE ‘/dev/sys/root’ [4.00 GB] inherit
    ACTIVE ‘/dev/sys/home’ [1.28 GB] inherit
    ACTIVE ‘/dev/sys/swap’ [512.00 MB] inherit
    $ lvextend -L +1024M /dev/sys/root
    $ lvextend -L +3072M /dev/sys/home
    $ resize2fs -f /dev/sys/root
    $ resize2fs -f /dev/sys/home

    6. 退出虚拟机,执行下列命令
    $ losetup -d /dev/loop0

    至此,虚拟机磁盘扩容完毕,通过df -h命令可以看到虚拟机磁盘空间root已经扩大1G,home已经扩大3G。

  5. 我是这样做的:

    dd if=/dev/zero of=vm01.img bs=1 count=1 seek=20G conv=notrunc

    e2fsck -f vm01.img -y

    resize2fs -p vm01.img

  6. 请问下,windows磁盘如何动态增加大小

  7. 请问缩小用什么命令

  8. windows磁盘可以先按前面dd加容量后,在虚拟机里起深山红叶这种工具盘修复分区就可以了,更方便。

  9. 对于ext2/ext3 使用
    e2fsck -f /dev/xxx
    resize2fs /dev/xxx

    对于ext4 使用

    e4fsck -f /dev/xxx
    resize4fs /dev/xxx

  10. win的话 用virt-resize也可以的。或者使用guestfish 可以操作

发表评论