manintheit.org

manintheit.org


Ubuntu Autoinstall

The server installer for 20.04 supports a new mode of operation: automated installation, auto installation for short. You might also know this feature as unattended or hands-off or preseeded installation. Auto installation lets you answer all those configuration questions ahead of time with an autoinstall config and lets the installation process run without any interaction.

Providing the autoinstall config

The autoinstall config is provided via cloud-init configuration, which is almost endlessly flexible. Autoinstall requires the files user-data and meta-data. According to my short experience, autoinstall is not as mature as cloud-init is. Other than that, its documentation is shallow that you may need to test or dig into the Internet to understand some of the features. Nevertless, I am still able to install basic VM fully networked and necessary repos to install Kubernetes componenets without any interaction.

Experiment

In this experiment, I am going to provision a VM (ubuntu20.04.2) for my Kubernetes Management and Worker nodes. So after basic installation, autoinstall disable the swap, enable some kernel parameters, enable repos to Install Kubernetes componenets.

Mount ISO to /mnt

First thing is to mount the Ubuntu iso to /mnt folder on the KVM host.

sudo mount -o loop ubuntu-20.04.2-live-server-amd64.iso /mnt/

Providing the autoinstall Config

As it is mentioned autoinstall is provided via cloud-init configuration. Because of that you must provide user-data and meta-data which will be served on very simple HTTP Server that when the VM boots it will fetch the configuration via HTTP GET Request.

user-data

Following user-data will be copied to /www folder.

#cloud-config
autoinstall:
  version: 1
  early-commands:
    - systemctl stop ssh # otherwise packer tries to connect and exceed max attempts
  network:
    network:
      version: 2
      ethernets:
        eth0:
          addresses:
            - 10.5.100.23/24
          gateway4: 10.5.100.254
          nameservers: 
            search: [homelab.io]
            addresses: [8.8.8.8]
  apt:
    preserve_sources_list: false
    primary:
      - arches: [amd64]
        uri: "http://archive.ubuntu.com/ubuntu/"
  ssh:
    install-server: yes
    authorized-keys:
      - "your SSH pub key here"
    allow-pw: yes
  identity:
    hostname: ubuntu-00
    password: "$6$FhcddHFVZ7ABA4Gi$9l4yURWASWe8xEa1jzI0bacVLvhe3Yn4/G3AnU11K3X0yu/mICVRxfo6tZTB2noKljlIRzjkVZPocdf63MtzC0" # root
    username: ubuntu # root doesn't work
  packages:
    - apt-transport-https 
    - ca-certificates 
    - curl
  user-data:
    disable_root: false
  late-commands:
    - echo 'ubuntu ALL=(ALL) NOPASSWD:ALL' > /target/etc/sudoers.d/ubuntu
    - sed -ie 's/GRUB_CMDLINE_LINUX=.\*/GRUB_CMDLINE_LINUX="net.ifnames=0 ipv6.disable=1 biosdevname=0 console=ttyS0,115200n8"/' /target/etc/default/grub
    - curtin in-target --target /target update-grub2
    - swapoff -a
    - sed -ie '/\/swap.img/s/^/#/g' /target/etc/fstab
    - curl -vo /target/usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
    - |
      cat <<EOF |  tee /target/etc/modules-load.d/k8s.conf
      br_netfilter
      EOF
    - |
      cat <<EOF | sudo tee /target/etc/sysctl.d/k8s.conf
      net.bridge.bridge-nf-call-ip6tables = 1
      net.bridge.bridge-nf-call-iptables = 1
      EOF
    - sysctl --system
    - echo 'deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main' | tee /target/etc/apt/sources.list.d/kubernetes.list

meta-data

meta-data file will be copied to the /www folder.

touch /www/meta-data

Start HTTP Server

To serving user-data and meta-data via http protocol, Python’s built-in SimpleHTTPServer module is used. HTTP server listening on port 3003.

mkdir /www
cd /www
# If Python version returned above is 3.X
python3 -m http.server 3003
# If Python version returned above is 2.X
python -m SimpleHTTPServer 3003

Create VM on KVM

To create a VM with virt-install following parameters provided. Some of the parameters differs based on your environment.

#Static IP
#ip=<client-ip>:<server-ip>:<gw-ip>:<netmask>:<hostname>:<device>:<autoconf>:<dns0-ip>:<dns1-ip>:<ntp0-ip>
#For ubuntu net.ifnames=0 and biosdevname=0  for predictable NIC device name like eth0,eth1
virt-install \
--connect qemu:///system \
--name aiubuntu \
--os-variant ubuntu20.04 \
--os-type linux \
--ram 2048 \
--disk bus=virtio,pool=KVMs,size=15,format=qcow2 \
--network network=OVS0,model=virtio,virtualport_type=openvswitch,portgroup=VLAN100 \
--vcpus 2 \
--location '/home/tonyukuk/Downloads/ubuntu-20.04.2-live-server-amd64.iso',initrd=casper/initrd,kernel=casper/vmlinuz \
--extra-args='net.ifnames=0 biosdevname=0 ip=10.5.100.45::10.5.100.254:255.255.255.0:ubuntutest:eth0:none:10.5.100.253 autoinstall  ds=nocloud-net;s=http://10.5.100.10:3003/'


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.