#cloud-config
autoinstall:
  # version is an Autoinstall required field.
  version: 1

  # This adds the default ubuntu-desktop packages to the system.
  # Any desired additional packages may also be listed here.
  #packages:
  #  - ubuntu-desktop

  #proxy: http://store00.science.uoit.ca:3142
  #shutdown: poweroff

  # This adds the default snaps found on a 22.04 Ubuntu Desktop system.
  # Any desired additional snaps may also be listed here.
  #snaps:
  #  - name: firefox
  #  - name: gnome-3-38-2004
  #  - name: gtk-common-themes
  #  - name: snap-store
  #  - name: snapd-desktop-integration

  ssh:
    allow-pw: true
    install-server: true

  refresh-installer:
    update: no

  # User creation can occur in one of 3 ways:
  # 1. Create a user using this `identity` section.
  # 2. Create users as documented in cloud-init inside the user-data section,
  #    which means this single-user identity section may be removed.
  # 3. Prompt for user configuration on first boot.  Remove this identity
  #    section and see the "Installation without a default user" section.
  identity:
    realname: 'Science Administrator'
    username: science
    # A password hash is needed. `mkpasswd --method=SHA-512` can help.
    # mkpasswd can be found in the package 'whois'
    #password: '$5$CCOAg23FpFu6zLTR$IdcNjm1BErAfFEIm7Rf18rdLcr.nZy0m5wpRn5zCtC5'
    password: '$5$3Gs/r47RIwS/RfMT$YfdsNor/ep2tHw1B356sc80WNXjGnLVIw.7wA5ldsZA'
    hostname: mcs02

  # Default to LVM on an SSD
  storage:
    version: 1
    config:
      - id: disk-sda
        type: disk
        ptable: gpt
        match:
          largest: yes
          #ssd: yes
        #wipe: superblock-recursive
        #preserve: false
        preserve: true
        #path: /dev/sda
      - id: part-sda1
        type: partition
        size: 1G
        device: disk-sda
        grub_device: true
        flag: boot
        #offset: 1048576
        #number: 1
        preserve: true
        #partition_type: c12a7328-f81f-11d2-ba4b-00a0c93ec93b
        #path: /dev/sda1
      - id: format-part-sda1
        type: format
        fstype: vfat
        label: ESP
        volume: part-sda1
        preserve: true
      - id: mount-part-sda1
        type: mount
        device: format-part-sda1
        path: /boot/efi
      - id: part-sda2
        type: partition
        #size: 548679974912
        size: SDA2_SIZE
        # blockdev --getsize64 /dev/sda2
        device: disk-sda
        flag: linux
        #number: 2
        #partition_type: 0fc63daf-8483-4772-8e79-3d69d8477de4
        preserve: true
        #path: /dev/sda2
      - id: lvm-volgroup-vg0
        name: vg0
        type: lvm_volgroup
        devices:
          - part-sda2
        preserve: true
      - id: lvm-part-lvroot
        type: lvm_partition
        name: root
        size: 100G
        volgroup: lvm-volgroup-vg0
        wipe: superblock
        preserve: true
        #path: /dev/vg0/root
      - id: format-root
        type: format
        fstype: ext4
        volume: lvm-part-lvroot
        preserve: false
      - id: mount-root
        type: mount
        device: format-root
        path: /
      - id: lvm-part-lvhome
        type: lvm_partition
        name: home
        size: LVHOME_SIZE
        #size: 441303695360
        # blockdev --getsize64 /dev/vg0/home
        volgroup: lvm-volgroup-vg0
        preserve: true
        #path: /dev/vg0/home
      - id: format-home
        type: format
        fstype: ext4
        volume: lvm-part-lvhome
        preserve: true
      - id: mount-home
        type: mount
        device: format-home
        path: /

  # Ubuntu Desktop uses the hwe flavor kernel by default.
  early-commands:
    - echo 'linux-generic-hwe-22.04' > /run/kernel-meta-package
    - sed -i "s/SDA2_SIZE/$(blockdev --getsize64 /dev/sda2)/" /autoinstall.yaml
    - sed -i "s/LVHOME_SIZE/$(blockdev --getsize64 /dev/vg0/home)/" /autoinstall.yaml

  # The live-server ISO does not contain some of the required packages,
  # such as ubuntu-desktop or the hwe kernel (or most of their depdendencies).
  # The system being installed will need some sort of apt access.
  # proxy: http://192.168.0.1:3142

  late-commands:
    # Enable the boot splash
    #- >-
    #  curtin in-target --
    #  sed -i /etc/default/grub -e
    #  's/GRUB_CMDLINE_LINUX_DEFAULT=".*/GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"/'
    #- curtin in-target -- update-grub

    # Let NetworkManager handle network
    - rm /target/etc/netplan/00-installer-config*yaml
    - >-
      printf "network:\n  version: 2\n  renderer: NetworkManager"
      > /target/etc/netplan/01-network-manager-all.yaml

    # Remove other packages present by default in Ubuntu Server but not
    # normally present in Ubuntu Desktop.
    #- >-
    #  curtin in-target -- apt-get remove -y
    #  ubuntu-server ubuntu-server-minimal binutils byobu dmeventd finalrd gawk
    #  kpartx open-iscsi sg3-utils ssh-import-id sosreport screen open-vm-tools
    #  lxd-agent-loader landscape-common fonts-ubuntu-console

    # Keep cloud-init, as it performs some of the installation on first boot.
    - curtin in-target -- apt-get install -y cloud-init

    # Finally, remove things only installed as dependencies of other things
    # we have already removed.
    - curtin in-target -- apt-get autoremove -y

    # A postinstall script may optionally be used for further install
    # customization. Deploy this postinstall.sh script on the webserver.
    # - wget -O /target/postinstall.sh http://192.168.0.2/postinstall.sh
    # - curtin in-target -- bash /postinstall.sh
    # - rm /target/postinstall.sh

  # Additional cloud-init configuration affecting the target
  # system can be supplied underneath a user-data section inside of
  # autoinstall.
  # user-data:
  #   …
