image:
  distribution: fedora

source:
  downloader: fedora-http
  url: https://kojipkgs.fedoraproject.org

targets:
  lxc:
    create_message: |
      You just created a {{ image.description }} container.
    config:
    - type: all
      before: 5
      content: |-
        lxc.include = LXC_TEMPLATE_CONFIG/fedora.common.conf

    - type: user
      before: 5
      content: |-
        lxc.include = LXC_TEMPLATE_CONFIG/fedora.userns.conf

    - type: all
      after: 4
      content: |-
        lxc.include = LXC_TEMPLATE_CONFIG/common.conf

    - type: user
      after: 4
      content: |-
        lxc.include = LXC_TEMPLATE_CONFIG/userns.conf

    - type: all
      content: |-
        lxc.arch = {{ image.architecture_kernel }}

files:
- path: /etc/default/grub
  generator: dump
  content: |-
    # Set the recordfail timeout
    GRUB_RECORDFAIL_TIMEOUT=0

    # Do not wait on grub prompt
    GRUB_TIMEOUT=0

    # Set the default commandline
    GRUB_CMDLINE_LINUX_DEFAULT="${GRUB_CMDLINE_LINUX_DEFAULT} console=tty1 console=ttyS0"

    # Set the grub console type
    GRUB_TERMINAL=console

    # Disable os-prober
    GRUB_DISABLE_OS_PROBER=true
  types:
  - vm

- path: /etc/dracut.conf.d/incus.conf
  generator: dump
  content: |-
    add_drivers+=" virtio_scsi "
  types:
  - vm

- generator: fstab
  types:
  - vm

- path: /etc/machine-id
  generator: dump

- path: /etc/fstab
  generator: dump
  types:
  - container

- path: /var/lib/dbus/machine-id
  generator: remove

- path: /etc/hostname
  generator: hostname

- path: /etc/hosts
  generator: hosts

- name: eth0.network
  path: /etc/systemd/network/eth0.network
  generator: dump
  content: |-
    [Match]
    Name=eth0

    [Network]
    DHCP=ipv4

    [DHCP]
    ClientIdentifier=mac
  variants:
  - default
  types:
  - container

- name: enp5s0.network
  path: /etc/systemd/network/enp5s0.network
  generator: dump
  content: |-
    [Match]
    Name=enp5s0

    [Network]
    DHCP=ipv4

    [DHCP]
    ClientIdentifier=mac
  variants:
  - default
  types:
  - vm

- name: meta-data
  generator: cloud-init
  variants:
  - cloud

- name: network-config
  generator: cloud-init
  content: |-
    {% if config_get("user.network-config", "") == "" %}version: 1
    config:
      - type: physical
        name: {% if instance.type == "virtual-machine" %}enp5s0{% else %}eth0{% endif %}
        subnets:
          - type: {% if config_get("user.network_mode", "") == "link-local" %}manual{% else %}dhcp{% endif %}
            control: auto
          - type: dhcp6
            control: auto{% else %}{{ config_get("user.network-config", "") }}{% endif %}
  variants:
  - cloud

- name: user-data
  generator: cloud-init
  variants:
  - cloud

- name: vendor-data
  generator: cloud-init
  variants:
  - cloud

- generator: incus-agent
  types:
  - vm

packages:
  manager: dnf
  update: true
  cleanup: true
  sets:
  - packages:
    - cpio
    - file
    - findutils
    - fipscheck
    - gettext
    - glibc-all-langpacks
    - hardlink
    - hostname
    - ipcalc
    - iproute
    - iproute-tc
    - iputils
    - kbd
    - kmod
    - openssh-clients
    - passwd
    - pigz
    - policycoreutils
    - procps-ng
    - sudo
    - systemd-udev
    - which
    - xz
    action: install

  - packages:
    - cloud-init
    action: install
    variants:
    - cloud

  - packages:
    - cloud-utils-growpart
    action: install
    types:
    - vm
    variants:
    - cloud

  - packages:
    - grub2-efi-x64
    action: install
    architectures:
    - x86_64
    types:
    - vm

  - packages:
    - grub2-efi-aa64
    action: install
    architectures:
    - aarch64
    types:
    - vm

  - packages:
    - kernel
    - shim
    - dracut
    action: install
    types:
    - vm

  - packages:
    - lvm2
    - lvm2-libs
    action: remove
    types:
    - vm

  - packages:
    - libsss_idmap
    - libsss_nss_idmap
    - sssd-client
    action: remove

