cobbler实现自动系统部署

516 阅读4分钟

#参考www.cnblogs.com/neteagles/p…

此处使用centos7做实验,环境准备

#此处使用centos7做实验,环境准备
    两台主机:
    一台主机:CentOS 7充当cobbler,http,dhcp,tftp服务器,并关闭防火墙和SELinux
    一台主机:充当测试机,用于实现自动化安装Linux系统
    网络要求:关闭vmware软件中的NAT模式中的DHCP服务,两个主机网卡基于NAT模式
#服务机
    #1.安装相关包并启动服务
        [root@centos7 ~]# yum install dhcp cobbler cobbler-web pykickstart httpd tftp -y
        #并关闭防火墙和SELinux 需重启
        [root@centos7 ~]# systemctl enable --now cobblerd httpd tftp dhcpd
        
    #2.修改cobbler相关的配置
	#查看需要哪些配置,配置一条后消失一条
	[root@centos7 ~]# cobbler check
	
	
        #生成新密码,默认安装好的系统root密码为shichong
        [root@centos7 ~]# openssl passwd -1 'shichong'
        $1$ySZ561LM$5Yl7wQynhphTwop/E1OTF1
        #修改下面四行
        [root@centos7 ~]# vim /etc/cobbler/settings 
        server: 10.0.0.170	  #IP地址修改为你当前主机的IP地址
        next_server: 10.0.0.170   #IP地址修改为你当前主机的IP地址
        manage_dhcp: 1  	  #设置1,表示通过cobbler生成dhcpd.conf配置文件
        default_password_crypted: "$1$ySZ561LM$5Yl7wQynhphTwop/E1OTF1"    #密码设置成你刚生成的新密码
        :wq
        [root@centos7 ~]# systemctl restart cobblerd
        
    #3.实现dhcp服务
	#修改dhcp的模版文件下面的行,用来生成dhcp的配置文件
        [root@centos7 ~]# vim /etc/cobbler/dhcp.template
        subnet 10.0.0.0 netmask 255.255.255.0 {
             option routers             10.0.0.2;			#路由器网关
             option domain-name-servers 218.30.19.40,61.134.1.4;	#dns地址
             option subnet-mask         255.255.255.0;
             range dynamic-bootp        10.0.0.100 10.0.0.150;          #地址范围
             range dynamic-bootp        10.0.0.200 10.0.0.254;          #地址范围 
        :wq	
        ##配置同步
        [root@centos7 ~]# cobbler sync         
        [root@centos7 ~]# systemctl start dhcpd
        #可以看到dhcp已经自动配置好
        [root@centos7 ~]# cat /etc/dhcp/dhcpd.conf  
        
    #4.下载启动的相关文件  
	[root@centos7 ~]# cobbler get-loaders
		
	#----此处实在是无法下载-执行:
            yum -y install syslinux
            cp /usr/share/syslinux/pxelinux.0 /var/lib/cobbler/loaders/
            cp /usr/share/syslinux/menu.c32 /var/lib/cobbler/loaders/
            systemctl restart cobblerd
            cobbler sync
            
    #5.修改菜单的标题信息(可选)
	[root@centos7 ~]# vim /etc/cobbler/pxe/pxedefault.template
            MENU TITLE Cobbler | http://www.shichong.org/
	[root@centos7 ~]#cobbler sync
	[root@centos7 ~]#cat /var/lib/tftpboot/pxelinux.cfg/default
        
    #6.导入CentOS系统的安装文件,生成相应的YUM源
	#挂载多个iso,或者把iso拷贝到机器里然后再挂载
        [root@centos7 ~]# lsblk
            sr0     11:0    1  3.7G  0 rom  
            sr1     11:1    1 10.3G  0 rom 
        [root@centos7 ~]# mkdir /mnt/centos{6,7}	
        [root@centos7 ~]# mount /dev/sr0 /mnt/centos6
        [root@centos7 ~]# mount /dev/sr1 /mnt/centos7
        #导入系统源文件生成仓库
        [root@centos7 ~]# cobbler import --name=centos-6.10-x86_64 --path=/mnt/centos6 --arch=x86_64
        [root@centos7 ~]# cobbler import --name=centos-7.9-x86_64 --path=/mnt/centos7 --arch=x86_64
        [root@centos7 ~]# du -sh /var/www/cobbler/ks_mirror/*
        #列出当前导入的linux发行版条目
	[root@centos7 ~]# cobbler distro list
	#列出当前菜单列表
	[root@centos7 ~]# cobbler profile list
		
                #------默认直接导入CentOS8.3会出现下面错误(针对于centos8)
                [root@centos7 ~]# cobbler import --name=centos-8.4-x86_64 --path=/mnt/centos8 --arch=x86_64
                    task started: 2021-01-18_162855_import
                    task started (id=Media import, time=Mon Jan 18 16:28:55 2021)
                    Found a candidate signature: breed=redhat, version=rhel8
                    No signature matched in /var/www/cobbler/ks_mirror/centos-8.3-x86_64
                    !!! TASK FAILED !!!   #出现No signature matched报错
                #修改下面的配置文件,修改第70行添加centos-linux
                [root@centos7 ~]# vim /var/lib/cobbler/distro_signatures.json
                    "rhel8": {
                            "signatures": [
                              "BaseOS"
                            ],
                            "version_file": "(redhat|sl|slf|centos-linux|oraclelinux|vzlinux)-release-(?!notes)([\\w]*-)*8(Server)*[\\.-]+(.*)\\.rpm",   #把这行centos改成centos-linux          
                    :wq
                [root@centos7 ~]#systemctl restart cobblerd
                #重新导入CentOS8
                [root@centos7 ~]# cobbler import --name=centos-8.4-x86_64 --path=/mnt/centos8 --arch=x86_64
                
    #7.准备kickstart文件,并关联至指定的YUM源
        #kickstart文件参考文末

        [root@centos7 ~]# rz
        #把kickstart文件传到Linux里
        [root@centos7 ~]# ls
            anaconda-ks.cfg  centos6.cfg  centos7.cfg  centos8.cfg
        [root@centos7 ~]# mv centos* /var/lib/cobbler/kickstarts/
        [root@centos7 ~]# ls
            anaconda-ks.cfg
        [root@centos7 ~]# ll /var/lib/cobbler/kickstarts/
            total 76
            -rw-r--r-- 1 root root  866 Dec 30 20:55 centos6.cfg
            -rw-r--r-- 1 root root 1384 Dec 30 20:55 centos7.cfg
        #将kickstart文件,关联指定的YUM源和生成菜单列表
        [root@centos7 ~]# cobbler profile add --name=CentOS-7.9_mini --distro=CentOS-7.9-x86_64 --kickstart=/var/lib/cobbler/kickstarts/centos7.cfg
        [root@centos7 ~]# cobbler profile add --name=CentOS-6.10_mini --distro=CentOS-6.10-x86_64 --kickstart=/var/lib/cobbler/kickstarts/centos6.cfg
        #查看菜单列表
        [root@centos7 ~]# cobbler profile list
            CentOS-6.10_mini
            CentOS-7.9_mini
            centos-6.10-x86_64
            centos-7.9-x86_64
        #删除多余列表
        [root@centos7 ~]# cobbler profile remove --name=centos-6.10-x86_64
	[root@centos7 ~]# cobbler profile remove --name=centos-7.9-x86_64


kickstarts 应答文件

centos7.cfg
#centos7.cfg 
        cat /var/lib/cobbler/kickstarts/centos7.cfg 
        #platform=x86, AMD64, or Intel EM64T
        #version=DEVEL
        # Install OS instead of upgrade
        install
        # Keyboard layouts
        keyboard 'us'
        # Root password
        rootpw --plaintext 123456
        # System language
        lang en_US
        # System authorization information
        auth  --useshadow  --passalgo=sha512
        # Use text mode install
        text
        firstboot --disable
        # SELinux configuration
        selinux --disabled


        # Firewall configuration
        firewall --disabled
        # Network information
        
         #指定网络静态或动态,network  --bootproto=static --device=eth0 --ip=10.0.0.123 --netmask=255.255.255.0 
        network  --bootproto=dhcp --device=ens0 
        
        
        # Reboot after installation
        reboot
        # System timezone
        timezone Asia/Shanghai
        # Use network installation
        
        
        #注意此行必须指定
        url --url=$tree			
        
        #修改内核参数,net.ifnames=0修改网卡名
        bootloader --append="net.ifnames=0" --location=mbr  
   
        # Partition clearing information
        clearpart --all
        # Disk partitioning information
        part / --fstype="xfs" --size=102400    				#分区
        part /boot --fstype="xfs" --size=1024 
        part /data --fstype="xfs" --size=51200 
        part swap --size=2048
        %post
        useradd shichong
        echo 123456 |passwd --stdin shichong
        mkdir /etc/yum.repos.d/backup
        mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup
        cat > /etc/yum.repos.d/base.repo <<EOF
        [base]
        baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
                http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
                http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
        gpgcheck=1
        gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
        EOF
        %end

        %packages
        vim
        wget
        lrzsz
        tree
        curl
        %end
centos6.cfg
#centos6.cfg 

        [root@centos7 ~]# cat /var/lib/cobbler/kickstarts/centos6.cfg 
        install
        text
        reboot
        #注意此行必须指定
        url --url=$tree
        lang en_US.UTF-8
        keyboard us
        network --onboot yes --device eth0 --bootproto dhcp  --noipv6
        rootpw --plaintext 123456
        firewall --disabled
        authconfig --enableshadow --passalgo=sha512
        selinux --disabled
        timezone Asia/Shanghai
        bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
        zerombr
        clearpart --all --initlabel
        part /boot --fstype=ext4 --size=1024
        part / --fstype=ext4 --size=102400
        part /data --fstype=ext4 --size=51200
        part swap --size=2048
        %packages
        @core
        @server-policy
        @workstation-policy
        autofs
        vim-enhanced
        %end
        %post
        useradd shichong 
        echo 123456 | passwd --stdin shichong &> /dev/null
        mkdir  /etc/yum.repos.d/backup
        mv /etc/yum.repos.d/* /etc/yum.repos.d/backup
        cat > /etc/yum.repos.d/base.repo <<EOF
        [base]
        name=base
         #此处是我挂载光盘的地方
        baseurl=file:///misc/cd
        gpgcheck=0
        EOF
        %end