name: lxd
base: core20
assumes:
  - snapd2.39
version: 4.0.11-db2ed32
grade: stable
summary: LXD - container and VM manager
description: |-
 LXD is a system container and virtual machine manager.

 It offers a simple CLI and REST API to manage local or remote instances,
 uses an image based workflow and support for a variety of advanced features.

 Images are available for all Ubuntu releases and architectures as well
 as for a wide number of other Linux distributions. Existing
 integrations with many deployment and operation tools, makes it work
 just like a public cloud, except everything is under your control.

 LXD containers are lightweight, secure by default and a great
 alternative to virtual machines when running Linux on Linux.

 LXD virtual machines are modern and secure, using UEFI and secure-boot
 by default and a great choice when a different kernel or operating
 system is needed.

 With clustering, up to 50 LXD servers can be easily joined and managed
 together with the same tools and APIs and without needing any external
 dependencies.


 Supported configuration options for the snap (snap set lxd [<key>=<value>...]):

   - ceph.builtin: Use snap-specific Ceph configuration [default=false]
   - ceph.external: Use the system's ceph tools (ignores ceph.builtin) [default=false]
   - criu.enable: Enable experimental live-migration support [default=false]
   - daemon.debug: Increase logging to debug level [default=false]
   - daemon.group: Set group of users that have full control over LXD [default=lxd]
   - daemon.preseed: Pass a YAML configuration to `lxd init` on initial start
   - daemon.syslog: Send LXD log events to syslog [default=false]
   - daemon.verbose: Increase logging to verbose level [default=false]
   - lvm.external: Use the system's LVM tools [default=false]
   - lxcfs.pidfd: Start per-container process tracking [default=false]
   - lxcfs.loadavg: Start tracking per-container load average [default=false]
   - lxcfs.cfs: Consider CPU shares for CPU usage [default=false]
   - openvswitch.builtin: Run a snap-specific OVS daemon [default=false]
   - openvswitch.external: Use the system's OVS tools (ignores openvswitch.builtin) [default=false]
   - shiftfs.enable: Enable shiftfs support [default=auto]

contact: lxd@lists.canonical.com
issues: https://github.com/canonical/lxd/issues
source-code: https://github.com/canonical/lxd
website: https://ubuntu.com/lxd
confinement: strict

apps:
  # Main commands
  activate:
    command: commands/daemon.activate
    daemon: oneshot
    plugs:
      - lxd-support
      - system-observe

  daemon:
    command: commands/daemon.start
    reload-command: commands/daemon.reload
    stop-command: commands/daemon.stop
    stop-timeout: 600s
    restart-condition: on-failure
    daemon: simple
    slots:
      - lxd
    plugs:
      - lxd-support
      - network-bind
      - system-observe
    sockets:
      unix:
        listen-stream: $SNAP_COMMON/lxd/unix.socket
        socket-mode: 0660

  lxc:
    command: commands/lxc
    completer: etc/bash_completion.d/snap.lxd.lxc
    plugs:
      - lxd-support
      - system-observe

  lxd:
    command: commands/lxd
    plugs:
      - lxd-support
      - system-observe

  # Sub-commands
  benchmark:
    command: commands/lxd-benchmark
    plugs:
      - lxd-support
      - system-observe
  buginfo:
    command: commands/buginfo
    plugs:
      - lxd-support
      - system-observe
  check-kernel:
    command: commands/lxd-check-kernel
    plugs:
      - lxd-support
      - system-observe
  lxc-to-lxd:
    command: commands/lxc-to-lxd
    plugs:
      - lxd-support
      - system-observe
  migrate:
    command: commands/lxd-migrate
    plugs:
      - lxd-support
      - system-observe

hooks:
  configure:
    plugs:
      - lxd-support
      - network
      - system-observe
  remove:
    plugs:
      - lxd-support
      - system-observe

parts:
  # Dependencies
  btrfs:
    source: snapcraft/empty
    plugin: nil
    stage-packages:
      - btrfs-progs
    organize:
      sbin/: bin/
    prime:
      - bin/btrfs
      - bin/btrfstune
      - bin/mkfs.btrfs

  ceph:
    source: snapcraft/empty
    plugin: nil
    stage-packages:
      - ceph-common
    organize:
      usr/bin/: bin/
      usr/lib/: lib/
    prime:
      - bin/ceph
      - bin/rbd
      - lib/*/ceph
      - lib/*/libatomic.so*
      - lib/*/libboost_iostreams.so*
      - lib/*/libboost_program_options.so*
      - lib/*/libboost_thread.so*
      - lib/*/libcephfs*
      - lib/*/libibverbs.so*
      - lib/*/librados.so*
      - lib/*/librbd.so*
      - lib/*/librdmacm.so*
      - lib/*/libsnappy.so*
      - lib/python3

  criu:
    source: https://github.com/checkpoint-restore/criu
    source-tag: v3.17.1
    source-type: git
    source-depth: 1
    plugin: nil
    build-packages:
      - asciidoc
      - libcap-dev
      - libnet1-dev
      - libnl-3-dev
      - libprotobuf-c-dev
      - libprotobuf-dev
      - protobuf-c-compiler
      - protobuf-compiler
      - xmlto
    stage-packages:
      - libnet1
      - libprotobuf-c1
    override-build: |
      set -ex

      [ "$(uname -m)" != "x86_64" ] && \
        [ "$(uname -m)" != "armv7l" ] && \
        [ "$(uname -m)" != "aarch64" ] && \
        [ "$(uname -m)" != "s390x" ] && \
        [ "$(uname -m)" != "ppc64le" ] && exit 0

      make
      mkdir -p "${SNAPCRAFT_PART_INSTALL}/criu/"
      cp criu/criu "${SNAPCRAFT_PART_INSTALL}/criu/"
    organize:
      usr/lib/: lib/
    prime:
      - criu/*
      - lib/*/libnet*
      - lib/*/libproto*

  dqlite:
    after:
      - raft
      - sqlite
    source: https://github.com/canonical/dqlite
    source-commit: 50ee9af350b2fb4e79f9eb58db22c8a0927138de # few commits before v1.13.0
    source-type: git
    source-depth: 1
    plugin: autotools
    autotools-configure-parameters:
      - --prefix=
    stage-packages:
      - libuv1
    build-packages:
      - libuv1-dev
    organize:
      usr/lib/: lib/
    prime:
      - lib/libdqlite*so*
      - lib/*/libuv*

  edk2:
    after:
      - nasm
    source: https://github.com/tianocore/edk2
    source-type: git
    source-tag: IRRELEVANT
    source-depth: 1
    plugin: nil
    build-packages:
      - on amd64:
        - acpica-tools
        - nasm
        - uuid-dev
      - on arm64:
        - acpica-tools
        - nasm
        - uuid-dev
    override-pull: |-
      [ "$(uname -m)" != "x86_64" ] && [ "$(uname -m)" != "aarch64" ] && exit 0
      set -ex
      git clone https://github.com/tianocore/edk2 . -b edk2-stable202208

      # Pull submodules after switching to source-commit
      git submodule update --init --recursive

      git config user.email "noreply@lists.canonical.com"
      git config user.name "LXD snap builder"
    override-build: |-
      [ "$(uname -m)" != "x86_64" ] && [ "$(uname -m)" != "aarch64" ] && exit 0

      # Apply patches
      patch -p1 < "${SNAPCRAFT_PROJECT_DIR}/patches/edk2-0001-force-DUID-LLT.patch"
      cp "${SNAPCRAFT_PROJECT_DIR}/patches/edk2-0002-logo.bmp" MdeModulePkg/Logo/Logo.bmp
      patch -p1 < "${SNAPCRAFT_PROJECT_DIR}/patches/edk2-0003-boot-delay.patch"
      patch -p1 < "${SNAPCRAFT_PROJECT_DIR}/patches/edk2-0004-gcc-errors.patch"

      ARCH="X64"
      PKG="OvmfPkg/OvmfPkgX64.dsc"
      FV_CODE="OVMF_CODE"
      FV_VARS="OVMF_VARS"
      if [ "$(uname -m)" = "aarch64" ]; then
          ARCH="AARCH64"
          PKG="ArmVirtPkg/ArmVirtQemu.dsc"
          FV_CODE="QEMU_EFI"
          FV_VARS="QEMU_VARS"
      fi

      # Run in a bash sub-shell as edksetup.sh requires it
      set -ex
      (
      cat << EOF
          . ./edksetup.sh
          make -C BaseTools ARCH=${ARCH}
          build -a ${ARCH} -t GCC49 -b RELEASE -p ${PKG} \
            -DSECURE_BOOT_ENABLE=TRUE \
            -DNETWORK_IP4_ENABLE=TRUE \
            -DNETWORK_IP6_ENABLE=TRUE \
            -DNETWORK_TLS_ENABLE=TRUE \
            -DNETWORK_HTTP_BOOT_ENABLE=TRUE \
            -DFD_SIZE_2MB \
            -DTPM_ENABLE=TRUE \
            -DTPM_CONFIG_ENABLE=TRUE \
            -DTPM2_ENABLE=TRUE \
            -DTPM2_CONFIG_ENABLE=TRUE
      EOF
      ) | bash -e

      mkdir -p "${SNAPCRAFT_PART_INSTALL}/share/qemu/"
      cp Build/*/*/FV/${FV_CODE}.fd "${SNAPCRAFT_PART_INSTALL}/share/qemu/OVMF_CODE.fd"
      cp Build/*/*/FV/${FV_VARS}.fd "${SNAPCRAFT_PART_INSTALL}/share/qemu/OVMF_VARS.fd"

      if [ "$(uname -m)" = "aarch64" ]; then
          truncate -s 64m "${SNAPCRAFT_PART_INSTALL}/share/qemu/OVMF_CODE.fd"
          truncate -s 64m "${SNAPCRAFT_PART_INSTALL}/share/qemu/OVMF_VARS.fd"
      fi

    prime:
      - share/qemu/*

  libmnl:
    # XXX: Netfilter's git repo is unreliable
    source: https://www.netfilter.org/projects/libmnl/files/libmnl-1.0.5.tar.bz2
    source-checksum: sha256/274b9b919ef3152bfb3da3a13c950dd60d6e2bcd54230ffeca298d03b40d0525
    plugin: autotools
    autotools-configure-parameters:
      - --prefix=
    organize:
      usr/lib/: lib/
    prime:
      - lib/libmnl*so*

  libnftnl:
    after:
      - libmnl
    # XXX: Netfilter's git repo is unreliable
    source: https://www.netfilter.org/projects/libnftnl/files/libnftnl-1.2.4.tar.bz2
    source-checksum: sha256/c0fe233be4cdfd703e7d5977ef8eb63fcbf1d0052b6044e1b23d47ca3562477f
    plugin: autotools
    autotools-configure-parameters:
      - --prefix=
    organize:
      usr/lib/: lib/
    prime:
      - lib/libnftnl*so*
    override-build: |
      snapcraftctl build

      sed -i "s# /lib/libmnl.la# ${SNAPCRAFT_STAGE}/lib/libmnl.la#g" "${SNAPCRAFT_PART_INSTALL}/lib/libnftnl.la"

  libseccomp:
    source: https://github.com/seccomp/libseccomp
    source-type: git
    source-tag: v2.5.4
    source-depth: 1
    plugin: autotools
    autotools-configure-parameters:
      - --prefix=
    build-packages:
      - gperf
    organize:
      usr/lib/: lib/
    prime:
      - lib/libseccomp*so*

  liburing:
    source: https://github.com/axboe/liburing
    source-type: git
    source-tag: liburing-2.3
    source-depth: 1
    plugin: autotools
    autotools-configure-parameters:
      - --prefix=
    override-build: |-
      [ "$(uname -m)" != "x86_64" ] && [ "$(uname -m)" != "aarch64" ] && [ "$(uname -m)" != "ppc64le" ] && [ "$(uname -m)" != "s390x" ] && exit 0
      snapcraftctl build
    organize:
      usr/lib/: lib/
    prime:
      - lib/liburing*so*

  libusb:
    source: https://github.com/libusb/libusb
    source-type: git
    source-tag: v1.0.26
    source-depth: 1
    plugin: autotools
    autotools-configure-parameters:
      - --prefix=
    organize:
      usr/lib/: lib/
    prime:
      - lib/libusb*so*

  logrotate:
    source: snapcraft/empty
    plugin: nil
    stage-packages:
      - logrotate
    organize:
      usr/bin/: bin/
      usr/lib/: lib/
      usr/sbin/: bin/
    stage:
      - bin/logrotate
    prime:
      - bin/logrotate

  lvm:
    source: snapcraft/empty
    plugin: nil
    stage-packages:
      - dmeventd
      - lvm2
      - thin-provisioning-tools
    organize:
      sbin/: bin/
      usr/lib/: lib/
      usr/sbin/: bin/
    prime:
      - bin/cache_*
      - bin/dmeventd
      - bin/era_*
      - bin/lv*
      - bin/pdata_tools
      - bin/pv*
      - bin/thin_*
      - bin/vg*
      - -bin/vgimportclone
      - -bin/lvmconf
      - -bin/lvmdump
      - -bin/lvmetad
      - -bin/lvmpolld
      - etc/lvm/lvm.conf
      - lib/*/device-mapper/*
      - lib/*/libaio.so*
      - lib/*/libdevmapper*
      - lib/*/liblvm*
      - lib/*/libreadline.so*

  nano:
    source: snapcraft/empty
    plugin: nil
    stage-packages:
      - nano
    organize:
      usr/bin/: bin/
    prime:
      - bin/nano
      - etc/nanorc

  nasm:
    source: https://github.com/netwide-assembler/nasm
    source-type: git
    source-tag: nasm-2.15.05
    source-depth: 1
    plugin: autotools
    autotools-configure-parameters:
      - --prefix=
    organize:
      usr/bin/: bin/
    prime:
      - bin/nasm
    override-build: |
      patch -p1 < "${SNAPCRAFT_PROJECT_DIR}/patches/nasm-0000-disable-manpages.patch"
      snapcraftctl build

  nftables:
    after:
      - libmnl
      - libnftnl
    # XXX: Netfilter's git repo is unreliable
    source: https://www.netfilter.org/projects/nftables/files/nftables-1.0.5.tar.bz2
    source-checksum: sha256/8d1b4b18393af43698d10baa25d2b9b6397969beecac7816c35dd0714e4de50a
    plugin: autotools
    autotools-configure-parameters:
      - --prefix=
      - --with-json
    build-packages:
      - libedit-dev
      - libjansson-dev
      - libreadline-dev
    stage-packages:
      - libjansson4
    organize:
      sbin/: bin/
      usr/lib/: lib/
    prime:
      - bin/nft
      - lib/*/libjansson*so*
      - lib/libnftables*so*

  nvidia-container:
    after:
      - libseccomp
    source: https://github.com/NVIDIA/libnvidia-container
    source-commit: c8f267be0bac1c654d59ad4ea5df907141149977 # v1.11.0
    source-depth: 1
    source-type: git
    plugin: make
    build-environment:
      - GIT_TAG: "1.11.0" # Enables source-depth: 1, should match git tag without "v" prefix.
    build-packages:
      - bmake
      - curl
      - lsb-release
    override-build: |-
      set -ex

      [ "$(uname -m)" != "x86_64" ] && [ "$(uname -m)" != "aarch64" ] && exit 0

      git config user.email "noreply@lists.canonical.com"
      git config user.name "LXD snap builder"

      mkdir -p "${SNAPCRAFT_PART_INSTALL}/bin/"
      cp ${SNAPCRAFT_PROJECT_DIR}/snapcraft/wrappers/nvidia-container-cli "${SNAPCRAFT_PART_INSTALL}/bin/"

      set +ex
      snapcraftctl build
    organize:
      usr/local/bin/nvidia-container-cli: bin/nvidia-container-cli.real
      usr/local/lib: lib/
    prime:
      - bin/nvidia-container-cli*
      - lib/libnvidia-container*.so*

  openvswitch:
    source: https://github.com/openvswitch/ovs
    source-type: git
    source-tag: v3.0.1
    source-depth: 1
    plugin: autotools
    autotools-configure-parameters:
      - --enable-ssl
      - --prefix=
    stage-packages:
      - uuid-runtime
    override-build: |
      set -ex

      git config user.email "noreply@lists.canonical.com"
      git config user.name "LXD snap builder"

      set +ex
      snapcraftctl build
      set -ex
    organize:
      sbin/: bin/
      usr/bin/: bin/
    prime:
      - bin/ovs-appctl
      - bin/ovs-vsctl
      - bin/ovs-vswitchd
      - bin/ovsdb-*
      - bin/uuidgen
      - share/openvswitch/

  spice-protocol:
    source: https://gitlab.freedesktop.org/spice/spice-protocol
    source-type: git
    source-tag: v0.14.4
    source-depth: 1
    plugin: meson
    prime: []

  spice-server:
    after:
      - spice-protocol
    source: https://gitlab.freedesktop.org/spice/spice
    source-type: git
    source-tag: v0.15.1
    source-depth: 1
    plugin: meson
    meson-parameters:
      - --prefix=/
      - -Dgstreamer=no
      - -Dmanual=false
      - -Dlz4=false
      - -Dsasl=false
      - -Dopus=disabled
      - -Dsmartcard=disabled
      - -Dtests=false
    build-packages:
      - libjpeg-turbo8-dev
      - python3-pyparsing
      - python3-six
    stage-packages:
      - libjpeg-turbo8
      - libpixman-1-0
    organize:
      sbin/: bin/
      usr/lib: lib/
      usr/local/lib/: lib/
    prime:
      - lib/*/libjpeg*so*
      - lib/*/libspice-server*so*
      - lib/*/libpixman*so*

  qemu:
    after:
      - libseccomp
      - liburing
      - libusb
      - spice-protocol
      - spice-server
    source: IRRELEVANT
    source-type: git
    source-tag: IRRELEVANT
    source-depth: 1
    plugin: autotools
    autotools-configure-parameters:
      - --disable-bochs
      - --disable-cloop
      - --disable-dmg
      - --disable-docs
      - --disable-guest-agent
      - --disable-parallels
      - --disable-qed
      - --disable-slirp
      - --disable-user
      - --disable-vdi
      - --disable-vnc
      - --disable-xen
      - --enable-attr
      - --enable-cap-ng
      - --enable-kvm
      - --enable-libusb
      - --enable-usb-redir
      - --enable-linux-aio
      - --enable-linux-io-uring
      - --enable-numa
      - --enable-pie
      - --enable-rbd
      - --enable-seccomp
      - --enable-spice
      - --enable-system
      - --enable-tcg
      - --enable-tools
      - --enable-vhost-crypto
      - --enable-vhost-kernel
      - --enable-vhost-net
      - --enable-vhost-user
      - --enable-virtfs
      - --firmwarepath=/snap/lxd/current/share/qemu/
      - --localstatedir=/var/
    build-packages:
      - bison
      - flex
      - pkg-config
      - libaio-dev
      - libcap-ng-dev
      - libglib2.0-dev
      - libnuma-dev
      - libpixman-1-dev
      - librbd-dev
      - libusbredirhost-dev
    stage-packages:
      - genisoimage
      - libmagic1
      - libnuma1
      - libpixman-1-0
      - libusbredirhost1
      - libusbredirparser1
    override-pull: |-
      [ "$(uname -m)" != "x86_64" ] && [ "$(uname -m)" != "aarch64" ] && [ "$(uname -m)" != "ppc64le" ] && [ "$(uname -m)" != "s390x" ] && exit 0
      set -ex
      git clone https://gitlab.com/qemu/qemu . -b v7.1.0
    override-build: |-
      [ "$(uname -m)" != "x86_64" ] && [ "$(uname -m)" != "aarch64" ] && [ "$(uname -m)" != "ppc64le" ] && [ "$(uname -m)" != "s390x" ] && exit 0

      set -ex
      # Mangle the configure a bit
      QEMUARCH="$(uname -m)"
      [ "${QEMUARCH}" = "ppc64le" ] && QEMUARCH="ppc64"
      sed -i "s/^unset target_list$/target_list=\"${QEMUARCH}-softmmu\"/" configure
      sed -i 's#libseccomp_minver=".*#libseccomp_minver="0.0"#g' configure

      set +ex
      snapcraftctl build
    organize:
      usr/bin/: bin/
      usr/lib/: lib/
      usr/local/bin/: bin/
      usr/local/lib/: lib/
      usr/local/libexec/: bin/
      usr/local/share/: share/
    prime:
      - bin/genisoimage*
      - bin/mkisofs*
      - bin/qemu-system-*
      - bin/qemu-img*
      - bin/virtfs-proxy-helper*
      - bin/virtiofsd*
      - lib/*/libatomic.so*
      - lib/*/libmagic*so*
      - lib/*/libnuma*so*
      - lib/*/libpixman*so*
      - lib/*/libusbredir*so*
      - share/qemu/keymaps*
      - share/qemu/efi-virtio.rom*
      - share/qemu/kvmvapic.bin*
      - share/qemu/s390-*.img*
      - share/qemu/slof.bin*
      - share/qemu/vgabios-*.bin*

  qemu-ovmf-secureboot:
    after:
      - edk2
      - qemu
    source: https://github.com/puiterwijk/qemu-ovmf-secureboot
    source-commit: 4da81a4cbdd6d96b7a653239865d3b1d92cc6117 # master's HEAD
    source-type: git
    source-depth: 1
    plugin: nil
    build-packages:
      - xorriso
    override-build: |-
      [ "$(uname -m)" != "x86_64" ] && [ "$(uname -m)" != "aarch64" ] && exit 0

      export ARCH="$(basename $(readlink -f ${SNAPCRAFT_STAGE}/lib/*-linux-gnu*/))"
      export LD_LIBRARY_PATH="${SNAPCRAFT_STAGE}/lib:${SNAPCRAFT_STAGE}/lib/${ARCH}"

      set -ex
      rm -Rf iso-root vfat-root shell.iso
      mkdir -p iso-root vfat-root/efi/boot
      cp ../../edk2/build/Build/*/*/*/Shell.efi vfat-root/efi/boot/bootx64.efi
      cp ../../edk2/build/Build/*/*/*/EnrollDefaultKeys.efi vfat-root/
      "${SNAPCRAFT_STAGE}/bin/qemu-img" convert --image-opts driver=vvfat,floppy=on,fat-type=12,label=UEFI_SHELL,dir=vfat-root iso-root/shell.img
      xorriso --as mkisofs -input-charset ASCII -J -rational-rock -e shell.img -no-emul-boot -o shell.iso iso-root/

      # Basic aarch64 support
      if [ "$(uname -m)" = "aarch64" ]; then
          sed -i ovmf-vars-generator \
              -e "s/'-machine', machinetype,/'-machine', 'virt', '-cpu', 'cortex-a57',/" \
              -e "/charserial1/d" \
              -e "s/ide-cd/scsi-cd/" \
              -e "s/'-device',$/'-device', 'virtio-scsi-pci,id=scsi', '-device',/"
      elif [ "$(uname -m)" = "x86_64" ]; then
          cp -f "${SNAPCRAFT_STAGE}/share/qemu/kvmvapic.bin" .
      fi

      mkdir -p "${SNAPCRAFT_PART_INSTALL}/share/qemu/"
      python3 ovmf-vars-generator \
        --qemu-binary "${SNAPCRAFT_STAGE}/bin/qemu-system-$(uname -m)" \
        --print-output --disable-smm --skip-testing \
        --oem-string "$(cat ${SNAPCRAFT_PROJECT_DIR}/snapcraft/etc/ubuntu-sb.crt)" \
        --ovmf-binary "${SNAPCRAFT_STAGE}/share/qemu/OVMF_CODE.fd" \
        --ovmf-template-vars "${SNAPCRAFT_STAGE}/share/qemu/OVMF_VARS.fd" \
        --uefi-shell-iso shell.iso \
        "${SNAPCRAFT_PART_INSTALL}/share/qemu/OVMF_VARS.ms.fd"
    prime:
      - share/qemu/*

  raft:
    source: https://github.com/canonical/raft
    source-commit: abf9c42a9bb63c24920ab9f0bfbc4b7a47e7e5f4 # no proximate release tag/version
    source-type: git
    source-depth: 1
    plugin: autotools
    autotools-configure-parameters:
      - --prefix=
    stage-packages:
      - libuv1
      - liblz4-1
    build-packages:
      - libuv1-dev
      - liblz4-dev
    organize:
      usr/lib/: lib/
    prime:
      - lib/libraft*so*
      - lib/*/libuv.so*

  sqlite:
    source: https://github.com/sqlite/sqlite
    source-type: git
    source-depth: 1
    source-tag: version-3.40.0
    plugin: autotools
    autotools-configure-parameters:
      - --prefix=
    build-packages:
      - tcl
    prime:
      - bin/sqlite3
      - lib/libsqlite3*so*

  squashfs-tools-ng:
    source: https://github.com/AgentD/squashfs-tools-ng
    source-type: git
    source-tag: v1.1.4
    source-depth: 1
    plugin: autotools
    autotools-configure-parameters:
      - --prefix=
    build-packages:
      - liblzma-dev
    prime:
      - bin/sqfs2tar
      - bin/tar2sqfs
      - lib/libsquashfs.so*

  vim:
    source: snapcraft/empty
    plugin: nil
    stage-packages:
      - vim-common
      - vim-tiny
    organize:
      usr/bin/: bin/
      usr/share/vim/vim*/debian.vim: etc/vimrc
    prime:
      - bin/vim.tiny
      - etc/vimrc

  xfs:
    source: snapcraft/empty
    plugin: nil
    stage-packages:
      - xfsprogs
    organize:
      usr/sbin/: bin/
      sbin/: bin/
    prime:
      - bin/xfs_admin
      - bin/xfs_db
      - bin/xfs_growfs
      - bin/xfs_repair
      - bin/mkfs.xfs

  xtables:
    source: snapcraft/empty
    plugin: nil
    stage-packages:
      - arptables
      - ebtables
    organize:
      usr/lib/ebtables/: lib/
      usr/sbin/: bin/
    prime:
      - bin/arptables-legacy
      - bin/ebtables-legacy
      - etc/ethertypes
      - etc/protocols
      - lib/libebtc.so.*

  xz:
    source: snapcraft/empty
    plugin: nil
    stage-packages:
      - xz-utils
    organize:
      usr/bin/: bin/
      usr/lib/: lib/
      usr/sbin/: bin/
    prime:
      - bin/lzma
      - bin/xz
      - lib/*/liblzma*so*
    override-build: |
      snapcraftctl build

      # Include the lzma symlink
      ln -s xz "${SNAPCRAFT_PART_INSTALL}/usr/bin/lzma"

  zfs-0-6:
    source: https://github.com/openzfs/zfs
    source-type: git
    source-tag: zfs-0.6.5.11
    plugin: autotools
    autotools-configure-parameters:
      - --prefix=/
      - --with-config=user
    build-packages:
      - uuid-dev
      - zlib1g-dev
    override-build: |
      set -ex

      [ "$(uname -m)" != "x86_64" ] && \
        [ "$(uname -m)" != "aarch64" ] && \
        [ "$(uname -m)" != "s390x" ] && \
        [ "$(uname -m)" != "ppc64le" ] && exit 0

      git config user.email "noreply@lists.canonical.com"
      git config user.name "LXD snap builder"

      git cherry-pick fb963d33ee0dd350143ba1c9cd35d5f7d86910d2  # Fix endian build problem on ppc64el

      set +ex
      snapcraftctl build
      set -ex

      mv "${SNAPCRAFT_PART_INSTALL}" "${SNAPCRAFT_PART_INSTALL}.tmp"
      mkdir -p "${SNAPCRAFT_PART_INSTALL}/zfs-0.6/bin" "${SNAPCRAFT_PART_INSTALL}/zfs-0.6/lib"
      mv "${SNAPCRAFT_PART_INSTALL}.tmp/sbin/zfs" "${SNAPCRAFT_PART_INSTALL}/zfs-0.6/bin/"
      mv "${SNAPCRAFT_PART_INSTALL}.tmp/sbin/zpool" "${SNAPCRAFT_PART_INSTALL}/zfs-0.6/bin/"
      mv "${SNAPCRAFT_PART_INSTALL}.tmp/lib/udev/zvol_id" "${SNAPCRAFT_PART_INSTALL}/zfs-0.6/bin/"
      mv "${SNAPCRAFT_PART_INSTALL}.tmp/lib/"*so* "${SNAPCRAFT_PART_INSTALL}/zfs-0.6/lib/"
      rm -Rf "${SNAPCRAFT_PART_INSTALL}.tmp"

  zfs-0-7:
    source: https://github.com/openzfs/zfs
    source-type: git
    source-tag: zfs-0.7.13
    source-depth: 1
    plugin: autotools
    autotools-configure-parameters:
      - --prefix=/
      - --with-config=user
    build-packages:
      - libblkid-dev
      - uuid-dev
      - zlib1g-dev
    override-build: |
      set -ex

      [ "$(uname -m)" != "x86_64" ] && \
        [ "$(uname -m)" != "aarch64" ] && \
        [ "$(uname -m)" != "s390x" ] && \
        [ "$(uname -m)" != "ppc64le" ] && exit 0

      set +ex
      snapcraftctl build
      set -ex

      mv "${SNAPCRAFT_PART_INSTALL}" "${SNAPCRAFT_PART_INSTALL}.tmp"
      mkdir -p "${SNAPCRAFT_PART_INSTALL}/zfs-0.7/bin" "${SNAPCRAFT_PART_INSTALL}/zfs-0.7/lib"
      mv "${SNAPCRAFT_PART_INSTALL}.tmp/sbin/zfs" "${SNAPCRAFT_PART_INSTALL}/zfs-0.7/bin/"
      mv "${SNAPCRAFT_PART_INSTALL}.tmp/sbin/zpool" "${SNAPCRAFT_PART_INSTALL}/zfs-0.7/bin/"
      mv "${SNAPCRAFT_PART_INSTALL}.tmp/lib/udev/zvol_id" "${SNAPCRAFT_PART_INSTALL}/zfs-0.7/bin/"
      mv "${SNAPCRAFT_PART_INSTALL}.tmp/lib/"*so* "${SNAPCRAFT_PART_INSTALL}/zfs-0.7/lib/"
      rm -Rf "${SNAPCRAFT_PART_INSTALL}.tmp"

  zfs-0-8:
    source: https://github.com/openzfs/zfs
    source-type: git
    source-tag: zfs-0.8.6
    source-depth: 1
    plugin: autotools
    autotools-configure-parameters:
      - --prefix=/
      - --with-config=user
    build-packages:
      - libblkid-dev
      - libssl-dev
      - uuid-dev
      - zlib1g-dev
    override-build: |
      set -ex

      [ "$(uname -m)" != "x86_64" ] && \
        [ "$(uname -m)" != "aarch64" ] && \
        [ "$(uname -m)" != "s390x" ] && \
        [ "$(uname -m)" != "ppc64le" ] && exit 0

      set +ex
      snapcraftctl build
      set -ex

      mv "${SNAPCRAFT_PART_INSTALL}" "${SNAPCRAFT_PART_INSTALL}.tmp"
      mkdir -p "${SNAPCRAFT_PART_INSTALL}/zfs-0.8/bin" "${SNAPCRAFT_PART_INSTALL}/zfs-0.8/lib"
      mv "${SNAPCRAFT_PART_INSTALL}.tmp/sbin/zfs" "${SNAPCRAFT_PART_INSTALL}/zfs-0.8/bin/"
      mv "${SNAPCRAFT_PART_INSTALL}.tmp/sbin/zpool" "${SNAPCRAFT_PART_INSTALL}/zfs-0.8/bin/"
      mv "${SNAPCRAFT_PART_INSTALL}.tmp/lib/udev/zvol_id" "${SNAPCRAFT_PART_INSTALL}/zfs-0.8/bin/"
      mv "${SNAPCRAFT_PART_INSTALL}.tmp/lib/"*so* "${SNAPCRAFT_PART_INSTALL}/zfs-0.8/lib/"
      rm -Rf "${SNAPCRAFT_PART_INSTALL}.tmp"

  zfs-2-0:
    source: https://github.com/openzfs/zfs
    source-type: git
    source-tag: zfs-2.0.7
    source-depth: 1
    plugin: autotools
    autotools-configure-parameters:
      - --prefix=/
      - --with-config=user
    build-packages:
      - libblkid-dev
      - libssl-dev
      - uuid-dev
      - zlib1g-dev
    override-build: |
      set -ex

      [ "$(uname -m)" != "x86_64" ] && \
        [ "$(uname -m)" != "aarch64" ] && \
        [ "$(uname -m)" != "s390x" ] && \
        [ "$(uname -m)" != "ppc64le" ] && exit 0

      set +ex
      snapcraftctl build
      set -ex

      mv "${SNAPCRAFT_PART_INSTALL}" "${SNAPCRAFT_PART_INSTALL}.tmp"
      mkdir -p "${SNAPCRAFT_PART_INSTALL}/zfs-2.0/bin" "${SNAPCRAFT_PART_INSTALL}/zfs-2.0/lib"
      mv "${SNAPCRAFT_PART_INSTALL}.tmp/sbin/zfs" "${SNAPCRAFT_PART_INSTALL}/zfs-2.0/bin/"
      mv "${SNAPCRAFT_PART_INSTALL}.tmp/sbin/zpool" "${SNAPCRAFT_PART_INSTALL}/zfs-2.0/bin/"
      mv "${SNAPCRAFT_PART_INSTALL}.tmp/lib/udev/zvol_id" "${SNAPCRAFT_PART_INSTALL}/zfs-2.0/bin/"
      mv "${SNAPCRAFT_PART_INSTALL}.tmp/lib/"*so* "${SNAPCRAFT_PART_INSTALL}/zfs-2.0/lib/"
      rm -Rf "${SNAPCRAFT_PART_INSTALL}.tmp"

  zfs-2-1:
    source: https://github.com/openzfs/zfs
    source-type: git
    source-tag: zfs-2.1.15
    source-depth: 1
    plugin: autotools
    autotools-configure-parameters:
      - --prefix=/
      - --with-config=user
    build-packages:
      - libblkid-dev
      - libssl-dev
      - uuid-dev
      - zlib1g-dev
    override-build: |
      set -ex

      [ "$(uname -m)" != "x86_64" ] && \
        [ "$(uname -m)" != "aarch64" ] && \
        [ "$(uname -m)" != "s390x" ] && \
        [ "$(uname -m)" != "ppc64le" ] && exit 0

      set +ex
      snapcraftctl build
      set -ex

      mv "${SNAPCRAFT_PART_INSTALL}" "${SNAPCRAFT_PART_INSTALL}.tmp"
      mkdir -p "${SNAPCRAFT_PART_INSTALL}/zfs-2.1/bin" "${SNAPCRAFT_PART_INSTALL}/zfs-2.1/lib"
      mv "${SNAPCRAFT_PART_INSTALL}.tmp/sbin/zfs" "${SNAPCRAFT_PART_INSTALL}/zfs-2.1/bin/"
      mv "${SNAPCRAFT_PART_INSTALL}.tmp/sbin/zpool" "${SNAPCRAFT_PART_INSTALL}/zfs-2.1/bin/"
      mv "${SNAPCRAFT_PART_INSTALL}.tmp/lib/udev/zvol_id" "${SNAPCRAFT_PART_INSTALL}/zfs-2.1/bin/"
      mv "${SNAPCRAFT_PART_INSTALL}.tmp/lib/"*so* "${SNAPCRAFT_PART_INSTALL}/zfs-2.1/lib/"
      rm -Rf "${SNAPCRAFT_PART_INSTALL}.tmp"


  zstd:
    source: snapcraft/empty
    plugin: nil
    stage-packages:
      - zstd
    organize:
      usr/bin/: bin/
    prime:
      - bin/pzstd
      - bin/zstd

  # Core components
  lxc:
    after:
      - libseccomp
    source: https://github.com/lxc/lxc
    source-type: git
    source-tag: lxc-4.0.12
    build-packages:
      - libapparmor-dev
      - libcap-dev
      - libgnutls28-dev
      - libselinux1-dev
      - pkg-config
    plugin: autotools
    autotools-configure-parameters:
      - --prefix=
      - --disable-api-docs
      - --disable-bash
      - --disable-doc
      - --disable-examples
      - --disable-memfd-rexec
      - --disable-tests
      - --disable-tools
      - --enable-apparmor
      - --enable-seccomp
      - --enable-selinux
      - --enable-capabilities
      - --with-rootfs-path=/var/snap/lxd/common/lxc/
      - --libexecdir=/snap/lxd/current/libexec/
    organize:
      snap/lxd/current/lxc: lxc
      snap/lxd/current/libexec: libexec
      share/lxc/hooks: lxc/hooks
    prime:
      - bin/lxc-checkconfig
      - lib/liblxc.so.1
      - lib/liblxc.so.1.*
      - lxc/config/common.conf.d
      - lxc/hooks/nvidia
    override-build: |
      set -ex

      git config user.email "noreply@lists.canonical.com"
      git config user.name "LXD snap builder"

      git cherry-pick ca4c25c6e9ebb92f9e241ef9a231d4a715cb812d  # lxc-net: don't start by default inside lxc
      git cherry-pick f314419d1e054f7833b6976ec5ed32373aace622  # lxc-checkconfig: Fix bashism
      git cherry-pick 71ba7f65616e72a313e2a41615e449178da9daf2  # doc: Fix reverse allowlist/denylist
      git cherry-pick f7446b4e10d71f79f9f3952255608268842ee1f3  # cgroups: check that opened file descriptor is a cgroup filesystem
      git cherry-pick f1c4a17e7df5d819b1b170917865e2e458c8e5db  # cgroups: log fd of newly created cgroup
      git cherry-pick 8ef019a6ce2555f7b438b3841ab5216e5d6973ba  # doc: Fix reverse allowlist/denylist in Japanese man page
      git cherry-pick 3b9f84fd2397d06782bbf67dc8421463c43ab139  # ttys: ensure container_ttys= env variable is set correctly
      git cherry-pick 5ba5725cb4a210c25707beeca64fde5f561d1c71  # cgroups: modify cgroup2 attach logic

      set +ex
      snapcraftctl build
      set -ex

      mkdir -p $SNAPCRAFT_PART_INSTALL/lxc/config/common.conf.d/
      ln -s /var/snap/lxd/common/lxc/local.conf $SNAPCRAFT_PART_INSTALL/lxc/config/common.conf.d/01-local.conf
      sed -i "s#includedir=.*#includedir=$SNAPCRAFT_PART_INSTALL/include#g" $SNAPCRAFT_PART_INSTALL/lib/pkgconfig/lxc.pc
      sed -i "s#libdir=.*#libdir=$SNAPCRAFT_PART_INSTALL/lib#g" $SNAPCRAFT_PART_INSTALL/lib/pkgconfig/lxc.pc

  lxcfs:
    source: https://github.com/lxc/lxcfs
    source-type: git
    source-tag: lxcfs-4.0.12
    build-packages:
      - libfuse-dev
      - libpam0g-dev
      - pkg-config
    stage-packages:
      - fuse
    plugin: autotools
    autotools-configure-parameters:
      - --prefix=
      - --datarootdir=/snap/lxd/current/
      - --localstatedir=/var/snap/lxd/common/var/
    organize:
      snap/lxd/current/lxc: lxc
      snap/lxd/current/lxcfs: lxcfs
      lib/lxcfs: lib
    prime:
      - bin/fusermount
      - lib/*/libfuse.so.*

      - bin/lxcfs
      - lib/liblxcfs.so

      - lxc
      - lxcfs
    override-build: |
      set -ex

      git config user.email "noreply@lists.canonical.com"
      git config user.name "LXD snap builder"

      set +ex
      snapcraftctl build
      set -ex

      sed -i "s#\${LXC_ROOTFS_MOUNT}/var/snap/lxd/common/var/lib/lxcfs/#\${LXC_ROOTFS_MOUNT}/var/lib/lxcfs/#g" $SNAPCRAFT_PART_INSTALL/snap/lxd/current/lxcfs/lxc.mount.hook
      patch -p1 $SNAPCRAFT_PART_INSTALL/snap/lxd/current/lxcfs/lxc.mount.hook < "${SNAPCRAFT_PROJECT_DIR}/patches/lxcfs-0001-hook.patch"

  lxd:
    source: .
    after:
      - lxc
      - dqlite
      - sqlite
    build-packages:
      - libacl1-dev
      - libudev-dev
      - pkg-config
    build-snaps:
      - go/1.18/stable
    stage-packages:
      - acl
      - attr
      - dnsmasq-base
      - gdisk
      - iw
      - netbase
      - pciutils
      - pigz
      - rsync
      - squashfs-tools
      - usbutils
      - xdelta3
    plugin: nil
    override-pull: |
      snapcraftctl pull
      set -ex

      # Setup build environment
      export GOPATH=$(realpath ./.go)

      # Setup the GOPATH
      rm -Rf "${GOPATH}"
      mkdir -p "${GOPATH}/src/github.com/canonical"
      ln -s "$(pwd)" "${GOPATH}/src/github.com/canonical/lxd"

      # Download the dependencies
      go get -d -v ./...
    override-build: |
      set -ex

      # Git cherry-picks
      cd ../src
      git config user.email "noreply@lists.canonical.com"
      git config user.name "LXD snap builder"

      # Setup build environment
      export GOPATH=$(realpath ./.go)
      export CGO_CFLAGS="-I${SNAPCRAFT_STAGE}/include/ -I${SNAPCRAFT_STAGE}/usr/local/include/"
      export CGO_LDFLAGS="-L${SNAPCRAFT_STAGE}/lib/ -L${SNAPCRAFT_STAGE}/usr/local/lib/"
      export CGO_LDFLAGS_ALLOW="(-Wl,-wrap,pthread_create)|(-Wl,-z,now)"

      # Build the binaries
      go build -o "${SNAPCRAFT_PART_INSTALL}/bin/lxc" github.com/canonical/lxd/lxc
      go build -o "${SNAPCRAFT_PART_INSTALL}/bin/lxc-to-lxd" github.com/canonical/lxd/lxc-to-lxd
      go build -o "${SNAPCRAFT_PART_INSTALL}/bin/lxd" -tags=libsqlite3 github.com/canonical/lxd/lxd
      CGO_ENABLED=0 go build -o "${SNAPCRAFT_PART_INSTALL}/bin/lxd-agent" -tags=agent,netgo github.com/canonical/lxd/lxd-agent
      go build -o "${SNAPCRAFT_PART_INSTALL}/bin/lxd-benchmark" github.com/canonical/lxd/lxd-benchmark

      # Setup bash completion
      mkdir -p ${SNAPCRAFT_PART_INSTALL}/etc/bash_completion.d/
      cp scripts/bash/lxd-client ${SNAPCRAFT_PART_INSTALL}/etc/bash_completion.d/snap.lxd.lxc
    organize:
      usr/bin/: bin/
      usr/lib/: lib/
      usr/share/misc/: share/misc/
      var/lib/usbutils/usb.ids: share/misc/
      usr/sbin/: bin/
      sbin/: bin/
    prime:
      - bin/dnsmasq
      - bin/getfattr
      - bin/setfattr
      - bin/iw
      - bin/pigz
      - bin/rsync
      - bin/setfacl
      - bin/sgdisk
      - bin/unsquashfs
      - bin/xdelta3

      - lib/*/libidn.so.*

      - etc/bash_completion.d/snap.lxd.lxc

      - share/misc

      - bin/lxc
      - bin/lxc-to-lxd
      - bin/lxd
      - bin/lxd-agent
      - bin/lxd-benchmark

  lxd-migrate:
    source: lxd-migrate/
    after:
      - lxd
      - sqlite
    build-snaps:
      - go/1.18/stable
    plugin: nil
    override-pull: |
      snapcraftctl pull
      set -ex

      # Setup build environment
      export GOPATH=$(realpath ./.go)

      # Download the dependencies
      go get -d -v ./...
    override-build: |
      set -ex

      # Setup build environment
      export GOPATH=$(realpath ./.go)
      export CGO_CFLAGS="-I${SNAPCRAFT_STAGE}/include/ -I${SNAPCRAFT_STAGE}/usr/local/include/"
      export CGO_LDFLAGS="-L${SNAPCRAFT_STAGE}/lib/ -L${SNAPCRAFT_STAGE}/usr/local/lib/"

      # Build the binaries
      go build -o "${SNAPCRAFT_PART_INSTALL}/bin/lxd-migrate" -tags=libsqlite3 ./

      # Install bridge script
      mkdir -p ${SNAPCRAFT_PART_INSTALL}/bin/
      cp scripts/upgrade-bridge ${SNAPCRAFT_PART_INSTALL}/bin/upgrade-bridge
    prime:
      - bin/lxd-migrate
      - bin/upgrade-bridge

  shmounts:
    source: shmounts/
    plugin: make
    prime:
      - bin/setup-shmounts

  snap-query:
    source: snap-query/
    build-snaps:
      - go/1.18/stable
    plugin: nil
    override-build: |
      set -ex

      # Setup build environment
      export GOPATH=$(realpath ./.go)

      # Build the binaries
      go build -o "${SNAPCRAFT_PART_INSTALL}/bin/snap-query" snap-query.go
    prime:
      - bin/snap-query

  strip:
    source: snapcraft/empty
    after:
      - btrfs
      - ceph
      - dqlite
      - libseccomp
      - logrotate
      - lvm
      - nano
      - nvidia-container
      - openvswitch
      - raft
      - sqlite
      - squashfs-tools-ng
      - vim
      - xfs
      - xz
      - zfs-0-6
      - zfs-0-7
      - zfs-0-8
      - zfs-2-0
      - zfs-2-1
      - lxc
      - lxcfs
      - criu
      - lxd
      - lxd-migrate
      - shmounts
      - snap-query
    plugin: nil
    override-prime: |
      set -x

      # Strip some of the heavy bits
      strip -s ${SNAPCRAFT_PRIME}/bin/lxc
      strip -s ${SNAPCRAFT_PRIME}/bin/lxd*
      strip -s ${SNAPCRAFT_PRIME}/bin/snap*
      strip -s ${SNAPCRAFT_PRIME}/lib/liblxc*
      strip -s ${SNAPCRAFT_PRIME}/lib/libdqlite*
      strip -s ${SNAPCRAFT_PRIME}/lib/libsqlite*

      for zfs in zfs-0.6 zfs-0.7 zfs-0.8 zfs-2.0 zfs-2.1; do
          [ ! -d "${SNAPCRAFT_PRIME}/${zfs}" ] && continue
          strip -s ${SNAPCRAFT_PRIME}/${zfs}/bin/* ${SNAPCRAFT_PRIME}/${zfs}/lib/*
      done

      [ -e "${SNAPCRAFT_PRIME}/criu/criu" ] && strip -s ${SNAPCRAFT_PRIME}/criu/criu

      exit 0

  wrappers:
    plugin: dump
    source: snapcraft/
    organize:
      hooks/: snap/hooks/
      wrappers/editor: bin/
      wrappers/remote-viewer: bin/
