作业

1.使用 kickstart 半自动化安装CentOS系统

一、系统自动化安装:

    1、anaconda

    由前面的学习我们知道,系统的启动流程:

        bootloader-->kernel(initramfs)-->rootfs-->/sbin/init

    所以,bootloader完全可以由光盘来进行引导,在引导的过程中在借助kickstart来进程自动安装

Anaconda是RedHat、CentOS、Fedora等Linux的安装管理程序。它可以提供文本、图形等安装管理方式,并支持Kickstart等脚本提供自动安装的功能。

    此外,其还支持许多启动参数,熟悉这些参数可为安装带来很多方便。该程序的功能是把位于光盘或其他源上的数据包,根据设置安装到主机上。

    为实现该定制安装,它提供一个定制界面,可以实现交互式界面供用户选择配置(如选择语言,键盘,时区等信息)。Anaconda的大部分模块用Python编写,有少许的载入模块用C编写。

2、kickstart文件的格式可以分为三个部分:

        命令段:指明各种安装前配置,如键盘类型等;

        程序包段:指明要安装的程序包组或程序包,不安装的程序包等;

%packages

@group_name

package

-package

%end

        脚本段:

%pre: 安装前脚本

        运行环境:运行于安装介质上的微型Linux环境

%post: 安装后脚本

        运行环境:安装完成的系统;

 

 

Anaconda支持的管理模式: 

    (1)Kickstart提供的自动化安装; 

    (2)对一个RedHat实施upgrade; 

    (3)Rescuse模式对不能启动的系统进行故障排除。 

 

要进入安装步骤,需要先有一个引导程序引导启动一个特殊的Linux安装环境系统;引导有多种方式: 

    (1)基于网络方式的小型引导镜像,需要提供小型的引导镜像; 

    (2)U盘引导,通过可引导存储介质中的小型引导镜像启动安装过程;  

    (3)基于PXE的网络安装方式,要提供PXE的完整安装环境; 

    (4)其他bootloder引导(如GRUB)。 

可用的安装方式:本地CDROM、硬盘驱动器、网络方式(NFS、FTP、HTTP)。 

 

 

 

3、kickstart文件创建

    创建kickstart文件的方式:

        (1) 直接手动编辑;

        依据某模板修改:

        在系统安装完成后都会在/root/目录下生成一个anaconda-ks.cfg的配置,配置记录了安装系统的选项和参数:

root@localhost:~ # cat anaconda-ks.cfg
#version=RHEL8
ignoredisk --only-use=sda
# Partition clearing information
clearpart --none --initlabel
# Use graphical install
graphical
repo --name="AppStream" --baseurl=file:///run/install/repo/AppStream
# Use CDROM installation media
cdrom
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8

# Network information
network --bootproto=dhcp --device=ens160 --ipv6=auto --activate
network --hostname=localhost.localdomain
# Root password
rootpw --iscrypted $6$/SirdA.Lfep/g2jo$ucmQpFIKuH7nZhk.TVeZ.esU3fYgTjsCKVhXPxXGjfR0xYEWHWyKVxvlvWrCpf7puGeY.Ft1BQl8dBB/Y2NU30
# X Window System configuration information
xconfig --startxonboot
# Run the Setup Agent on first boot
firstboot --enable
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc --nontp
user --name=zhang --password=$6$l68F38Jqtow.1Kld$LDDOHMaJudyVO6dim8y6/Xab5JrYAeER6PxhaxQwiksMoJkiByv/hhM.Uxvq/YfMabRzeRpro8WbZr2iy5XWW1 --iscrypted --gecos="zhang"
# Disk partitioning information
part /boot --fstype="ext4" --ondisk=sda --size=1024
part /data --fstype="xfs" --ondisk=sda --size=50000
part swap --fstype="swap" --ondisk=sda --size=2048
part / --fstype="xfs" --ondisk=sda --size=100000

%packages
@^graphical-server-environment
kexec-tools