actions:
- trigger: post-unpack
  action: |-
    #!/bin/sh

    set -eux

    # Increase connection timeout
    echo timeout=60 >> /etc/dnf/dnf.conf

    # Let dnf find the fastest mirror
    echo fastestmirror=True >> /etc/dnf/dnf.conf

    # Backup all repo files
    for repo in $(ls /etc/yum.repos.d/*.repo); do
      cp "${repo}" "${repo}.bak"
    done

- trigger: post-unpack
  action: |-
    #!/bin/sh

    set -eux

    # Use Fedora 38 GPG key to avoid package installation errors
    for repo in $(ls /etc/yum.repos.d/*.repo); do
      grep -q 'RPM-GPG-KEY-fedora-38' "${repo}" && continue

      sed -ri 's#^gpgkey=.*#\0 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-38-$basearch#g' "${repo}"
    done
  architectures:
  - aarch64
  - x86_64
  releases:
  - Rawhide

- trigger: post-unpack
  action: |-
    #!/bin/sh

    set -eux

    # Use baseurl instead of metalink to avoid networking issues
    for repo in $(ls /etc/yum.repos.d/*.repo); do
      grep -q '^#baseurl' "${repo}" || continue

      sed -ri 's/^metalink=.*/#\0/g;s@^#(baseurl=)http://download.example/pub/fedora/linux(.*)@\1https://muug.ca/mirror/fedora-secondary\2@g' "${repo}"
    done
  architectures:
  - ppc64le
  - s390x

- trigger: post-packages
  action: |-
    #!/bin/sh
    umount -l /etc/resolv.conf || true
    rm /etc/resolv.conf
    ln -sf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf
    systemctl enable systemd-resolved
    cp -R /etc/skel/.bash* /root/

- trigger: post-packages
  action: |-
    #!/bin/sh
    systemctl enable systemd-networkd
  variants:
  - default

- trigger: post-packages
  action: |-
    #!/bin/sh
    systemctl enable systemd-networkd
  variants:
  - cloud

- trigger: post-packages
  action: |-
    #!/bin/sh
    systemctl enable cloud-init
    systemctl enable cloud-config
    systemctl enable cloud-final
  variants:
  - cloud

- trigger: post-unpack
  action: |-
    #!/bin/sh
    # Generate machine-id in order for the kernel stuff to be configured properly
    systemd-machine-id-setup
  types:
  - vm

- trigger: post-files
  action: |-
    #!/bin/sh
    set -eux

    # Remove the "Container Image" string as this is going to be am VM
    sed -i 's/[ ]*(Container Image)//;/^VARIANT/d' /etc/os-release

    target=/boot/efi/EFI/fedora/grub.cfg
    grub2-mkconfig -o "${target}"
    sed -i "s#root=[^ ]*#root=${DISTROBUILDER_ROOT_UUID}#g" "${target}"

    sed -Eri "s#^(linux|initrd) .+/overlay/#\1 /#g" /boot/loader/entries/*
    sed -ri 's#^options .+#options $kernelopts#g' /boot/loader/entries/*

    # Regenerate initramfs
    kver=$(ls /boot/initramfs-*.img | sed -r 's#.*initramfs-(.+)\.img#\1#')
    dracut --kver "${kver}" -f

    # Unmask all masked unit files
    for f in $(find /etc/systemd/system -type l); do
      [ "$(readlink ${f})" = "/dev/null" ] && rm "${f}"
    done

    # Wipe grub environment
    rm -f /boot/efi/EFI/fedora/grubenv

    exit 0
  types:
  - vm

- trigger: post-files
  action: |-
    #!/bin/sh
    set -eux

    # Restore repos
    for repo in $(ls /etc/yum.repos.d/*.bak); do
      mv "${repo}" ${repo%.*}
    done
