KVM安装之脚本和镜像目录树准备

1.php脚本需要先安装PHP环境,Apache服务器必须支持PHP

$ yum install -y php    #安装PHP
$ php -v                      #查看是否安装PHP
yum -y install httpd    #安装Apache服务器
service httpd restart    #启动Apache
把脚本放到/var/www/html/下
centos下挂在ISO镜像
ks下放:
centos65_kvm.cfg
ks.php
挂在ISO镜像目录树
$ mount -o loop -t iso9660 /home/backup/CentOS-6.5-x86_64-bin-DVD1.iso /var/www/html/centos/6/os/x86_64
修改本地yum源,让安装虚拟机时从此yum源来下载软件:
$ vim /etc/centos65.repo
[c6-media]
name=CentOS-$releasever -Media
baseurl=http://192.168.10.10/centos/6/os/x86_64
gpgcheck=0
enabled=1
把其他默认的yum源都本分到bak下.
文件内容:
centos65_kvm.cfg
install
url --url="http://172.29.1.4/centos/6/os/x86_64/"
rootpw --iscrypted $1$J2XgJk6a$roQYKKcP7NzFfhvfb74XD.
auth --useshadow --passalgo=sha512
text
keyboard us
lang en_US
firewall --disabled
selinux --disabled
skipx
logging --level=info
reboot
timezone Asia/Shanghai
network --bootproto=dhcp --device=em1 --onboot=on
bootloader --location=mbr
zerombr
clearpart --all --initlabel
part /boot --asprimary --fstype="ext4"--size=512
part swap --fstype="swap"--size=16384
part /--fstype="ext4"--size=51200
part /opt --fstype="ext4"--grow --size=1
%post
repoip=172.29.1.4
mkdir /etc/yum.repos.d/bak
mv /etc/yum.repos.d/*repo /etc/yum.repos.d/bak/
cat >/etc/yum.repos.d/centos65.repo <<EOF
[Base]
name=centos65_x86_64 -Base
baseurl=http://$repoip/centos/6/os/x86_64
enable=1
gpgcheck=0
EOF
yum -y install kvm python-virtinst libvirt tunctl bridge-utils virt-manager qemu-kvm-tools virt-viewer virt-v2
hwclock --set--date="`date +%D %T`"
hwclock --hctosys
sed -i 's/1024/102400/'/etc/security/limits.d/90-nproc.conf
sed -i 's#exec /sbin/shutdown -r now##exec /sbin/shutdown -r now#'/etc/init/control-alt-delete.conf
>/etc/sysconfig/i18n
cat >>/etc/sysconfig/i18n << EOF
LANG="en_US.UTF-8"
EOF
sed -i 's/^GSSAPIAuthentication yes$/GSSAPIAuthentication no/'/etc/ssh/sshd_config
sed -i 's/#UseDNS yes/UseDNS no/'/etc/ssh/sshd_config
service sshd restart
%packages
@base
%end
ks.php
<?php
if($_GET){
header("Content-Type: text/plain");
define(HTTP_URL_BASE,"http://172.29.1.4");
$os_base = HTTP_URL_BASE ."/centos/6/os/x86_64";
$src_base = HTTP_URL_BASE ."/src";
$os_hostname = $_GET["hostname"];
if(!$os_hostname ){
$os_hostname ="localhost.localdomain";
}
$os_password = $_GET["pw"];
if(!$os_password ){
$os_password ="zgtx123.com";
}
#$os_version = $_GET["ver"];
#if( !$os_version ){
# $os_version = "6.5";
#}
$net_ipaddr_eth0 = $_GET["ip1"];
$net_ipaddr_eth1 = $_GET["ip2"];
if(!$net_ipaddr_eth1 ){
$remark="#";
}
$net_netmask = $_GET["mask"];
if(!$net_netmask ){
$net_netmask ="255.255.255.0";
}
$net_gateway = $_GET["gw"];
if(!$net_gateway){
$net_gateway ="172.29.1.254";
}
$net_gateway2 = $_GET["gw2"];
if(!$net_gateway2){
$net_gateway2 ="172.29.1.254";
}
$net_nameservers = $_GET["ns"];
if(!$net_nameservers){
$net_nameservers ="1.2.4.8,114.114.114.114";
}
$fs_type = $_GET["fs-type"];
$fs_auto_detect = false;
if(!$fs_type ){
$fs_type ="ext4";
$fs_auto_detect = true;
}
$disk_name = $_GET["disk"];
if(!$disk_name ){
$disk_name ="vda";
}
$boot_size = $_GET["boot-size"];
if(!$boot_size ){
$boot_size =512;
}
$swap_size = $_GET["swap-size"];
if(!$swap_size ){
$swap_size =1024*4;
}
?>
install
text
skipx
firewall --disabled
selinux --disabled
timezone Asia/Shanghai
key --skip
url --url=<?php echo $os_base ."
";?>
rootpw <?php echo $os_password ."
";?>
authconfig --enableshadow --enablemd5
keyboard us
lang en_US
logging --level=info
network --bootproto=static --device=eth0 --gateway=<?php echo $net_gateway;?>--ip=<?php echo $net_ipaddr_eth0;?>--nameserver=<?php echo $net_nameservers;?>--netmask=<?php echo $net_netmask;?>--hostname=<?php echo $os_hostname;?>--onboot=on
#<?php echo $remark;?>network --bootproto=static --device=eth1 --ip=<?php echo $net_ipaddr_eth1;?> --netmask=<?php echo $net_netmask;?> --onboot=on
bootloader --location=mbr <?php echo $disk_name ."
";?>
zerombr
clearpart --all --initlabel --drives=<?php echo $disk_name ."
";?>
part /boot --asprimary --fstype="<?php echo $fs_type; ?>"--size="<?php echo $boot_size; ?>"--ondisk=<?php echo $disk_name ."
";?>
part swap --fstype="swap"--size="<?php echo $swap_size; ?>"--ondisk=<?php echo $disk_name ."
";?>
part /--fstype="<?php echo $fs_type; ?>"--grow --size=1--ondisk=<?php echo $disk_name ."
";?>
reboot
%post
#!/bin/bash
############################# disable ipv6 and iptables ######################################
rm -f /etc/sysconfig/iptables
rm -f /etc/sysconfig/ip6tables
echo "NETWORKING_IPV6=no">>/etc/sysconfig/network
echo "alias net-pf-10 off">>/etc/modprobe.d/dist.conf
echo "alias ipv6 off">>/etc/modprobe.d/dist.conf
chkconfig iptables off
chkconfig ip6tables off
############################# config yum #######################################################
mkdir /etc/yum.repos.d/bak
mv /etc/yum.repos.d/*repo /etc/yum.repos.d/bak/
cat >/etc/yum.repos.d/centos6-init.repo<<EOF
[base]
name=centos6
baseurl=http://172.29.1.4/centos/6/os/x86_64/
enable=1
gpgcheck=0
EOF
yum -y install openssh-clients openssl openssl-d* vim wget gcc gcc-c++ make autoconf nfs-utils ntpdate zip unzip lsof
yum clean all
###############################/etc/rc.local################################
cat >>/etc/rc.local<<EOF
mount 172.29.1.4:/opt/mirrors/src /opt/share_dir
EOF
mkdir -p /opt/share_dir
mount 172.29.1.4:/opt/mirrors/src /opt/share_dir
echo "* soft nofile 4096">>/etc/security/limits.conf
echo "* hard nofile 4096">>/etc/security/limits.conf
################################ ntp ###############################
#echo "1 * * * * /usr/sbin/ntpdate time.nist.gov" >/var/spool/cron/root
hwclock --set--date="`date +%D %T`"
hwclock --hctosys
############################### turnoff the control-alt-delete ####################
sed -i 's#exec /sbin/shutdown -r now##exec /sbin/shutdown -r now#'/etc/init/control-alt-delete.conf
############################### set ssh ########################################
sed -i 's/^GSSAPIAuthentication yes$/GSSAPIAuthentication no/'/etc/ssh/sshd_config
sed -i 's/#UseDNS yes/UseDNS no/'/etc/ssh/sshd_config
mkdir -p /root/.ssh/
################################ config zabbix ############################################################
#
#cat >/usr/local/etc/zabbix_agentd.conf<<EOF
#<?php echo 'SourceIP='.$net_ipaddr_eth0;echo "
";?>
#Server=127.0.0.1,172.29.1.4
#ListenPort=10050
#ServerActive=172.29.1.4:10051
#<?php echo 'Hostname='.$os_hostname;echo "
"?>
#UnsafeUserParameters=1
#EOF
#
#cp /opt/share_dir/ks/zabbix_agentd /bin/
#chmod +x /bin/zabbix_agentd
############################### config init(/etc/profile /etc/sysctl.conf) ##########################
cat /opt/share_dir/ks/profile >/etc/profile
cat /opt/share_dir/ks/sysctl.conf >/etc/sysctl.conf
################################ deploy app ##########################################################
#!/bin/bash
mkdir -p /opt/server/logs
tar zxf /opt/share_dir/ks/jdk18.tar.gz -C /usr/local/
tar zxf /opt/share_dir/ks/tomcat.tar.gz -C /opt/server/
source /etc/profile
cd /opt/
tar zxf /opt/share_dir/ks/apr-1.4.5.tar.gz -C /opt/
cd /opt/apr-1.4.5
./configure --prefix=/usr/local/apr
make
make install
rm -rf /opt/apr-1.4.5
cd /opt/
tar zxf /opt/share_dir/ks/apr-iconv-1.2.1.tar.gz -C /opt/
cd /opt/apr-iconv-1.2.1
./configure --prefix=/usr/local/apr-iconv --with-apr=/usr/local/apr
make
make install
rm -rf /opt/apr-iconv-1.2.1
cd /opt/
tar zxf /opt/share_dir/ks/apr-util-1.3.12.tar.gz -C /opt/
cd /opt/apr-util-1.3.12
./configure --prefix=/usr/local/apr-iconv --with-apr=/usr/local/apr
make
make install
rm -rf /opt/apr-util-1.3.12
cd /opt/server/tomcat/bin/tomcat-native-1.1.27-src/jni/native
./configure --with-apr=/usr/local/apr --with-java-home=/usr/local/jdk/
make
make install
%packages --nobase
@core
%end
<?php
}else{
?>
Usage:http://172.29.1.4/ks/ks.php?hostname=VM-XX-XX&pw=123456&ip1=x.x.x.x&ip2=x.x.x.x&mask=x.x.x.x&gw=x.x.x.x&ns=x.x.x.x,x.x.x.x&fs-type=ext4&disk=vda&boot-size=500&swap-size=4096
<?php
}
?>
博采众长才能相互印证,故步自封必将粗陋浅薄!
原文地址:https://www.cnblogs.com/tangwan/p/5388643.html