%end

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

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end

 (2) 可使用图形化的创建工具:system-config-kickstart(该工具系统没有自带,需要安装system-config包)

        推荐使用图形化的工具来进行创建:

 

在图形终端输入命令:system-config-kickstart来运行kickstart脚本创建向导

 

 

输入要安装的linux镜像文件的位置,支持多种方式

 

 

 

grub选项

 

提前做好分区计划

 

 

配置网络信息

 

 

配置其他选项

 

选择需要安装的软件包

 

可编写系统安装前后的脚本

 

 

重要的一步:在光盘启动时,按esc键进入boot模式,输入kickstart的配置文件路径

 

 

回车后自动安装

   编写完成后自动生成的kickstart脚本如下:

 root@localhost:~ #cat mylinux-ks.cfg 
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Firewall configuration
firewall --disabled
# Install OS instead of upgrade
install
# Use network installation
url --url="http://10.1.0.1/cobbler/ks_mirror/CentOS-6-x86_64/"
# Root password
rootpw --iscrypted $1$LY.D8e4v$UnqW3SAJSZyg9hxr.YjAo/
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use text mode install
text
# System keyboard
keyboard us
# System language
lang en_US
# SELinux configuration
selinux --enforcing
# Do not configure the X Window System
skipx
# Installation logging level
logging --level=info
# Reboot after installation
reboot
# System timezone
timezone --isUtc Asia/Shanghai
# Network information
network  --bootproto=dhcp --device=eth0 --onboot=on
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all  
# Disk partitioning information
part / --fstype="ext4" --size=20000
part /boot --fstype="ext4" --size=200
part swap --fstype="swap" --size=4000
%post
echo "welcome to use mylinux" >> /etc/issue
echo -e "[base]\nbaseurl=/msic/cd\ngpgcheck=0" > /etc/yum.repo.d/base.repo
%end
%packages
@base
@basic-desktop
@desktop-debugging
@desktop-platform
@fonts
@general-desktop
@graphical-admin-tools
@input-methods
@kde-desktop
@legacy-x
@remote-desktop-clients
@x11
%end

 4、kickstart文件使用(自动安装系统):

 

 

挂载光盘进入救援模式,在选择菜单出按esc间,进入命令模式,输入kickstart文件的位置:linux ks=

eg.

DVD drive: ks=cdrom:/PATH/TO/KICKSTART_FILE

Hard drive: ks=hd:device:/directory/KICKSTART_FILE

HTTP server: ks=http://host:port/path/to/KICKSTART_FILE

FTP server: ks=ftp://host:port/path/to/KICKSTART_FILE

HTTPS server: ks=https://host:port/path/to/KICKSTART_FILE

NFS server:ks=nfs:host:/path/to/KICKSTART_FILE


 实验八:实现pxe安装双系统centos6、centos7 
PXE自动化安装centos6和centos7

一、PXE

PrebootExcution Environment 预启动执行环境

Intel公司研发基于Client/Server的网络模式,支持远程主机通过网络从远端服务器下载映像,并由此支持通过网络启动操作系统,PXE可以引导和安装Windows,linux等多种操作系统

二、PXE工作原理

1.Client向PXE Server上的DHCP发送IP地址请求消息,DHCP检测Client是否合法(主要是检测Client的网卡MAC地址),如果合法则返回Client的IP地址,同时将启动文件pxelinux.0的位置信息一并传送给Client

2.Client向PXE Server上的TFTP发送获取pxelinux.0请求消息,TFTP接收到消息之后再向Client发送pxelinux.0大小信息,试探Client是否满意,当TFTP收到Client发回的同意大小信息之后,正式向Client发送pxelinux.0

3.Client执行接收到的pxelinux.0文件

4.Client向TFTP Server发送针对本机的配置信息文件(在TFTP 服务的pxelinux.cfg目录下),TFTP将配置文件发回5.Client,继而Client根据配置文件执行后续操作。

