运维部署自动化实践(二)PXE+Preseed自动安装Ubuntu16.04 server

上一章:运维部署自动化实践(一)利用PXE远程自动化安装Ubuntu16.04 server

接上文

在上文中,尝试利用Kickstart工具实现PXE远程安装的自动化,但是安装过程中出现了一个分区报错需要人工确认,导致自动化过程中断。查阅了一些技术博客和官方文档,发现Kickstart主要用于Redhat系的linux系统安装,而对于Debian系的支持可能不太好。Debian系Linux采用了特有的debian-installer作为系统安装工具,而运维人员可以通过一个preseed.cfg配置文件,写入安装各个阶段需要的参数或指令,这样就可以避免安装过程中的人机交互,实现无人值守的自动化安装。
事实上上一文的过程也利用了preseed配置文件(在上文中ubuntu-server.seed文件),只不过大部分安装指令都写在了kickstart配置里。本文在没有kickstart工具的情况下,完全采用preseed配置实现ubuntu系统安装。

Install Ubuntu with Preseeding

配置dhcp、tftp、http服务

这一步可以直接复制上一章的操作,本文尝试用dnsmasq来代替isc-dhcp-server和tftpd-hpa,提供集成的dhcp和tftp服务

  1. dnsmasq

安装dnsmasq
sudo apt-get install dnsmasq
编辑/etc/dnsmasq.conf,将以下的配置配置激活(去掉注释)

bogus-priv
filterwin2k
interface=eth0  # 查看本机的网卡名
dhcp-range=192.168.1.50,192.168.1.150,12h  
dhcp-boot=pxelinux.0
enable-tftp
tftp-root=/var/ftpd  # tftp的目录
dhcp-authoritative

创建tftp目录
sudo mkdir /var/ftpd

启动dnsmasq
sudo service dnsmasq start

  1. Http
    安装apache2, 步骤省略,默认http目录为/var/www/html/
