CentOS7 cobbler 配置,实现用 pxe 启动安装 kvm 虚拟机

Cobbler安装与配置

网络拓扑

 

CentOS7 cobbler 配置,实现用 pxe 启动安装 kvm 虚拟机_第1张图片

系统配置


# 关闭防火墙
[root@cobblervm ~]# systemctl stop firewalld.service
[root@cobblervm ~]# systemctl disable firewalld.service
[root@cobblervm ~]# firewall-cmd --state
not running
​
# 关闭SElinux
[root@cobblervm ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@cobblervm ~]# reboot

安装cobbler


# 安装epel源
[root@cobblervm ~]# yum -y install epel-release# 安装cobbler,其中没有 hcdp 服务,我的 dcph 是放在网关上的。
[root@cobblervm ~]# yum install -y cobbler cobbler-web tftp xinetd cman pykickstart debmirror syslinux net-tools# 安装cobbler依赖的包
[root@cobblervm ~]# yum install -y ed patch perl perl-Compress-Zlib perl-Digest-SHA1 perl-LockFile-Simple perl-libwww-perl fence-agents# 设置开机启动
[root@cobblervm ~]# systemctl enable httpd.service
[root@cobblervm ~]# systemctl enable xinetd.service
[root@cobblervm ~]# systemctl enable cobblerd.service# 查看服务状态
[root@cobblervm ~]# systemctl list-unit-files | grep -E "httpd|xinetd|cobblerd"
cobblerd.service                            enabled 
httpd.service                               enabled 
xinetd.service                              enabled

配置服务器


# 配置 web
[root@cobblervm ~]# sed -i 's/#ServerName www.example.com:80/ServerName 127.0.0.1:80/' /etc/httpd/conf/httpd.conf
[root@cobblervm ~]# systemctl restart httpd.service# 配置 cobbler
[root@cobblervm ~]# sed -i 's/server: 127.0.0.1/server: 192.168.1.254/g' /etc/cobbler/settings # cobbler 修改密码
[root@cobblervm ~]# openssl passwd -1 -salt 'root' '123456'
$1$root$8e67rDXAsnndS790noSQV0 # 生成的密码
[root@cobblervm ~]# vim /etc/cobbler/settings
......
default_password_crypted: "$1$root$8e67rDXAsnndS790noSQV0"  <== 用上面生成的密码替换此处的默认密码
......
​
# cobbler 默认不允许任何人登录,手动添加登录用户(添加admin)
[root@cobblervm ~]# useradd admin
[root@cobblervm ~]# passwd admin
[root@cobblervm ~]# sed -i 's/admin = ""/admin = "admin"/' /etc/cobbler/users.conf
[root@cobblervm ~]# sed -i 's/module = authn_configfile/module = authn_pam/' /etc/cobbler/modules.conf# 配置 xinetd
[root@cobblervm ~]# sed -i '14s/yes/no/' /etc/xinetd.d/tftp 
[root@cobblervm ~]# systemctl restart  xinetd.service
[root@cobblervm ~]# systemctl restart rsyncd.service
[root@cobblervm ~]# systemctl enable rsyncd.service# 配置 pxe
[root@cobblervm ~]# sed -i 's/manage_dhcp: 0/manage_dhcp: 1/' /etc/cobbler/settings
[root@cobblervm ~]# sed -i 's/pxe_just_once: 0/pxe_just_once: 1/' /etc/cobbler/settings# 重启服务
[root@cobblervm ~]# systemctl restart cobblerd.service
[root@cobblervm ~]# systemctl restart xinetd.service
[root@cobblervm ~]# cobbler sync# 对于防火墙如果不想关闭的话可以如下设置
[root@cobblervm ~]# firewall-cmd --permanent --add-port=67/udp
[root@cobblervm ~]# firewall-cmd --permanent --add-port=68/udp
[root@cobblervm ~]# firewall-cmd --permanent --add-port=80/tcp
[root@cobblervm ~]# firewall-cmd --permanent --add-port=443/tcp
[root@cobblervm ~]# firewall-cmd --reload
[root@cobblervm ~]# firewall-cmd --permanent --list-ports# 另外对于 dhcp 服务器的配置要求如下
[root@dhcpvm ~]# cat /etc/dhcp/dhcpd.conf 
#
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.example
#   see dhcpd.conf(5) man page
#
ddns-update-style interim;
 
allow booting;
allow bootp;
 
ignore client-updates;
set vendorclass = option vendor-class-identifier;
​
subnet 192.168.1.0 netmask 255.255.255.0 {
    range 192.168.1.2 192.168.1.253;    
    option broadcast-address 192.168.1.255;    
    option routers 192.168.1.1;    
    option domain-name-servers 159.226.39.1;    
    option netbios-name-servers 192.168.1.1;    
    option domain-name lan;    
    filename "/pxelinux.0";
    default-lease-time 21600;
    max-lease-time 43200;
    next-server 192.168.1.254;      <== pxe 启动的服务器地址,也就是 cobbler 地址
}
host tftp_server {
    hardware ethernet 52:54:00:fc:15:5f;    
    fixed-address 192.168.1.254;    <== 给 cobbler 服务器分配固定地址
}

登录 cobbler

用浏览器访问网址 https://xxx.xxx.xxx.xxx:/cobbler_web

CentOS7 cobbler 配置,实现用 pxe 启动安装 kvm 虚拟机_第2张图片

CentOS7 cobbler 配置,实现用 pxe 启动安装 kvm 虚拟机_第3张图片

系统镜像安装到 cobbler 服务器上


# 上传系统安装镜像文件CentOS-7-x86_64-DVD-1511.iso 到/usr/local/src/目录(拿U盘考,用Xftp工具上传都行)
[root@cobblervm ~]# mkdir -p /var/www/html/os/CentOS-7-x86_64      <==创建挂载目录 # 挂载系统镜像
[root@cobblervm ~]# mount -t iso9660 -o loop /usr/local/src/CentOS-7-x86_64-DVD-1511.iso  /var/www/html/os/CentOS-7-x86_64# 添加以下代码,实现开机自动挂载
[root@cobblervm ~]# vim /etc/fstab
/usr/local/src/CentOS-7-x86_64-DVD-1511.iso /var/www/html/os/CentOS-7-x86_64 iso9660 defaults,ro,loop 0 0

编写安装配置脚本

可以用专用编辑器生成,推荐一款 linux 下的 system-config-kickstart 工具


# 给个样本,不一定能安装成功,这个文件可以在 cobbler_web 界面的 Kickstart Templates 中看到
[root@cobblervm ~]# vim /var/lib/cobbler/kickstarts/CentOS7.ks
install
lang zh_CN.UTF-8
keyboard us
timezone Asia/Shanghai
auth --useshadow --enablemd5
selinux --disabled
firewall --disabled
services --enabled=NetworkManager,sshd
eula --agreed
ignoredisk --only-use=sda
reboot
 
bootloader --location=mbr
zerombr
clearpart --all --initlabel
part swap --asprimary --fstype="swap" --size=1024
part /boot --fstype xfs --size=200
part pv.01 --size=1 --grow
volgroup rootvg pv.01
logvol / --fstype xfs --name=lv01 --vgname=rootvg --size=10240
logvol /data --fstype xfs --name=lv02 --vgname=rootvg --size=1 --grow
​
rootpw --iscrypted $default_password_crypted
 
repo --name=base --baseurl=http://192.168.17.181/cobbler/ks_mirror/CentOS-7-x86_64/
url --url=http://192.168.17.181/cobbler/ks_mirror/CentOS-7-x86_64/
 
%packages --ignoremissing
@base
@core
@chinese-support
ntp
net-tools
%end
 
%post
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
echo "0 1 * * * /usr/sbin/ntpdate 202.120.2.101;hwclock -w;" >>/var/spool/cron/root
/usr/sbin/ntpdate 202.120.2.101 >/dev/null 2>&1
cd /root
wget http://192.168.17.181/cobbler/ks_mirror/config/autoip7.sh
sh /root/autoip7.sh
%end

导入系统镜像,要等一会


[root@cobblervm ~]# cobbler import --path=/var/www/html/os/CentOS-7-x86_64/  --name=CentOS-7-x86_64  --arch=x86_64[root@cobblervm ~]# cobbler import --path=/var/www/html/os/CentOS-7-x86_64/  --name=CentOS-7-x86_64  --arch=x86_64

修改配置文件

单击 web 界面的 Profile 文件并编辑刚刚生成的配置文件。

CentOS7 cobbler 配置,实现用 pxe 启动安装 kvm 虚拟机_第4张图片

然后将 Kickstart 改成自己编辑的 kickstart 脚本

CentOS7 cobbler 配置,实现用 pxe 启动安装 kvm 虚拟机_第5张图片

设置 pxe 启动配置


[root@cobblervm ~]# cat /etc/cobbler/pxe/pxedefault.template 
DEFAULT menu
PROMPT 0
MENU TITLE Cobbler | http://cobbler.github.io/
TIMEOUT 200
TOTALTIMEOUT 6000
ONTIMEOUT CentOS-7-x86_64 <== 改成上面导入镜像使设置的 CentOS-7-x86_64,意思是超时自动引导pxe启动
​
LABEL local
        MENU LABEL (local)
        MENU DEFAULT
        LOCALBOOT -1$pxe_menu_items
​
MENU end
​
# 查看是否修改成功
[root@cobblervm ~]# cat /var/lib/tftpboot/pxelinux.cfg/default

最后启动安装 pxe 启动的虚拟机。


# 创建虚拟机
[root@h96 ~]# virt-install --virt-type kvm --name 虚拟机名 --ram 内存 --pxe  --vcpus=1  --disk path=磁盘 --network bridge=br0,model=virtio --graphics vnc,listen=0.0.0.0 --noautoconsole
# 用 virt-manager 查看自动安装的情况

如图所示

CentOS7 cobbler 配置,实现用 pxe 启动安装 kvm 虚拟机_第6张图片

CentOS7 cobbler 配置,实现用 pxe 启动安装 kvm 虚拟机_第7张图片

CentOS7 cobbler 配置,实现用 pxe 启动安装 kvm 虚拟机_第8张图片

CentOS7 cobbler 配置,实现用 pxe 启动安装 kvm 虚拟机_第9张图片

 

你可能感兴趣的:(linux学习之路)