运维笔记12 (装机脚本kickstart,无介质装机(PXE))

概述:

由于kickstart自动化装机脚本一般都是在无介质装机时使用的,也就是网络安装中,为了简化学习,我们先从虚拟机的模拟网络安装开始,当熟悉kickstart和网络安装后,我们介绍本次的杀招--PXE无介质装机,让你摆脱U盘,一跟网线进行装机,其中需要dhcp,ftp,http服务的一些知识。

1.虚拟机的网络安装方法

虚拟机真是个非常好的东西,给我这种新手随意折腾的地方,也不会有什么后果,因为这次的装机都是通过网络方式的,所以我们先把网络装虚拟机练好,后面也比较容易理解了。

1)打开新建虚拟机,看第二个选项就是网络安装虚拟机,可以通过http,ftp,nfs的方式

2)这里选择你的镜像的url(统一资源定位符),下面的URL Options是我们之后选择自动化脚本时候需要的选项,这里这是网络装机,所以先不做选择。


3)这下面的几步都是默认,基本一路Forward就可以了,和之前讲过的虚拟机安装时一样的传送门


4)选择磁盘大小

5)选择完毕了可以安装了


6)开始安装


等等好像不太对,等了好长时间以为安装好了,却成了这个样子,翻看前面好像也没做错什么呀,咱们继续往下看

7)我使用ip命令查看了下当前的ip,发现了问题,我选择的可是网络安装这台虚拟机没有ip怎么可以,没有ip我怎么在我自己的http服务器上下载镜像资源呢,就没法通信呀,但是前面没发现有设置虚拟机ip的选项,所以我选择搭建一个dhcp服务器,实践证明dhcp服务器搭建好后问题就解决了。剩下的和一般的装机过程没有任何区别。



2.kickstart自动化安装脚本

我们知道每次装机时都要选好多东西才可以进入安装过程,安装一台还挺有意思的,但是安装好100台怎么办,是不是想吐了,而且全是重复的劳动,liunx正好有这样一个自动化安装脚本来解决我们遇到的问题。让我们先看一个脚本感受下。

timezone Asia/Shanghai --isUtc
user --name=mo --password=$6$m4Ajdb2qyXJAdvuF$yEwzblKH2WGKqp0czWpkWP0XAhuX6TOUPByeHy755kfR4BPKFElRn9Ne1ALXfFeE1nK3K9iuuwjaG9ro7AhwG. --iscrypted --gecos="mo"
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
# Partition clearing information
clearpart --all --initlabel --drives=sda
# Disk partitioning information
part pv.472 --fstype="lvmpv" --ondisk=sda --size=238173
part /boot --fstype="xfs" --ondisk=sda --size=300
volgroup rhel --pesize=4096 pv.472
logvol swap  --fstype="swap" --size=3814 --name=swap --vgname=rhel
logvol /  --fstype="xfs" --size=138982 --name=root --vgname=rhel
logvol /home  --fstype="xfs" --size=95367 --name=home --vgname=rhel

%packages
@core
kexec-tools

%end

%addon com_redhat_kdump --enable --reserve-mb='auto'

%end


这个脚本有些东西不是很好懂,而且你写错了的话装的系统就有问题了,所以这里有一个图形化的工具简化我们编写的难度。

这个工具就是system-config-kickstart,我们下面只要简单讲解下使用这个工具的各个步骤,你就可以自己生成自动化安装脚本了。

[root@2+2 html]# yum search system-config-kickstart
已加载插件:product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
base                                                               | 4.1 kB  00:00:00     
(1/2): base/group_gz                                               | 134 kB  00:00:00     
(2/2): base/primary_db                                             | 3.4 MB  00:00:00     
========================== N/S matched: system-config-kickstart ==========================
system-config-kickstart.noarch : A graphical interface for making kickstart files

3.图形化工具system-config-kickstart

1)第一步选择默认语言,键盘,时区,密码(如果选择Encrypt在最后生成的脚本中密码就是加密的,否则是明文),最后的高级选项,第一项目标设备,选择x86就可以,因为我们都是家用的主机,剩下两项勾选即可。


2)选择安装的方式,这里就是指定下安装的源,和上面一样我们选择网络源安装,我们的网络源是http服务。

3)选择第一个安装新的引导,否则你要自己写引导了


4)对磁盘的分区操作,这里的前三个选项全部选第一个,再最后你选择你所需要的分区结构。

5)网络配置,这个一定要配置好,且网络设备一定写eth0否则会在安装时出问题

6)登陆的验证方式不用管这项



7)防火墙设置与selinux,可以关闭也可以不管。

8)这项选择默认即可

9)最后两项是安装前和安装后执行的脚本,我们可以写一些命令作为测试。

10)最后将这个保存即可,就生成了我们自己的kickstart脚本。


11)自己生成的kickstart脚本

#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'# Reboot after installation
reboot
# Root password
rootpw --iscrypted $1$PlK5EMXM$EfrAhb3X6dx5Eyxk5LNov/
# System timezone
timezone Africa/Abidjan
# Use network installation
url --url="http://172.25.254.3/rhel7.1"
# System language
lang en_US
# Firewall configuration
firewall --disabled
# Network information
network  --bootproto=dhcp --device=eth0
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use text mode install
text
firstboot --disable
# SELinux configuration
selinux --disabled

# 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

