Created
February 28, 2024 15:04
-
-
Save platu/1fd65398266c1cb10db581018acdfb10 to your computer and use it in GitHub Desktop.
IaC Lab01 customize a VM image for the lab environment with management VRF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script customizes a VM image for the lab environment | |
VM="$1" | |
shift | |
USER="$1" | |
shift | |
SECRET="$1" | |
shift | |
OOB="$1" | |
# Are the 4 parameters there ? | |
if [[ -z ${VM} || -z ${USER} || -z ${SECRET} || -z ${OOB} ]]; then | |
echo "Usage : $0 <image file name> <user name> <user password> <Out of Band VLAN id>" | |
exit 1 | |
fi | |
# Does the image file exist ? | |
if [[ ! -f ${VM} ]]; then | |
echo "File ${VM} not found" | |
exit 1 | |
fi | |
vm_name=$(basename "${VM}") | |
# Prepare network interface configuration files | |
if [[ ! -d "network" ]]; then | |
mkdir network | |
fi | |
cat <<EOF >network/01-lo.network | |
[Match] | |
Name=lo | |
[Network] | |
LinkLocalAddressing=ipv6 | |
Address=127.0.0.1/8 | |
EOF | |
cat <<EOF >network/10-mgmt-vrf.netdev | |
[NetDev] | |
Name=mgmt-vrf | |
Kind=vrf | |
[VRF] | |
TableId=2 | |
EOF | |
cat <<EOF >network/11-mgmt-vrf.network | |
[Match] | |
Name=mgmt-vrf | |
[Link] | |
ActivationPolicy=up | |
RequiredForOnline=no | |
EOF | |
cat <<EOF >network/20-mgmt-vlan.netdev | |
[NetDev] | |
Name=mgmt | |
Kind=vlan | |
[VLAN] | |
Id=${OOB} | |
EOF | |
cat <<EOF >network/21-mgmt-vlan.network | |
[Match] | |
Name=mgmt | |
Type=vlan | |
[Network] | |
Description=Out of band interface | |
VRF=mgmt-vrf | |
DHCP=yes | |
IPv6AcceptRA=true | |
[IPv6AcceptRA] | |
UseDNS=true | |
EOF | |
cat <<EOF >network/70-enp0s1.network | |
[Match] | |
Name=enp0s1 | |
Type=ether | |
[Network] | |
VLAN=mgmt | |
EOF | |
# Customize generic image | |
virt-customize \ | |
--format "qcow2" \ | |
--no-network \ | |
--run-command "adduser --gecos \"\" --disabled-password ${USER}" \ | |
--run-command "adduser ${USER} sudo" \ | |
--run-command "adduser ${USER} adm" \ | |
--password "${USER}:password:${SECRET}" \ | |
--copy-in network/01-lo.network:/etc/systemd/network/ \ | |
--copy-in network/10-mgmt-vrf.netdev:/etc/systemd/network/ \ | |
--copy-in network/11-mgmt-vrf.network:/etc/systemd/network/ \ | |
--copy-in network/20-mgmt-vlan.netdev:/etc/systemd/network/ \ | |
--copy-in network/21-mgmt-vlan.network:/etc/systemd/network/ \ | |
--copy-in network/70-enp0s1.network:/etc/systemd/network/ \ | |
--run-command "echo 'net.ipv4.tcp_l3mdev_accept=1' >> /etc/sysctl.conf" \ | |
--run-command "sed -i 's/#Port 22/Port 22\nPort 2222/' /etc/ssh/sshd_config" \ | |
--run-command "sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config" \ | |
--run-command "DEBIAN_FRONTEND=noninteractive dpkg-reconfigure openssh-server" \ | |
--uninstall cloud-init \ | |
-a "${VM}" >"${vm_name}"_customized.txt | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment