本文共 9320 字,大约阅读时间需要 31 分钟。
在宿主机上创建一个虚拟机:操作系统,debian7,内存2G,磁盘30G(lvm卷).swap 2G(kvm环境可参考http://my.oschina.net/davehe/blog/92170)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | root @ubuntu :/opt# virt-install -d -n debian7_201 -r 2048 --disk path=/dev/vm/vm201 --network bridge=br0 --vnc --cdrom=/opt/debian- 7.0 . 0 -amd64-CD- 1 .iso [Fri, 10 May 2013 16 : 46 : 15 virt-install 5886 ] DEBUG (cli: 220 ) Launched with command line: /usr/bin/virt-install -d -n debian7_201 -r 2048 --disk path=/dev/vm/vm201 --network bridge=br0 --vnc --cdrom=/opt/debian- 7.0 . 0 -amd64-CD- 1 .iso [Fri, 10 May 2013 16 : 46 : 15 virt-install 5886 ] DEBUG (cli: 325 ) Requesting libvirt URI default [Fri, 10 May 2013 16 : 46 : 15 virt-install 5886 ] DEBUG (cli: 327 ) Received libvirt URI qemu: ///system [Fri, 10 May 2013 16 : 46 : 15 virt-install 5886 ] DEBUG (virt-install: 259 ) Requesting virt method 'default' , hv type 'default' . [Fri, 10 May 2013 16 : 46 : 15 virt-install 5886 ] DEBUG (virt-install: 469 ) Received virt method 'hvm' [Fri, 10 May 2013 16 : 46 : 15 virt-install 5886 ] DEBUG (virt-install: 470 ) Hypervisor name is 'kvm' [Fri, 10 May 2013 16 : 46 : 15 virt-install 5886 ] DEBUG (cli: 952 ) --graphics compat generated: vnc [Fri, 10 May 2013 16 : 46 : 16 virt-install 5886 ] DEBUG (DistroInstaller: 209 ) DistroInstaller location is a local file/path: /opt/debian- 7.0 . 0 -amd64-CD- 1 .iso [Fri, 10 May 2013 16 : 46 : 16 virt-install 5886 ] DEBUG (virt-install: 624 ) Guest.has_install_phase: True Starting install... [Fri, 10 May 2013 16 : 46 : 16 virt-install 5886 ] DEBUG (Guest: 1293 ) Generated install XML: ... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | root @10 .1. 6.201 :~# fdisk -l Disk /dev/sda: 32.2 GB, 32212254720 bytes 255 heads, 63 sectors/track, 3916 cylinders, total 62914560 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: 0x000a9969 Device Boot Start End Blocks Id System /dev/sda1 * 2048 58984447 29491200 83 Linux /dev/sda2 58984448 62912511 1964032 82 Linux swap / Solaris root @10 .1. 6.201 :~# df -h Filesystem Size Used Avail Use% Mounted on rootfs 28G 808M 26G 4 % / udev 10M 0 10M 0 % /dev tmpfs 202M 184K 202M 1 % /run /dev/disk/by-uuid/c75048b2-daf9- 4464 -98c2-d6e42428d628 28G 808M 26G 4 % / tmpfs 5 .0M 0 5 .0M 0 % /run/lock tmpfs 787M 0 787M 0 % /run/shm |
若要为虚拟机增加一块磁盘
最直接就是给虚拟机增加一块磁盘,然后直接格式化挂载到某个目录,这样硬盘空间就扩展了.
这里使用img镜像和lvm卷添加都可以,lvm卷当然在效率上更高.
修改配置文件:
1 2 3 4 5 6 7 8 9 10 11 12 | root @ubuntu :/etc/libvirt/qemu# vim debian7_201.xml 29 <disk type= 'block' device= 'disk' > 30 <driver name= 'qemu' type= 'raw' /> 31 <source dev= '/dev/vm/vm201' /> 32 <target dev= 'hda' bus= 'ide' /> 33 <address type= 'drive' controller= '0' bus= '0' target= '0' unit= '0' /> 34 </disk> 35 <disk type= 'block' device= 'disk' > #增加卷vm23 36 <driver name= 'qemu' type= 'raw' /> 37 <source dev= '/dev/vm/vm23' /> 38 <target dev= 'hdb' bus= 'ide' /> #注意硬盘符hdb 39 </disk> |
1 2 3 4 5 6 7 8 9 10 11 12 13 | root @ubuntu :/etc/libvirt/qemu# virsh virsh # define debian7_201.xml #注意使用undefine会删除与客户端关联的文件,先备份 Domain debian7_201 defined from debian7_201.xml virsh # create debian7_201.xml Domain debian7_201 created from debian7_201.xml virsh # start debian7_201 virsh # list Id Name State ---------------------------------------------------- 6 debian7_201 running |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | root @10 .1. 6.201 :~# fdisk -l Disk /dev/sda: 32.2 GB, 32212254720 bytes 255 heads, 63 sectors/track, 3916 cylinders, total 62914560 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: 0x000a9969 Device Boot Start End Blocks Id System /dev/sda1 * 2048 58984447 29491200 83 Linux /dev/sda2 58984448 62912511 1964032 82 Linux swap / Solaris Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders, total 41943040 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: 0x00000000 Disk /dev/sdb doesn't contain a valid partition table |
当然也可以用创建一个虚拟机磁盘镜像,在修改配置文件写名路径等,剩下操作同理.
1 2 | root @ubuntu :/opt# kvm-img create -f qcow2 vm.img 5G Formatting 'vm.img' , fmt=qcow2 size= 5368709120 encryption=off cluster_size= 65536 lazy_refcounts=off |
1 2 3 4 5 6 7 8 9 10 11 12 13 | root @ubuntu :/etc/libvirt/qemu# vim debian7_201.xml 29 <disk type= 'block' device= 'disk' > 30 <driver name= 'qemu' type= 'raw' /> 31 <source dev= '/dev/vm/vm201' /> 32 <target dev= 'hda' bus= 'ide' /> 33 <address type= 'drive' controller= '0' bus= '0' target= '0' unit= '0' /> 34 </disk> 35 <disk type= 'file' device= 'disk' > #注意type为file 36 <driver name= 'qemu' type= 'qcow2' /> 37 <source file= '/opt/vm.img' /> #source为file 38 <target dev= 'hdb' bus= 'ide' /> 39 <address type= 'drive' controller= '0' bus= '0' target= '0' unit= '1' /> 40 </disk> |
1 2 3 4 5 6 7 8 9 10 11 12 | virsh # define debian7_201.xml Domain debian7_201 defined from debian7_201.xml virsh # create debian7_201.xml Domain debian7_201 created from debian7_201.xml virsh # start debian7_201 virsh # list Id Name State ---------------------------------------------------- 8 debian7_201 running |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | root @10 .1. 6.201 :~# fdisk -l Disk /dev/sda: 32.2 GB, 32212254720 bytes 255 heads, 63 sectors/track, 3916 cylinders, total 62914560 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: 0x000a9969 Device Boot Start End Blocks Id System /dev/sda1 * 2048 58984447 29491200 83 Linux /dev/sda2 58984448 62912511 1964032 82 Linux swap / Solaris Disk /dev/sdb: 5368 MB, 5368709120 bytes #这就是我们在宿主机上创建的vm.img镜像 255 heads, 63 sectors/track, 652 cylinders, total 10485760 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: 0x00000000 Disk /dev/sdb doesn't contain a valid partition table |
以上方法需要重启虚拟机才能生效,还有一种方法可以动态扩展虚拟机磁盘.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | root @ubuntu :/etc/libvirt/qemu# lvs LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert vm200 vm -wi-ao-- 5 .00g vm201 vm -wi-ao-- 30 .00g vm_21 vm -wi-ao-- 20 .00g vm_22 vm -wi-a--- 20 .00g virsh # attach-disk debian7_201 /dev/vm/vm_21 sdc Disk attached successfully root @10 .1. 6.201 :~# fdisk -l Disk /dev/sda: 32.2 GB, 32212254720 bytes 255 heads, 63 sectors/track, 3916 cylinders, total 62914560 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: 0x000a9969 Device Boot Start End Blocks Id System /dev/sda1 * 2048 58984447 29491200 83 Linux /dev/sda2 58984448 62912511 1964032 82 Linux swap / Solaris Disk /dev/sdb: 5368 MB, 5368709120 bytes 255 heads, 63 sectors/track, 652 cylinders, total 10485760 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: 0x00000000 Disk /dev/sdb doesn't contain a valid partition table Disk /dev/sdc: 21.5 GB, 21474836480 bytes #宿主机上创建的vm_21卷 64 heads, 32 sectors/track, 20480 cylinders, total 41943040 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: 0x00000000 Disk /dev/sdc doesn't contain a valid partition table |
1 2 | virsh # detach-disk debian7_201 sdc #动态删除虚拟机上的磁盘 Disk detached successfully |
期间我曾使用virtio参数创建虚拟机如下:
1 | root @ubuntu :/opt# virt-install -d -n debian7_201 -r 2048 --disk path=/dev/vm/vm201<span style= "color:#008200;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;line-height:14.652778625488281px;background-color:#FFFFFF;" >,bus=virtio,cache=none</span> --network bridge=br0 --vnc --cdrom=/opt/debian- 7.0 . 0 -amd64-CD- 1 .iso |
virsh是由一个名叫ibvirt的软件提供的管理工具,可以管理kvm及xen等虚拟机
附录:virsh的一些常用命令行选项
help | 打印基本帮助信息。 |
list | 列出所有客户端。 |
dumpxml | 输出客户端 XML 配置文件。 |
create | 从 XML 配置文件生成客户端并启动新客户端。 |
start | 启动未激活的客户端。 |
destroy | 强制客户端停止。 |
define | 为客户端输出 XML 配置文件。 |
domid | 显示客户端 ID。 |
domuuid | 显示客户端 UUID。 |
dominfo | 显示客户端信息。 |
domname | 显示客户端名称。 |
domstate | 显示客户端状态。 |
quit | 退出这个互动终端。 |
reboot | 重新启动客户端。 |
restore | 恢复以前保存在文件中的客户端。 |
resume | 恢复暂停的客户端。 |
save | 将客户端当前状态保存到某个文件中。 |
shutdown | 关闭某个域。 |
suspend | 暂停客户端。 |
undefine | 删除与客户端关联的所有文件。 |
migrate | 将客户端迁移到另一台主机中。 |
setmem | 为客户端设定分配的内存。 |
setmaxmem | 为管理程序设定内存上限。 |
setvcpus | 修改为客户端分配的虚拟 CPU 数目。 |
vcpuinfo | 显示客户端的虚拟 CPU 信息。 |
vcpupin | 控制客户端的虚拟 CPU 亲和性。 |
domblkstat | 显示正在运行的客户端的块设备统计。 |
domifstat | 显示正在运行的客户端的网络接口统计。 |
attach-device | 使用 XML 文件中的设备定义在客户端中添加设备。 |
attach-disk | 在客户端中附加新磁盘设备。 |
attach-interface | 在客户端中附加新网络接口。 |
detach-device | 从客户端中分离设备,使用同样的 XML 描述作为命令attach-device。 |
detach-disk | 从客户端中分离磁盘设备。 |
detach-interface | 从客户端中分离网络接口。 |
转载地址:http://ulsab.baihongyu.com/