原理也很简单,就是通过命令行将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="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 ~]#
下面是相关的具体步骤:
环境介绍:
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="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 ~]#
######################自己环境的一个脚本#########################################
#!/bin/bash
templatenamelabel="centos6_template"
cpu="1"
memory="512MiB"
rootdisk="20GiB"
datadisk="20GiB"
repoiso="http://192.168.22.100/repo/centos6.6/"
repoks="http://192.168.22.100/repo/ks/CentOS-6.6-x86_64.ks"
#get local storage uuid
localsr=`xe sr-list | grep -C 1 "Local" | grep "name-label" | cut -d: -f2 | sed 's/^[ \t]*//g'`
#get Other install media uuid
othertemplate=`xe template-list | grep -C 1 "Other install media" | grep "uuid" | cut -d: -f2 | sed 's/^[ \t]//g'`
# init empty vm
vmuuid=`xe vm-install new-name-label=$templatenamelabel sr-uuid=$localsr template=$othertemplate`
# set vm cpu memory, careful memory-static-max memory-dynamic-max memory-dynamic-min memory-static-min order
# cpu
xe vm-param-set VCPUs-max=$cpu uuid=$vmuuid
xe vm-param-set VCPUs-at-startup=$cpu uuid=$vmuuid
# memory
xe vm-param-set memory-static-max=$memory uuid=$vmuuid
xe vm-param-set memory-dynamic-max=$memory uuid=$vmuuid
xe vm-param-set memory-dynamic-min=$memory uuid=$vmuuid
xe vm-param-set memory-static-min=$memory uuid=$vmuuid
# set vm bootloader,httprepo,kickstart
xe vm-param-set HVM-boot-policy="" uuid=$vmuuid
xe vm-param-set PV-bootloader="eliloader" uuid=$vmuuid
xe vm-param-set other-config:install-repository=$repoiso uuid=$vmuuid
xe vm-param-set PV-args="ks=$repoks ksdevice=eth0" uuid=$vmuuid
# set vm root disk
xe vm-disk-add uuid=$vmuuid sr-uuid=$localsr device=0 disk-size=$rootdisk
# set vm root disk bootable
rootdiskuuid=`xe vbd-list vm-uuid=$vmuuid userdevice=0 params=uuid --minimal`
xe vbd-param-set bootable=true uuid=$rootdiskuuid
# set vm network
networkuuid=`xe network-list bridge=xenbr0 --minimal`
vifuuid=`xe vif-create vm-uuid=$vmuuid network-uuid=$networkuuid mac=random device=0`
# start vm
xe vm-start uuid=$vmuuid