%packages
@base
%end
我在最后加了我们需要安装的一个基础包,也就是相关软件如果是最小安装关键字为@core。

将这个脚本放到http服务器上然后在虚拟机安装的第一步把他的url填进去即可自动安装了。



4.无介质安装PXE(网线安装操作系统)

1)第一步我们要先做些环境准备,yum dhcp tftp-server syslinux httpd -y,这些都是为了完成PXE安装所需要的。这里我们不太熟悉的有tftp-server,syslinux。我们一个一个介绍tftp-server是一个多线程TFTP服务器,允许任何数量的客户端连接同时进行。它支持tsize , blksize ,和间隔的选择,PXE启动,并可以运行独立的或以daemon方式运行的。他有一个特性就是,它只能从文件服务器上获得或写入文件,不能列出目录,不进行认证,它传输8位数据。我们使用他来向网卡发送数据,发送的数据呢是夹杂在dhcp服务分发的数据中;syslinux是一个功能强大的引导加载程序,而且兼容各种介质。它的目的是简化首次安装Linux的时间,并建立修护或其它特殊用途的启动盘。总之需要他里面的一个数据文件pxelinux.0。


2)配置过程

关闭防火墙,关闭防火墙默认启动

[root@2+2 html]# systemctl stop firewalld.service
<span style="white-space:pre">		</span> systemcttl disable firewalld.servic
编辑xinetd.d

[root@2+2 html]# vim /etc/xinetd.d/tftp 
将这个文件中的disable的yes修改成no,其实这步就是对tftp的配置,但是为什么在xinetd.d的目录下呢,这就要涉及到linux下的守护进程与超级守护进程。

所谓守护进程就是自己常驻内存占用系统资源,有客户需要服务时自己直接提供,比如httpd;超级守护进程是由一个守护进程管理的(比如init.d,和本文中提到的xinetd.d),当有客户需要服务时,先要和这个管理守护进程打交道,再由这个进程通知我们真正需要的服务,比如tftp,如果有想深入了解的同学可以进入传送门了解。

# default: off
# description: The tftp server serves files using the trivial file transfer \
#       protocol.  The tftp protocol is often used to boot diskless \
#       workstations, download configuration files to network-aware printers, \
#       and to start the installation process for some operating systems.
service tftp
{
        socket_type             = dgram
        protocol                = udp
        wait                    = yes
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args             = -s /var/lib/tftpboot
        <span style="color:#ff0000;">disable                 = yes</span>
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}

重启xinetd.d,开启http服务

[root@2+2 html]# systemctl restart xinetd
[root@2+2 html]# systemctl start httpd


将需要发送到网卡的文件复制到tftp服务的发送目录下

[root@2+2 ~]# cd /var/lib/tftpboot/
[root@2+2 tftpboot]# cp /usr/share/syslinux/pxelinux.0 ./
[root@2+2 tftpboot]# ls
pxelinux.0
[root@2+2 tftpboot]# cp /var/www/html/rhel7.1/isolinux//* ./
[root@2+2 tftpboot]# ls
boot.cat  grub.conf   isolinux.bin  memtest     splash.png  upgrade.img   vmlinuz
boot.msg  initrd.img  isolinux.cfg  pxelinux.0  TRANS.TBL   vesamenu.c32
[root@2+2 tftpboot]# mkdir pxelinux.cfg
[root@2+2 tftpboot]# cp ./isolinux.cfg ./pxelinux.cfg/default



配置dhcp服务

从我们上面网络装机失败的教训看出,dhcp服务是必须的,毕竟涉及到网络,双方想通信就当然都要有ip了,不过这次dhcp还有别的作用。就是在dhcp数据包中添加引导程序。

其他的配置与正常的一致,只是在最后需要添加两行,即红字处。

# If this DHCP server is the official DHCP server for the local 
# network, the authoritative directive should be uncommented. 
#authoritative; 
 
# Use this to send dhcp log messages to a different log file (you also 
# have to hack syslog.conf to complete the redirection). 
log-facility local7; 
 
# No service will be given on this subnet, but declaring it helps the  
# DHCP server to understand the network topology. 
 
 
# This is a very basic subnet declaration. 
 
subnet 192.168.1.0 netmask 255.255.255.0 { 
  range 192.168.1.3 192.168.1.8; 
  option routers 192.168.1.1; 
  <span style="color:#ff0000;">filename "pxelinux.0"; <span style="white-space:pre">	</span>#pxelinux.0文件
  next-server 192.168.1.239; <span style="white-space:pre">	</span>#pxelinux.0文件所在主机的ip</span>
} 

到这里基本上已经大功告成了。但是继续装机发现并不可以装机,那是因为还有一些需要修改,打开你的default文件。

label linux
  menu label ^Install Red Hat Enterprise Linux 7.1
  kernel vmlinuz
  append initrd=initrd.img #将这个后面的删除掉,换成(1)inst.stage2=hd:LABEL=RHEL-7.1\x20Server.x86_64 quiet
  (1)repo=http://192.168.1.239/rhel7.1(换成你的镜像源) ks=http://...(如果你有kickstart脚本可以加上)
label check
  menu label Test this ^media & install Red Hat Enterprise Linux 7.1
  menu default<span style="white-space:pre">		</span>#这个选项在哪里开机选项就默认在哪里
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.1\x20Server.x86_64 rd.live.check quiet
这样我们就完成啦。













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值