下载自动生成安装脚本的软件
配置好yum源,这个之前的章节已经写过了,不清楚的可以参考之前的文章。
Linux之软件安装篇
yum install system-config-kickstart -y ##下载软件
system-config-kickstart ##运行软件,定制系统
vim ks.cfg ##编辑软件生成文件,自定义安装包
ksvalidator ks.cfg ##检查脚本是否有语法错误
yum install httpd -y ##下载httpd
systemctl start httpd ##启动httpd
systemctl enable httpd ##设置httpd开机启动
systemctl stop firewalld ##关闭防火墙
systemctl disable firewalld ##设置防火墙开机不启动
cp ks.cfg /var/www/html/ks.cfg ##把脚本放到httpd的共享文件夹里
在需要自动安装虚拟机的主机上,写一个以.sh结尾的脚本,详情可以参考之前的文章。
Linux之系统虚拟机管理-6、虚拟机有关脚本 这里列出,我自己写的脚本,仅供参考。
#!/bin/bash ##脚本运行的环境
virt-install \ ##虚拟机安装
--name $1 \ ##虚拟机名称
--memory 1000 \ ##内存大小
--vcpus 1 \ ##cpu个数
--disk /var/lib/libvirt/images/$1.qcow2,size=8,bus=virtio \ ##硬盘
--network bridge=br0,model=virtio \ ##网络设置
--location http://172.25.254.133/shop \ ##虚拟机安装镜像
--extra-args "ks=http://172.25.254.133/ks/ks.cfg" &> /dev/null & ##安装过程有关设置脚本
sh 脚本名 虚拟机名称 ##执行脚本
示例:
system-config-kickstart
这里设置键盘类型,语言,时区,及管理员密码,系统硬盘类型,安装后重启等。
这里设置新安装,还有安装的镜像。
引导程序有关设置
设置硬盘分区,这里我设置/boot分区200M,swap分区500M,/分区。
设置网络类型,我们设置DHCP,关于DHCP服务的搭建可以参考。
接下来三步我们可以选择默认。
这里我们是写入系统安装好后的任务,这里我们配置好yum源和下载好httpd软件。
File–>save 将脚本保存下来。
vim vim ks.cfg
cat ks.cfg
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $1$u8A.0rdk$jSFRXKAOSLrd9yQm91zm1.
# Use network installation
url --url="http://172.25.254.133/shop"
# System language
lang en_US
# Firewall configuration
firewall --disabled
# System authorization information
auth --useshadow --passalgo=sha512
# Use text mode install
text
firstboot --disable
# SELinux configuration
selinux --disabled
# Network information
network --bootproto=dhcp --device=eth0
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="xfs" --size=200
part swap --fstype="swap" --size=500
part / --fstype="xfs" --grow --size=1
%post
cat >/etc/yum.repos.d/yum.repo <<EOF
[rhel]
name=rhel7.3
baseurl=http://172.25.254.133/shop
gpgcheck=0
EOF
yum install httpd -y
%end
%packages ##系统下载的安装包
@base ##下载软件组
lftp ##下载的软件
%end ##结束符
ksvalidator ks.cfg ##检查脚本是否有语法错误
cp ks.cfg /var/www/html/ks.cfg ##把脚本放到httpd的共享文件夹里
在需要装虚拟机的主机上编写脚本:
vim Linux.sh
#!/bin/bash
virt-install \
--name $1 \
--memory 1000 \
--vcpus 1 \
--disk /var/lib/libvirt/images/$1.qcow2,size=8,bus=virtio \
--network bridge=br0,model=virtio \
--location http://172.25.254.133/shop \
--extra-args "ks=http://172.25.254.133/ks/ks.cfg" &> /dev/null &
sh Linux.sh min
开始装虚拟机。
虚拟机安装结束,等待重启。
虚拟机安装成功,查看yum是否配置成功以及http服务。表明安装结束。