XCP/XenServer命令行方式自动化安装VM


背景介绍:
前面我的一篇文章《XCP/XenServer自动化创建并初始化虚拟机》中,讲解了如何通过传递内核参数,来克隆VM并以新的主机名,IP地址等进行初始化。
但是在这之前其实应该还有另外一个问题,就是第一个VM怎样才能自动化创建呢?为了解决这个问题,我咨询了不少的人,但是他们大多数给我的回答的都是,使用XenCenter。
但这并不是我们想要的,经过不断的试错之后,终于成功的完成了第一个VM的自动化安装,其实原理也很简单,就是通过命令行将VM初始化,绑定虚拟硬盘,网络,然后通过httprepo来网络加载ISO,再配合kickstart进行自动化安装。但是在很多具体的步骤上,却又是步步陷阱,一些很小的参数方面设置不正确都会导致失败,而这些,在官方文档中基本上算是被忽略了的。
下面是相关的具体步骤:

环境介绍:
httprepo: 192.168.92.128
xenserver: 192.168.92.140
vm: 192.168.92.142

1. 创建 http repo
# yum install httpd
# wget http://linux.mirrors.es.net/centos/6/isos/x86_64/CentOS-6.4-x86_64-minimal.iso

# mkdir -p /var/www/html/repo/centos/6
# mkdir /var/www/html/repo/ks
# mount -o loop CentOS-6.4-x86_64-minimal.iso /var/www/html/repo/centos/6/

# service httpd start

2. 创建 kickstart 配置文件
# vi /var/www/html/repo/ks/centos-6.4-x86_64-minimal.ks

 
cmdline
skipx
install
cdrom
lang en_US.UTF-8
keyboard us

network --onboot yes --device eth0 --bootproto=static --ip=192.168.92.142 --netmask=255.255.255.0 --gateway=192.168.92.2 --nameserver=192.168.92.2 --noipv6

rootpw mypasswd

firewall --service=ssh
authconfig --enableshadow --passalgo=sha512
selinux --disabled
timezone --utc Etc/UTC

bootloader --location=mbr --driveorder=xvda --append="crashkernel=auto"

zerombr
clearpart --all --initlabel
autopart

reboot

%packages --nobase
@core
%end

3. 自动安装VM
获取Local storage uuid
# xe sr-list | grep -C 1 "Local"

                uuid ( RO): fbeda99f-b5a7-3100-5e3d-fbb48a46fca0
          name-label ( RW): Local storage
    name-description ( RW): 


初始化一个空的VM
# xe vm-install new-name-label=centos6_template sr-uuid=fbeda99f-b5a7-3100-5e3d-fbb48a46fca0 template=Other\ install\ media

2fe3c706-9506-50d5-a557-0d61ebde651b

设置VM的CPU,内存
# xe vm-param-set VCPUs-max=1 uuid=2fe3c706-9506-50d5-a557-0d61ebde651b
# xe vm-param-set VCPUs-at-startup=1 uuid=2fe3c706-9506-50d5-a557-0d61ebde651b

# xe vm-param-set memory-dynamic-max=512MiB uuid=2fe3c706-9506-50d5-a557-0d61ebde651b
# xe vm-param-set memory-static-max=512MiB uuid=2fe3c706-9506-50d5-a557-0d61ebde651b
# xe vm-param-set memory-dynamic-min=512MiB uuid=2fe3c706-9506-50d5-a557-0d61ebde651b
# xe vm-param-set memory-static-min=512MiB uuid=2fe3c706-9506-50d5-a557-0d61ebde651b

为自动化安装VM设置bootloader,httprepo,kickstart
# xe vm-param-set HVM-boot-policy="" uuid=2fe3c706-9506-50d5-a557-0d61ebde651b
# xe vm-param-set PV-bootloader="eliloader" uuid=2fe3c706-9506-50d5-a557-0d61ebde651b
# xe vm-param-set other-config:install-repository="http://192.168.92.128/repo/centos/6/" uuid=2fe3c706-9506-50d5-a557-0d61ebde651b
# xe vm-param-set PV-args="ip=192.168.92.142 netmask=255.255.255.0 gateway=192.168.92.2 ns=192.168.92.2 noipv6 ks=http://192.168.92.128/repo/ks/centos-6.4-x86_64-minimal.ks ksdevice=eth0" uuid=2fe3c706-9506-50d5-a557-0d61ebde651b

为VM添加一块虚拟硬盘
# xe vm-disk-add uuid=2fe3c706-9506-50d5-a557-0d61ebde651b sr-uuid=fbeda99f-b5a7-3100-5e3d-fbb48a46fca0 device=0 disk-size=20GiB

设置虚拟硬盘为bootable
# xe vbd-list vm-uuid=2fe3c706-9506-50d5-a557-0d61ebde651b userdevice=0 params=uuid --minimal

d304bbbd-f4e2-d648-a668-fe6a803bc301

# xe vbd-param-set bootable=true uuid=d304bbbd-f4e2-d648-a668-fe6a803bc301

为VM创建网络
# xe network-list bridge=xenbr0 --minimal

a6fcd4a1-fb61-6f73-2b31-2a20ad45e0cc

# xe vif-create vm-uuid=2fe3c706-9506-50d5-a557-0d61ebde651b network-uuid=a6fcd4a1-fb61-6f73-2b31-2a20ad45e0cc mac=random device=0

aaf0a04d-c721-fae8-aca1-eb63e047ea93

启动VM,接下来,VM将自动化安装好所有的基础软件包,并启动SSH服务
# xe vm-start uuid=2fe3c706-9506-50d5-a557-0d61ebde651b

4. 等待大约20分钟左右,登陆VM
# ssh root@192.168.92.142

The authenticity of host '192.168.92.142 (192.168.92.142)' can't be established.
RSA key fingerprint is d5:de:ac:6a:7f:33:0c:ca:84:aa:d5:62:43:d2:9a:23.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.92.142' (RSA) to the list of known hosts.
root@192.168.92.142's password: mypasswd
Last login: Thu Oct 10 22:17:48 2013 from 192.168.92.140
[root@localhost ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog
[root@localhost ~]# 

, , ,

  1. No comments yet.
(will not be published)
*