Skip to content

Instantly share code, notes, and snippets.

@informationsea
Last active March 5, 2022 12:05
Show Gist options
  • Save informationsea/c7188db807c2ce0c7e8b1ac1813ed670 to your computer and use it in GitHub Desktop.
Save informationsea/c7188db807c2ce0c7e8b1ac1813ed670 to your computer and use it in GitHub Desktop.
Automated Alamlinux Install with KVM
#!/bin/bash
function install-almalinux-usage() {
cat <<EOF 1>&2
$0 [options] name
-l INSTALL_ISO AlmaLinux ISO [Example: ${VM_INSTALL_ISO}]
-b BOOTPROTO IPv4 boot protocol (dhcp/static) [Example: ${VM_IP_BOOTPROTO}]
-i IP_ADDR IPv4 address [Example: ${VM_IP_ADDR}]
-m IP_NETMASK IPv4 netmask [Example: ${VM_IP_NETMASK}]
-g GATEWAY IPv4 gateway [Example: ${VM_GATEWAY}]
-n NAME_SERVER IPv4 name server [Example: ${VM_NAMESERVER}]
-v NET_DEVICE Network device name in VM [Example: ${VM_NET_DEVIDE}]
-k NET_IF Network interface to attach [Example: ${VM_NET_IF}]
-o HOSTNAME Hostname [Example: ${VM_HOSTNAME}]
-d DISK_SIZE Disk size (in giga byte) [Example: ${VM_DISKSIZE_G}]
-r RAMSIZE_M RAM size (in mega byte) [Example: ${VM_RAMSIZE_M}]
-u CPU_NUM # of CPU [Example: ${VM_CPU_NUM}]
-t TIMEZONE Timezone [Example: ${VM_TIMEZONE}]
-p NTP_SERVER NTP Server [Example: ${VM_NTP_SEVER}]
-e ENCRYPTED_ROOTPW Encrypted root password [Create with \`python3 -c 'import crypt,getpass;pw=getpass.getpass();print(crypt.crypt(pw) if (pw==getpass.getpass("Confirm: ")) else exit())'\`]
-s ROOT_SSHKEY Root ssh key
EOF
exit 1
}
function install-almalinux() {
set -eu -o pipefail
VM_INSTALL_ISO=${DEFAULT_VM_INSTALL_ISO:-/nfs1/data/linux-iso/AlmaLinux-8.5-x86_64-dvd.iso}
VM_IP_BOOTPROTO=${DEFAULT_VM_IP_BOOTPROTO:-static}
VM_IP_ADDR=${DEFAULT_VM_IP_ADDR:-192.168.10.50}
VM_IP_NETMASK=${DEFAULT_VM_IP_NETMASK:-255.255.255.0}
VM_GATEWAY=${DEFAULT_VM_GATEWAY:-192.168.10.1}
VM_NAMESERVER=${DEFAULT_VM_NAMESERVER:-192.168.10.1}
VM_NET_DEVIDE=${DEFAULT_VM_NET_DEVIDE:-enp1s0}
VM_NET_IF=${DEFAULT_VM_NET_IF:-network=default}
VM_HOSTNAME=${DEFAULT_VM_HOSTNAME:-localhost.localdomain}
VM_DISKSIZE_G=${DEFAULT_VM_DISKSIZE_G:-50}
VM_RAMSIZE_M=${DEFAULT_VM_RAMSIZE_M:-4096}
VM_CPU_NUM=${DEFAULT_VM_CPU_NUM:-2}
VM_NTP_SEVER=${DEFAULT_VM_NTP_SEVER:-ntp.nict.jp}
VM_TIMEZONE=${DEFAULT_VM_TIMEZONE:-Asia/Tokyo}
VM_ENCRYPTED_ROOTPW=${DEFAULT_VM_ENCRYPTED_ROOTPW:-'$6$Eea/ka1jKRM4PYIs$5uklsxAC5kbxJ8hsijiKwvWGtGWBi9bL0L0PZzSm8G.XyZud9DWo85CaSCxiGNPLH0X8vTfL9hi29OKpE2uEQ.'}
VM_ROOT_SSHKEY=${DEFAULT_VM_ROOT_SSHKEY:-}
while getopts l:b:i:m:g:n:v:k:o:d:r:u:p:t:e:s:h\? OPT; do
case $OPT in
l) VM_INSTALL_ISO="$OPTARG" ;;
b) VM_IP_BOOTPROTO="$OPTARG" ;;
i) VM_IP_ADDR="$OPTARG" ;;
m) VM_IP_NETMASK="$OPTARG" ;;
g) VM_GATEWAY="$OPTARG" ;;
n) VM_NAMESERVER="$OPTARG" ;;
v) VM_NET_DEVIDE="$OPTARG" ;;
k) VM_NET_IF="$OPTARG" ;;
o) VM_HOSTNAME="$OPTARG" ;;
d) VM_DISKSIZE_G="$OPTARG" ;;
r) VM_RAMSIZE_M="$OPTARG" ;;
u) VM_CPU_NUM="$OPTARG" ;;
p) VM_NTP_SEVER="$OPTARG" ;;
t) VM_TIMEZONE="$OPTARG" ;;
e) VM_ENCRYPTED_ROOTPW="$OPTARG" ;;
s) VM_ROOT_SSHKEY="$OPTARG" ;;
h) install-almalinux-usage ;;
\?) install-almalinux-usage ;;
esac
done
shift $((OPTIND - 1))
VM_NAME=$1
if [ ${VM_IP_BOOTPROTO} = static ]; then
NETWORK_CONFIG="network --bootproto=${VM_IP_BOOTPROTO} --ip=${VM_IP_ADDR} --netmask= --gateway=${VM_GATEWAY} --nameserver=${VM_NAMESERVER} --device=${VM_NET_DEVIDE} --ipv6=auto --activate"
else
NETWORK_CONFIG="network --bootproto=dhcp"
fi
CONFIG_FILE=$(mktemp --suffix .cfg)
cat <<EOF > "${CONFIG_FILE}"
#version=RHEL8
# Use graphical install
graphical
repo --name="AppStream" --baseurl=file:///run/install/sources/mount-0000-cdrom/AppStream
%packages
@^minimal-environment
@guest-agents
@standard
kexec-tools
zsh
vim
%end
# Keyboard layouts
keyboard --xlayouts='jp'
# System language
lang ja_JP.UTF-8
# Network information
firewall --enable --ssh
${NETWORK_CONFIG}
network --hostname=${VM_HOSTNAME}
# Use CDROM installation media
cdrom
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=vda
# Partition clearing information
clearpart --none --initlabel
# Disk partitioning information
part /boot/efi --fstype="efi" --ondisk=vda --size=600 --fsoptions="umask=0077,shortname=winnt"
part /boot --fstype="xfs" --ondisk=vda --size=1024
part pv.103 --fstype="lvmpv" --ondisk=vda --grow --size=18854
volgroup almalinux --pesize=4096 pv.103
logvol swap --fstype="swap" --size=2047 --name=swap --vgname=almalinux
logvol / --fstype="xfs" --grow --size=1024 --name=root --vgname=almalinux
# System timezone
timezone ${VM_TIMEZONE} --isUtc --ntpservers=${VM_NTP_SEVER}
# Root password
rootpw --iscrypted ${VM_ENCRYPTED_ROOTPW}
%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
reboot
EOF
echo CONFIG=${CONFIG_FILE}
sudo virt-install --name ${VM_NAME} \
--os-type linux \
--os-variant centos8 \
--location "${VM_INSTALL_ISO}" \
--disk size=${VM_DISKSIZE_G}\
--graphics spice \
--vcpus=${VM_CPU_NUM} \
--ram=${VM_RAMSIZE_M} \
--initrd-inject "${CONFIG_FILE}" \
--network "${VM_NET_IF}" \
--extra-args "inst.ks=file://$(basename ${CONFIG_FILE})"
#--noautoconsole \
#echo CONFIG=${CONFIG_FILE}
#cat ${CONFIG_FILE}
rm ${CONFIG_FILE}
}
install-almalinux "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment