image:
  distribution: openSUSE

source:
  downloader: opensuse-http
  url: https://mirrorcache-eu.opensuse.org/download
  keys:
  # 0x55A0B34D49501BB7CA474F5AA193FBB572174FC2
  - |-
    -----BEGIN PGP PUBLIC KEY BLOCK-----

    mQENBFImAl0BCACkjaXGvVLHBGTVXVP0khtpUVHqFvCRtaIIMHaX/5oTr3nyehDQ
    Ex9VLsSRcNa0QxtnCHFRQzjWWqe+i6pBginnSjucgmjnIKyJsF4l6R+rwAiinHQX
    C4s6Lqg/wH9xDPRBrMYFqlc/7MVf0Glhk1+lAxgQjolMt+5AbbrWlBbwc/i+++zl
    ES3MaeH8aiwup/ogjhmk0SbCQQ/ib21p3XWBwx2oz/KM6Voq9tKDvMczjzNRY3ZT
    6Di3FsUSKI7kgljiNiuN+675YwqEqxWEJgdE5a7Zb67giH1Ik08b5wQiF5jSAICD
    DxW7/ibWBvZJnqhqQT2xJpLC5VaJqwkN8o83ABEBAAG0PlZpcnR1YWxpemF0aW9u
    IE9CUyBQcm9qZWN0IDxWaXJ0dWFsaXphdGlvbkBidWlsZC5vcGVuc3VzZS5vcmc+
    iQE7BBMBAgAmBQJSJgJdAhsDBQkEHrAABgsJCAcDAgQVAggDBBYCAwECHgECF4AA
    CgkQoZP7tXIXT8ITnwf3SVUUoVjVLFCjhIxdet8BL011cJDwr9TwKEQfq4Ybsq5L
    5Y1/Zk86rTzrVOZrODLwNRIC3fMuegZV5f85KMggXu37Di+UvX+dQW9v1hte+hAT
    +gsqb60kOnE/Yacgkb6D3xIzRudAB2q/xfvHl/hgfn416yGI8NvntT7n4Hk9wT28
    9JSFkun0uaessg77aXlAdsqHwdugm9hELeva89OoYoiZ4d9r4ScTMSj0UkNgnh7g
    CyIScZHYqiiOeosUtAX9u1PyUFfFsg9s5snfud7aF48EfXU0RTtZAGKtG4GPDv3q
    bYc5TJ2pQzs9y5Bk/jAMR/QQw8CKglBsn1cjYkKViEYEExECAAYFAlImAl0ACgkQ
    OzARt2udZSO5yACgr6Ei7QZ+PAmg4Mr5db+4M3aepAEAniU33RaTKBCGkwQi6kHr
    4VaII2/E
    =l8DH
    -----END PGP PUBLIC KEY BLOCK-----

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

    - type: user
      before: 5
      content: |-
        lxc.include = LXC_TEMPLATE_CONFIG/opensuse.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/hostname
  generator: hostname

- path: /etc/hosts
  generator: hosts

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

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

- 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

    GRUB_USE_LINUXEFI="true"
  types:
  - vm

- name: ifcfg-eth0
  path: /etc/sysconfig/network/ifcfg-eth0
  generator: dump
  content: |-
    STARTMODE='auto'
    BOOTPROTO='dhcp'
  types:
  - container

- name: ifcfg-eth0
  path: /etc/sysconfig/network/ifcfg-eth0
  generator: dump
  content: |-
    STARTMODE='auto'
    BOOTPROTO='dhcp'
  types:
  - vm
  releases:
  - 15.5
  - 15.6

- name: ifcfg-enp5s0
  path: /etc/sysconfig/network/ifcfg-enp5s0
  generator: dump
  content: |-
    STARTMODE='auto'
    BOOTPROTO='dhcp'
  types:
  - vm
  releases:
  - tumbleweed

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

- name: network-config
  generator: cloud-init
  variants:
  - cloud

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

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

- generator: fstab
  types:
  - vm

- generator: incus-agent
  types:
  - vm

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

- path: /etc/fstab
  generator: dump
  content: "# empty fstab to silence cloud-init warnings"
  types:
  - container
  variants:
  - cloud

- path: /etc/polkit-1/rules.d/50-default.rules
  generator: dump
  content: |-
    /*
    * Allow user "opensuse" to execute any actions
    * without password authentication, similar to "sudo NOPASSWD:".
    * This is useful for the system settings as it will stop
    * asking for a password.
    */
    polkit.addRule(function(action, subject) {
        if (subject.user == "opensuse") {
            return polkit.Result.YES;
        }
    });
  types:
  - vm
  variants:
  - desktop-kde

- path: /etc/sddm.conf.d/autologin.conf
  generator: dump
  content: |-
    [Autologin]
    User=opensuse
    Session=default
    Relogin=true
  types:
  - vm
  variants:
  - desktop-kde
  releases:
  - 15.5
  - 15.6

- path: /etc/sddm.conf.d/autologin.conf
  generator: dump
  content: |-
    [Autologin]
    User=opensuse
    Session=plasmawayland
    Relogin=true
  types:
  - vm
  variants:
  - desktop-kde
  releases:
  - tumbleweed

- path: /home/opensuse/.config/kscreenlockerrc
  generator: dump
  content: |-
    [Daemon]
    Autolock=false
    LockOnResume=false
  types:
  - vm
  variants:
  - desktop-kde

- path: /etc/sudoers.d/90-incus
  generator: dump
  mode: 0440
  content: |-
    # User rules for opensuse
    opensuse ALL=(ALL) NOPASSWD: ALL
  types:
  - vm
  variants:
  - desktop-kde

packages:
  manager: zypper
  update: true
  cleanup: true
  sets:
  - packages:
    - apparmor-abstractions
    - apparmor-parser
    - dbus-1
    - elfutils
    - file
    - glib2-tools
    - gzip
    - hostname
    - iproute2
    - iputils
    - pigz
    - rsync
    - sudo
    - systemd-sysvinit
    - tar
    - which
    action: install

  - packages:
    - hardlink
    action: install
    releases:
    - tumbleweed

  - packages:
    - libdevmapper1_03
    action: install
    architectures:
    - ppc64le

  - packages:
    - xz
    action: install
    architectures:
    - aarch64
    - x86_64
    - i686

  - packages:
    - net-tools
    releases:
    - tumbleweed
    action: install

  - packages:
    - cloud-init
    - cloud-init-config-suse
    - fdupes
    action: install
    variants:
    - cloud

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

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

  - packages:
    - e2fsprogs
    - growpart
    - kernel-default
    action: install
    types:
    - vm

  - packages:
    - discover
    - dolphin
    - konsole
    - sddm
    action: install
    types:
    - vm
    variants:
    - desktop-kde

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

    systemd-machine-id-setup
    mount -t tmpfs tmpfs /sys/firmware
  types:
  - vm

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

    zypper install -y -t pattern kde

    # Reliable shutdown
    echo HandlePowerKey=poweroff >> /etc/systemd/logind.conf
    echo PowerKeyIgnoreInhibited=yes >> /etc/systemd/logind.conf

    # Timezone
    rm -f /etc/localtime
    ln -s /usr/share/zoneinfo/UTC /etc/localtime
    echo UTC > /etc/timezone

    # Locale
    echo LANG=en_US.UTF-8 > /etc/locale.conf

    # User
    USERNAME="opensuse"
    useradd ${USERNAME} -s /bin/bash -m -U -G video,users

    sed -ri "s#(DISPLAYMANAGER_AUTOLOGIN)=.*#\1=${USERNAME}#" /etc/sysconfig/displaymanager

    systemctl enable sddm

    # When calling `lxc stop` KDE will show a dialog for 30 seconds which defaults to logging out.
    # This decreases the timeout to 10 seconds, and performs a shutdown per default.
    config_file=/usr/share/plasma/look-and-feel/org.kde.breeze.desktop/contents/logout/Logout.qml
    sed -i 's/return root.haltRequested/return root.tempHaltRequested/g;s/return root.logoutRequest/return root.haltRequest/g;s/return root.tempHaltRequested/return root.logoutRequested/g;s/Logging out/TEMP/g;s/Shutting down/Logging out/g;s/TEMP/Shutting down/g;s/case ShutdownType.ShutdownTypeHalt/case ShutdownType.ShutdownTypeLogout/g;s/timeout: 30/timeout: 10/g' "${config_file}"
  types:
  - vm
  variants:
  - desktop-kde

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

    # Have the sysconfig network renderer support openSUSE Tumbleweed
    sed -i "s|'opensuse'|'opensuse-tumbleweed'|" $(find /usr -wholename '*/cloudinit/net/sysconfig.py')
  releases:
  - tumbleweed
  variants:
  - cloud

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

    # Enable the cloud-init systemd service
    systemctl enable cloud-init.service cloud-config.service cloud-final.service
  variants:
  - cloud

- trigger: post-packages
  action: |-
    #!/bin/sh
    umount -l /etc/resolv.conf || true
    rm /etc/resolv.conf
    ln -sf /var/run/netconfig/resolv.conf /etc/resolv.conf

- trigger: post-files
  action: |-
    #!/bin/sh
    set -eux
    umount -l /etc/resolv.conf || true
    rm /etc/resolv.conf
    ln -sf /var/run/netconfig/resolv.conf /etc/resolv.conf

    dracut --regenerate-all --force
    mount -t tmpfs tmpfs /sys/firmware
    mkdir /sys/firmware/efi
    grub2-mkconfig -o /boot/grub2/grub.cfg

    if which shim-install; then
     shim-install --no-nvram --removable
     shim-install --no-nvram
    else
     grub2-install --no-nvram --removable
     grub2-install --no-nvram
    fi

    grub2-mkconfig -o /boot/grub2/grub.cfg
    umount /sys/firmware
    sed -i "s#root=[^ ]*#root=${DISTROBUILDER_ROOT_UUID}#g" /boot/grub2/grub.cfg
  types:
  - vm

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

    # linuxefi and initrdefi don't seem to exist for aarch64.
    # Instead, we should use linux and initrd.
    sed -i "s#linuxefi#linux#g;s#initrdefi#initrd#g" /boot/grub2/grub.cfg
  types:
  - vm
  architectures:
  - aarch64

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

    # Automatic disk resize
    cat << EOF > /etc/systemd/system/incus-growpart.service
    [Unit]
    Description=Incus - grow root partition

    [Service]
    Type=oneshot
    ExecStartPre=-/usr/sbin/growpart /dev/sda 2
    ExecStart=/usr/sbin/resize2fs /dev/sda2

    [Install]
    WantedBy=default.target
    EOF
    systemctl enable incus-growpart
  types:
  - vm
  variants:
  - default
  - desktop-kde