6.Client向TFTP发送Linux内核请求信息,TFTP接收到消息之后将内核文件发送给Client

7.Client向TFTP发送根文件请求信息,TFTP接收到消息之后返回Linux根文件系统

8.Client启动Linux内核

 

9.Client下载安装源文件,读取自动化安装脚本

三、PXE实现自动化安装部署centos系统

 

准备一台主机作为网络环境内的dhcp服务器、tftp服务器、http服务器,在此主机上配置PXE环境,为网络中的其它主机提供自动化系统安装服务。
关闭防火墙
 

  1. [root@CentOS7 ~]#systemctl stop firewalld.service 关闭防火墙
  2. [root@CentOS7 ~]systemctl disable firewalld.service 禁止防火墙开机自启动

关闭SELINUX
[root@CentOS7 ~]sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config 关闭selinux策略

 [root@CentOS7 ~]getenforce 获取linux当前状态

    1.安装相关软件包


[root@CentOS7 ~]yum install -y dhcp httpd tftp-server syslinux 一次性安装四个软件包

 [root@CentOS7 ~]systemctl start httpd tftp.socket 启动http和tftp服务

 [root@CentOS7 ~]systemctl enable httpd tftp.socket 设置开机自 启动

  2.制作CentOS6和CentOS7的yum源

 
[root@CentOS7 ~]mkdir -pv /var/www/html/centos/{6,7}

 [root@CentOS7 ~]echo /dev/sr0 /var/www/html/centos/7 iso9660 defaults 0 0 >> /etc/fstab

 [root@CentOS7 ~]echo /dev/sr1 /var/www/html/centos/6 iso9660 defaults 0 0 >> /etc/fstab

 [root@CentOS7 ~]mount -a 使挂载生效

3.制作CentOS6和CentOS7的kickstart文件

 


[root@CentOS7 ~]mkdir /var/www/html/ksdir

[root@CentOS7 ~]cp -p /root/anaconda7-ks.cfg /var/www/html/ksdir/ks7_desktop.cfg

[root@CentOS7 ~]cp -p /root/anaconda6-ks.cfg /var/www/html/ksdir/ks6-mini.cfg

[root@CentOS7 ~]ls

  • 注意应答文件的权限问题

systemctl  set-default multi-user.target  默认进入多用户模式

systemctl  disable initial-setup.service 自动化centos7


#version=DEVEL
install
url --url=http://192.168.138.130/centos/6 #指定安装使用的yum源路径
lang en_US.UTF-8
keyboard us
network --onboot yes --device eth0 --bootproto dhcp --noipv6
rootpw --iscrypted $6$KzWQPi7a9pSHtuyE$1/WjqP3tuUdT4etobzIUcPMZzKLQ6M7ekKrarqMJfuqfH2vEl4OdE4KJvJVsbOjQaDeYzULThmRx/HNpnFgAI/
firewall --disabled
authconfig --enableshadow --passalgo=sha512
selinux --disabled
timezone Asia/Shanghai
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
# The following is the partition information you requested
# Note that any partitions you deleted are not expressed
# here so unless you clear all partitions first, this is
# not guaranteed to work
clearpart --all #清空全部分区
zerombr #安装时自动清空MBR
reboot #安装完成后自动重启
text #使用text文本安装方式
part /boot --fstype=ext4 --size=1024
part / --fstype=ext4 --size=50000
part /data --fstype=ext4 --size=30000
part swap --size=2048
%packages #要安装的软件包
@core
autofs
%end
%post #安装后脚本
useradd centos
echo centos | passwd --stdin centos
%end

编辑CentOS7的应答文件ks7_desktop.cfg


