#########################
###### unit.自动安装系统    #######
#########################

命令行"Network Install":
virt-install \
> --name test \
> --ram 1024 \
> --file /var/lib/libvirt/p_w_picpaths/test.qcow2 \
> --file-size 8 \
> --location ftp://172.25.254.250/pub/rhel7.2 &

记不住命令,可以使用"man virt-install"来查找

####################1.kickstart脚本####################
kickstart脚本是自动应答系统在安装过程中一切问题的脚本文件
这个文件可以实现系统的自动安装
在系统安装完毕后会在系统的root家目录中生成
"anaconda-ks.cfg"这个文件就是以此系统为模版生成的kickstart脚本

[root@foundation50 Desktop]# ls /root/
anaconda-ks.cfg  Downloads                   Music     Templates
Desktop          foundation-config-post.log  Pictures  Videos
Documents        initial-setup-ks.cfg        Public
"anaconda-ks.cfg"安装过程中运行的程序脚本

####################2.kickstart脚本的制作####################
手动编写kickstart的难度很大
系统中system-config-kickstart工具可以以图形的方式制作kickstart

yum install system-config-kickstart -y    ##安装图形ks制作工具
system-config-kickstart            ##打开图形ks制作工具
ksvalidator ks.cfg            ##检测ks语法

使用ks工具生成的"ks.cfg"文件后,使用vim进行手动编辑
vim ks.cfg
--------------------------------------------------
%packages
@base
%end
:wq
--------------------------------------------------
注意:如果不添加上述配置,安装时会提示"Software selection(Nothing selected)"
需要按"r"键(refresh),再按"b"键(begin installation),才会开始安装
而且安装的是一个很小的系统,只安装318个包
最小化安装应该是499个包

--------------------------------------------------
# Use text mode install
text
firstboot --disable
--------------------------------------------------
"text"表示使用文本安装,即安装过程不开启图形
"firstboot --disable"
/百度"firstboot":
firstboot(可选)
    决定是否在系统第一次引导时启动“设置代理”。
    如果启用,firstboot软件包必须被安装。如果不指定,这个选项是缺省为禁用的。
    --enable或--enabled,系统第一次引导时,启动“设置代理”。
    --disable或--disabled,系统第一次引导时,不启动“设置代理”。
    --reconfig,在系统引导时在重配置(reconfiguration)模式下启用"设置代理"。这个模式启用了语言,鼠标,键盘,根密码,安全级别,时区和缺省网络配置之外的选项。

"ksvalidator ks.cfg"
对"ks.cfg"的语法进行检测,比如:
[root@foundation50 Desktop]# vim ks.cfg
--------------------------------------------------
删除最后一行:
%end
:wq
--------------------------------------------------
[root@foundation50 Desktop]# ksvalidator ks.cfg
The following problem occurred on line 41 of the kickstart file:

Section %packages does not end with %end.

####################3.kickstart文件共享####################
yum install httpd -y
systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl start httpd
systemctl enable httpd
cp ks.cfg /var/www/html

cp ks.cfg /var/www/html/
注意:一定要用"cp"复制,不能用"mv"移动
这个是系统机制,以后会讲,现在不用考虑

如果不小心使用了"mv"移动,就会发现使用http无法进行访问
输入如下命令:
restorecom /var/www/html/ -R
就可以访问了

####################4.测试ks文件####################
[root@foundation22 ~]# virt-install \
> --name kstest \
> --ram 800 \
> --file /var/lib/libvirt/p_w_picpaths/kstest.qcow2 \
> --file-size 8 \
> --location ftp://172.25.254.250/pub/rhel7.2 \
> --extra-args "ks=http://172.25.254.100/ks.cfg" &

####################附加pxe网络安装服务####################
1.相应软件安装
yum install dhcp tftp-server syslinux httpd -y
systemctl stop firewalld.service
systemctl disable firewalld.service
vim /etc/xinetd.d/tftp
[ disable = yes ] =====> [ disable = no ]
systemctl restart xinetd
systemctl start httpd
systemctl enable httpd

注意:
rhel7.2的系统安装tftp-server,不会依赖安装"xinetd",所以要手动安装"yum install xinetd -y"。好坑

2.必须文件的下载复制
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
把安装镜像中的isolinux/* 全部复制到/var/lib/tftpboot/
mkdir /var/lib/tftpboot/pxelinux.cfg
cp /var/lib/tftpboot/isolinux.cfg /var/lib/tftpboot/pexlinux.cfg/default

####################
[root@foundation50 isolinux]# rpm -ql tftp-server
/etc/xinetd.d/tftp
/usr/lib/systemd/system/tftp.service
/usr/lib/systemd/system/tftp.socket
/usr/sbin/in.tftpd
/usr/share/doc/tftp-server-5.2
/usr/share/doc/tftp-server-5.2/CHANGES
/usr/share/doc/tftp-server-5.2/README
/usr/share/doc/tftp-server-5.2/README.security
/usr/share/man/man8/in.tftpd.8.gz
/usr/share/man/man8/tftpd.8.gz
/var/lib/tftpboot
[root@foundation50 isolinux]# rpm -ql syslinux | grep pxelinux.0
/usr/share/syslinux/gpxelinux.0
/usr/share/syslinux/pxelinux.0
[root@foundation50 isolinux]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
####################

3.dhcpd服务的配置
subnet 172.25.254.0 netmask 255.255.255.0 {
  range 172.25.254.200 172.25.254.220;
  option routers 172.25.254.254;
  filename "pxelinux.0";
  next-server 172.25.254.100;
}

[root@foundation50 tftpboot]# vim /var/lib/tftpboot/pxelinux.cfg/default

--------------------------------------------------

 61 label custom
##增加选项"custom"
 62   menu label ^Custom Install
##选项名称为"Custom Install","^"表示后面的字符高亮显示
 63   kernel vmlinuz
 64   append initrd=initrd.img repo=http://172.25.50.250/rhel7.2 quiet
##quiet表示不看内核信息
 65
 66 label linux
 67   menu label ^Install Red Hat Enterprise Linux 7.2
 68   menu default
##修改为默认选项
 69   kernel vmlinuz
 70   append initrd=initrd.img repo=http://172.25.50.250/rhel7.2 ks=http://172.25.50.250/ks.cfg
##修改信息,指定安装源和ks脚本
 71
 72 label check
 73   menu label Test this ^media & install Red Hat Enterprise Linux 7.2
 74 #  menu default
 75   kernel vmlinuz
 76   append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.2\x20Server.x86_64 rd.live.check quiet
--------------------------------------------------