#cloud-config
autoinstall:
  version: 1
  reboot: false

  locale: en_US.UTF-8
  timezone: UTC

  keyboard:
    layout: us

  identity:
    hostname: ubuntu-desktop
    username: ubuntu
    password: "$6$/kAgZbw6$efoYzY/CJGQnUEOg1M09zGFXD0EtsuW5OMMbKC013GKZ19ev4hDpBJGj9W/G.RK5mJQv.nGzccxpJdJu9Eaha0"

  updates: security

  storage:
    version: 2                        # <-- REQUIRED by schema
    config:
      # ------------------- DISKS -------------------
      - type: disk
        id: disk0
        match: { path: /dev/vda }
        ptable: gpt
        wipe: superblock-recursive
        preserve: false
        grub_device: true

      - type: disk
        id: disk1
        match: { path: /dev/vdb }
        ptable: gpt
        wipe: superblock-recursive
        preserve: false

      - type: disk
        id: disk2
        match: { path: /dev/vdc }
        ptable: gpt
        wipe: superblock-recursive
        preserve: false

      - type: disk
        id: disk3
        match: { path: /dev/vdd }
        ptable: gpt
        wipe: superblock-recursive
        preserve: false

      # ------------------- EFI PARTITIONS -------------------
      - type: partition
        id: efi0
        device: disk0
        size: 512M
        flag: boot
        wipe: superblock
        preserve: false

      # (efi1..efi3 will be created & synced in late-commands)

      # ------------------- /boot PARTITIONS -------------------
      - type: partition
        id: boot0
        device: disk0
        size: 1G
        wipe: superblock
        preserve: false

      - type: partition
        id: boot1
        device: disk1
        size: 1G
        wipe: superblock
        preserve: false

      - type: partition
        id: boot2
        device: disk2
        size: 1G
        wipe: superblock
        preserve: false

      - type: partition
        id: boot3
        device: disk3
        size: 1G
        wipe: superblock
        preserve: false

      # ------------------- ROOT PARTITIONS -------------------
      # Make these "consume the rest" to avoid size math issues
      - type: partition
        id: root0
        device: disk0
        size: 99488M
        wipe: superblock
        preserve: false

      - type: partition
        id: root1
        device: disk1
        size: 99488M
        wipe: superblock
        preserve: false

      - type: partition
        id: root2
        device: disk2
        size: 99488M
        wipe: superblock
        preserve: false

      - type: partition
        id: root3
        device: disk3
        size: 99488M
        wipe: superblock
        preserve: false

      # ------------------- RAID ARRAYS -------------------
      - type: raid
        id: mdboot
        name: mdboot
        raidlevel: 1
        devices: [boot0, boot1, boot2, boot3]

      - type: raid
        id: md0
        name: md0
        raidlevel: 10
        devices: [root0, root1, root2, root3]

      # ------------------- FILESYSTEMS -------------------
      - type: format
        id: md0-fs
        volume: md0
        fstype: ext4
        label: rootfs

      - type: format
        id: mdboot-fs
        volume: mdboot
        fstype: ext4
        label: bootfs

      - type: format
        id: efi0-fs
        volume: efi0
        fstype: fat32
        label: EFI

      # ------------------- MOUNTS -------------------
      - type: mount
        id: mount-root
        device: md0-fs
        path: /

      - type: mount
        id: mount-boot
        device: mdboot-fs
        path: /boot

      - type: mount
        id: mount-efi
        device: efi0-fs
        path: /boot/efi

  grub:
    reorder_uefi: true

  late-commands:
    # Create/replicate ESP on vdb/vdc/vdd and copy contents from the primary ESP
    - curtin in-target -- bash -euxo pipefail -c '
        for disk in /dev/vdb /dev/vdc /dev/vdd; do
          part="${disk}1"
          mkdir -p /mnt/esp
          mkfs.fat -F32 -n EFI "$part"
          mount "$part" /mnt/esp
          rsync -aHAX --delete /boot/efi/ /mnt/esp/
          umount /mnt/esp
        done
      '

    # Install GRUB entries for each disk’s ESP
    - curtin in-target -- bash -euxo pipefail -c '
        for disk in /dev/vdb /dev/vdc /dev/vdd; do
          part="${disk}1"
          mkdir -p /mnt/esp
          mount "$part" /mnt/esp
          grub-install --target=x86_64-efi --efi-directory=/mnt/esp --bootloader-id=ubuntu --recheck
          umount /mnt/esp
        done
      '
