4 动手写创建虚拟机的脚本
写好的脚本如下(参考: http://www.linuxidc.com/Linux/2012-10/72891.htm ):
创建虚拟机的脚本: vmcre.sh
- #!/bin/bash
- function rm_dir {
- if [ -d "$1" ]; then
- rm -rf $1
- else
- echo "folder not existed: $1"
- fi
- }
- function rm_file {
- if [ -f "$1" ]; then
- rm $1
- else
- echo "file not existed: $1"
- fi
- }
- # check input parameters
- if [ -z $1 ]; then
- echo "Usage: ./vmcre.sh vmname vmip"
- exit 1
- fi
- if [ $1 = "--help" ]; then
- echo "Usage: ./vmcre.sh vmname vmip"
- exit 2
- fi
- if [ -z $2 ]; then
- echo "Usage: ./vmcre.sh vmname vmip"
- echo "ip of vm missed"
- exit 3
- fi
- rm_dir "/var/lib/libvirt/images/$1/"
- rm_file "/etc/libvirt/qemu/$1"
- mkdir -p "/var/lib/libvirt/images/$1/mytemplates/libvirt"
- cp /etc/vmbuilder/libvirt/* /var/lib/libvirt/images/$1/mytemplates/libvirt/
- cp ./vm.partition /var/lib/libvirt/images/$1/
- cd /var/lib/libvirt/images/$1/
- <SPAN style="COLOR: #ff0000"># you should change below line for you</SPAN>
- echo `vmbuilder kvm Ubuntu --suite=quantal --flavour=virtual --arch=amd64
- --proxy=http://10.112.18.178:3142 -o --libvirt=qemu:///system --ip=$2 --gw=10.112.18.254 --dns=8.8.8.8
- --bcast=10.112.18.255 --mask=255.255.255.0
- --net=10.112.18.0 --part=vm.partition --templates=mytemplates
- --user=car --name=car --pass=abc123 --addpkg=openssh-server --mem=2048 --hostname=$1 --bridge=br0 --debug --verbose`
- echo `virsh define /etc/libvirt/qemu/$1.xml`
- echo `virsh start $1`
注意到你在使用这个脚本的时候要修改一些参数, 我用红色标记出来. 你也可以完善这个脚本以让它更加通用.
# you should change below line for you
下面的内容要写在一行里, 用空格隔开.
虚拟机的分区脚本: vm.patition
- root 24000
- swap 2000
- ---
- /var 8000
这个脚本被vmcre.sh调用, 其中的"---"表示分为2个分区, root分区24GB, swap分区2GB, /var分区8GB. 你可以更改为适合你的机器.
5 创建虚拟机
#./vmcre.sh vm1 10.112.18.191
# ./vmcre.sh vm2 10.112.18.192
# ./vmcre.sh vm3 10.112.18.193
创建好之后虚拟机立即启动.
查看所有虚拟机
# virsh list --all
启动虚拟机
# virsh start vm1
修改修改vm1虚拟机参数, 修改下面的文件即可:
/etc/libvirt/qemu/vm1.xml
好了, 创建虚拟机就是这么简单. 一定要在bios里启用硬件支持。
