CentOS 7 kickstart 文件,将条目添加到 /etc/hosts

CentOS 7 kickstart 文件,将条目添加到 /etc/hosts

我有一个 CentOS 7 kickstart 文件,大部分情况下运行良好。该%packages部分失败,因为我需要向 /etc/hosts 添加一个条目,以将 mirrorlist.centos.org 指向我们公司内部的存储库。我可以%post毫无顾虑地在该部分中添加该条目,但它似乎在软件包之后运行。我尝试将其添加到该%pre部分,但它从未出现在 /etc/hosts 中。根据文档,pre 部分在安装开始之前运行,所以我猜文件系统甚至还不存在。我可以将软件包移动到该%post部分并直接运行yum install <my package>,但我认为使用软件包部分会更简洁一些。这是我的完整 kick start 文件:

install
cdrom
lang en_US.UTF-8
keyboard us
network --bootproto=dhcp --noipv6 --onboot=on --device=eth0
rootpw --plaintext XXXX
firewall --disabled
selinux --permissive
timezone UTC
unsupported_hardware
bootloader --timeout=1 --location=mbr --append="net.ifnames=0 biosdevname=0"
text
skipx
zerombr
clearpart --all --initlabel
autopart --nohome --nolvm
auth --enableshadow --passalgo=sha512 --kickstart
firstboot --disabled
reboot --eject
user --name=vagrant --plaintext --password XXXX

%packages --nobase --ignoremissing --excludedocs --instLangs=en_US.utf8
# vagrant needs this to copy initial files via scp
openssh-clients
sudo
selinux-policy-devel
wget
nfs-utils
net-tools
tar
bzip2
deltarpm
rsync
qemu-guest-agent
-fprintd-pam
-intltool

# unnecessary firmware
-*firmware
-microcode_ctl
%end

%pre --log=/mnt/ks-pre.log
#!/bin/sh
echo X.X.X.X   mirrorlist.centos.org >> /etc/hosts
touch /mnt/hello
touch /root/hello
%end

%post --log=/root/ks-post.log
echo X.X.X.X mirrorlist.centos.org >> /etc/hosts
# sudo
echo 'Defaults:vagrant !requiretty' > /etc/sudoers.d/vagrant
echo '%vagrant ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers.d/vagrant
chmod 440 /etc/sudoers.d/vagrant

# Enable hyper-v daemons only if using hyper-v virtualization
if [ $(virt-what) == "hyperv" ]; then
    yum -y install hyperv-daemons cifs-utils
    systemctl enable hypervvssd
    systemctl enable hypervkvpd
fi

# Since we disable consistent network naming, we need to make sure the eth0
# configuration file is in place so it will come up.
# Delete other network configuration first because RHEL/C7 networking will not
# restart successfully if there are configuration files for devices that do not
# exist.
rm -f /etc/sysconfig/network-scripts/ifcfg-e*
cat > /etc/sysconfig/network-scripts/ifcfg-eth0 << _EOF_
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=eth0
DEVICE=eth0
ONBOOT=yes
_EOF_

#add our SSH key as a login method
mkdir /root/.ssh
chmod 700 /root/.ssh
cat >> /root/.ssh/authorized_keys << _EOF_
ssh-rsa xxxxxx
_EOF_
chmod 600 /root/.ssh/authorized_keys

#cloud init
yum install -y cloud-init
cat > /etc/cloud/cloud.cfg.d/90_dpkg.cfg << _EOF_
datasource_list: [ NoCloud, None ]
_EOF_
systemctl enable cloud-init

cat > /etc/cloud/cloud.cfg << _EOF_
users:
 - default

disable_root: 0
ssh_pwauth:   1

mount_default_fields: [~, ~, 'auto', 'defaults,nofail,x-systemd.requires=cloud-init.service', '0', '2']
resize_rootfs_tmp: /dev
ssh_deletekeys:   0
ssh_genkeytypes:  ~
syslog_fix_perms: ~
disable_vmware_customization: true

cloud_init_modules:
 - disk_setup
 - migrator
 - bootcmd
 - write-files
 - growpart
 - resizefs
 - set_hostname
 - update_hostname
 - update_etc_hosts
 - rsyslog
 - users-groups
 - ssh

cloud_config_modules:
 - mounts
 - locale
 - set-passwords
 - rh_subscription
 - yum-add-repo
 - package-update-upgrade-install
 - timezone
 - puppet
 - chef
 - salt-minion
 - mcollective
 - disable-ec2-metadata
 - runcmd

cloud_final_modules:
 - rightscale_userdata
 - scripts-per-once
 - scripts-per-boot
 - scripts-per-instance
 - scripts-user
 - ssh-authkey-fingerprints
 - keys-to-console
 - phone-home
 - final-message
 - power-state-change

system_info:
  default_user:
    name: centos
    lock_passwd: true
    gecos: Cloud User
    groups: [adm, systemd-journal]
    sudo: ["ALL=(ALL) NOPASSWD:ALL"]
    shell: /bin/bash
  distro: rhel
  paths:
    cloud_dir: /var/lib/cloud
    templates_dir: /etc/cloud/templates
  ssh_svcname: sshd

# vim:syntax=yaml
_EOF_

echo Complete

%end

答案1

根据@HBruijn 提供的信息,我只想回答我自己的问题。似乎 packages 部分仅用于从安装媒体安装软件包。这很合理,因为在操作系统安装阶段,您通常不会使用外部存储库。通常,您会先安装操作系统,然后再安装其他软件包。因此,为此使用 post 部分很合理。在那里,很容易添加条目/etc/hosts,然后安装其他软件包。

相关内容