#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
url --url=http://192.168.138.130/centos/7/
# Use graphical install
firewall --disabled
selinux --disabled
text
reboot
# Run the Setup Agent on first boot
firstboot --disable
ignoredisk --only-use=sda
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8
# Network information
network --bootproto=dhcp --device=ens33 --onboot=on --ipv6=auto --activate
network --hostname=localhost.localdomain
# System bootloader configuration
bootloader --location=mbr --boot-drive=sda
# Partition clearing information
zerombr
clearpart --all --initlabel
# Disk partitioning information
part swap --fstype="swap" --ondisk=sda --size=2048
part /boot --fstype="xfs" --ondisk=sda --size=1024
part / --fstype="xfs" --ondisk=sda --size=51200
part /app --fstype="xfs" --ondisk=sda --size=20480
eula --agreed
%packages
@^gnome-desktop-environment
@base
@core
@desktop-debugging
@dial-up
@directory-client
@fonts
@gnome-desktop
@guest-agents
@guest-desktop-agents
@input-methods
@internet-browser
@java-platform
@multimedia
@network-file-system-client
@networkmanager-submodules
@print-client
@x11
%end
%addon com_redhat_kdump --disable --reserve-mb='auto'
%end
%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
%post
useradd centos
echo centos | passwd --stdin centos &> /dev/null
systemctl set-default multi-user.target
systemctl disable initial-setup-graphical.service
%end

  4.配置dhcp服务

默认dhcp配置文件内没有配置信息,可以使用配置示例文件将配置文件覆盖后进行修改,dhcp服务必须在更改配置

文件才能启动。

[root@CentOS7 ~]cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
[root@CentOS7 ~]vim /etc/dhcp/dhcpd.conf 编辑配置文件

# DHCP server to understand the network topology.

subnet 192.168.138.0 netmask 255.255.255.0 {

range 192.168.138.10 192.168.138.199;

option routers 192.168.138.254;

next-server 192.168.138.130;

filename "pxelinux.0";

}

[root@CentOS7 ~]systemctl enable dhcpd 设置dhcp服务开机自启动

[root@CentOS7 ~]systemctl start dhcpd 启动dhcp服务

[root@CentOS7 ~]ss -ntlu 查看用到的服务端口是否都已开启 dhcp:67 tftp69 http80

 

[root@CentOS7 ~]mkdir /var/lib/tftpboot/pxelinux.cfg/
[root@CentOS7 ~]mkdir /var/lib/tftpboot/centos{6,7}/ #分别存放各自的内核
[root@CentOS7 ~]cp /var/www/html/centos/6/isolinux/{initrd.img,vmlinuz} /var/lib/tftpboot/centos6/ #拷贝内核文件
[root@CentOS7 ~]cp /var/www/html/centos/7/isolinux/{initrd.img,vmlinuz} /var/lib/tftpboot/centos7/
[root@CentOS7 ~]cp /usr/share/syslinux/{pxelinux.0,menu.c32} /var/lib/tftpboot
[root@CentOS7 ~]cp /var/www/html/centos/7/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default 拷贝菜单模板
[root@CentOS7 ~]tree /var/lib/tftpboot/
├── centos6
│   ├── initrd.img
│   └── vmlinuz
├── centos7
│   ├── initrd.img
│   └── vmlinuz
├── menu.c32
├── pxelinux.0
└── pxelinux.cfg
└── default

    6.准备启动菜单

[root@CentOS7 ~]vim /var/lib/tftpboot/pxelinux.cfg/default
default vesamenu.c32
timeout 600
menu title Auto Install CentOS
label centos7
menu label ^Install Desktop CentOS 7
kernel centos7/vmlinuz
append initrd=centos7/initrd.img ks=http://192.168.138.130/ksdir/ks7_desktop.cfg
label centos6
menu label install ^Mini CentOS 6
menu default
kernel centos6/vmlinuz
append initrd=centos6/initrd.img ks=http://192.168.138.130/ksdir/ks6_mini.cfg
label local
menu label Boot from ^local drive
localboot 0xffff
menu end
tree /var/lib/tftpboot/

      7.客户端测试安装即可

posted @ 2020-09-12 10:04  Zs,  阅读(193)  评论(0)    收藏  举报