布置PXE文件
  1. 将ubuntu镜像mount到http目录下
    sudo mkdir /var/www/html/ubuntu
    sudo mount ~/Downloads/ubuntu-16.04.5-server-amd64.iso /var/www/html/ubuntu
  2. 拷贝启动文件到tftp目录
    sudo cp -r /var/www/html/ubuntu/install/netboot/* /var/ftpd/
  3. 在http根目录下创建preseed配置文件
    sudo touch /var/www/html/preseed.cfg
  4. 编辑preseed.cfg文件,内容如下:
# Locale sets language and country. 
d-i debian-installer/language string en
d-i debian-installer/country string US
d-i debian-installer/locale string en_US #应该时zh_CN

# Keyboard selection. 
d-i console-setup/ask_detect boolean false 
d-i console-setup/layoutcode string us 

# Network configuration. #注,在netboot模式下,网络设置不起作用,需要在dhcp中设定用户名密码,
d-i netcfg/choose_interface select eth0 
d-i netcfg/dhcp_timeout string 60 
d-i netcfg/get_hostname string libvert 
d-i netcfg/get_domain string libvert 
d-i netcfg/no_default_route boolean true 

# Clock and time zone setup 
d-i clock-setup/utc boolean false 
d-i time/zone string Asia/Shanghai 


# Mirror settings  #安装文件镜像设置,使用http协议
#d-i mirror/protocol string http 
#d-i mirror/country manual   #这一步要注释掉,否则会需要人工选择镜像所处地区
d-i mirror/http/hostname string 192.168.1.101  
d-i mirror/http/directory string /ubuntu  # 镜像路径 http://192.168.1.101/ubuntu
d-i mirror/http/proxy string 

# Partitioning ###分区设定,这个要注意
d-i partman-auto/disk string/dev/sda
d-i partman-auto/method string regular 
d-i partman-lvm/device_remove_lvm boolean true 
d-i partman-md/device_remove_md boolean true 
d-i partman-auto/choose_recipe select atomic
 # This makes partman automatically partition without confirmation
d-i partman/confirm_write_new_label boolean true 
d-i partman/choose_partition select finish 
d-i partman/confirm boolean true 
d-i partman/confirm_nooverwrite boolean true 

#add new 
d-i partman-lvm/confirm boolean true 
d-i partman-lvm/confirm_nooverwrite boolean true 
d-i partman-auto-lvm/guided_size string max 
# Base system installation 
d-i base-installer/kernel/image string linux-generic 

# Account setup 
#d-i passwd/root-login boolean true 
#d-i passwd/root-password password 123456 
#d-i passwd/root-password-again password 123456 
#d-i passwd/make-user boolean false  
#d-i user-setup/encrypt-home boolean false 
#d-i user-setup/allow-password-weak boolean true  #允许weakpassword,

### Account setup
# Skip creation of a root account (normal user account will be able to
# use sudo). The default is false; preseed this to true if you want to set
# a root password.
d-i passwd/root-login boolean false
# Alternatively, to skip creation of a normal user account.
#d-i passwd/make-user boolean false

# Root password, either in clear text
#d-i passwd/root-password password r00tme
#d-i passwd/root-password-again password r00tme
# or encrypted using a crypt(3)  hash.
#d-i passwd/root-password-crypted password [crypt(3) hash]

# To create a normal user account.
d-i passwd/user-fullname string DeepctrlUser
d-i passwd/username string deepctrl
# Normal user's password, either in clear text
d-i passwd/user-password password deepctrl
d-i passwd/user-password-again password deepctrl
# or encrypted using a crypt(3) hash.
#d-i passwd/user-password-crypted password [crypt(3) hash]
# Create the first user with the specified UID instead of the default.
#d-i passwd/user-uid string 1010
# The installer will warn about weak passwords. If you are sure you know
# what you're doing and want to override it, uncomment this.
d-i user-setup/allow-password-weak boolean true

# The user account will be added to some standard initial groups. To
# override that, use this.
d-i passwd/user-default-groups string audio cdrom video

# Set to true if you want to encrypt the first user's home directory.
d-i user-setup/encrypt-home boolean false

# Package selection 
tasksel tasksel/first multiselect standard, ubuntu-server 
d-i pkgsel/include string openssh-server 
d-i pkgsel/upgrade select none 
d-i pkgsel/language-packs multiselect en, zh 
d-i pkgsel/update-policy select none 

# Boot loader installation 
d-i grub-installer/only_debian boolean true 

# Finishing up the installation 
d-i finish-install/reboot_in_progress note 

#d-i live-installer/net-image string http://192.168.1.101/ubuntu/install/filesystem.squashfs
  1. 修改/var/ftpd/pxelinux.cfg/default
# D-I config version 2.0
# search path for the c32 support libraries (libcom32, libutil etc.)
path ubuntu-installer/amd64/boot-screens/
include ubuntu-installer/amd64/boot-screens/menu.cfg
default ubuntu-installer/amd64/boot-screens/vesamenu.c32
prompt 0
timeout 1
  1. 修改/var/ftpd/ubuntu-installer/amd64/boot-screens/txt.cfg 为以下内容:
default install
label install
    menu label ^Install
    menu default
    kernel ubuntu-installer/amd64/linux
    append initrd=ubuntu-installer/amd64/initrd.gz ramdisk_size=100000 auto=true priority=critical interface=auto netcfg/no_default_route=true preseed/url=http://192.168.1.101/preseed.cfg 
label cli
    menu label ^Command-line install
    kernel ubuntu-installer/amd64/linux
    append tasks=standard pkgsel/language-pack-patterns= pkgsel/install-language-support=false vga=788 initrd=ubuntu-installer/amd64/initrd.gz --- quiet

注意该步骤配置文件中没有再指定ks.cfg,后面的安装过程完全通过preseed.cfg控制

目标机启动,完成安装

Dell服务器进入PXE模式,安装过程无需再人工介入。

下一步继续实践在Preseed里添加后处理,执行自动安装软件和配置系统环境。

猜你喜欢

转载自blog.csdn.net/weixin_33963189/article/details/86913845