#!/usr/bin/env dmm-perform-recipe

module_path:
    - local

global_settings:
    chroot: &chroot /tmp/disk
    diskimg: &diskimg /tmp/disk.img
    loopdev: &loopdev loop7
    apt_depends: util-linux adduser parted mkfs debootstrap

recipe:
    create_disk:
        module: fallocate
        destination: *diskimg
        size: "10G"
    create_partitions:
        module: parted
        destination: *diskimg
        static-layout:
            - mklabel gpt
            - "mkpart primary 1M 50MB"
            - set 1 esp on
            - "mkpart primary 50MB 100%"
    loop_disk:
        module: losetup
        function: setup
        diskimg: *diskimg
        mountpoint: *chroot
        loopdev: *loopdev
    format_partitions:
        module: mkfs
        partitions:
            - esp:
              fstype: vfat
              partition: /dev/loop7p1
              options: ''
            - rootfs:
              fstype: ext4
              partition: /dev/loop7p2
              options: -b 4096
    mount_partitions:
        module: mountfs
        partitions:
            - rootfs:
              source: /dev/loop7p2
              mountpoint: *chroot
              mountopts: -o noatime,delalloc,barrier=0
              fstype: ext4
            - esp:
              source: /dev/loop7p1
              mountpoint: /tmp/disk/boot/efi
              mountopts: ''
              fstype: vfat
    install_debian:
        module: debootstrap
        destination: /tmp/disk
        release: testing
        mirror: http://deb.debian.org/debian
        debootstrapopts: ''
    setup_network:
        module: networking
        chroot: *chroot
        hostname: buildvm
    add_users:
        module: users
        users:
            - username: buildvm
              password: buildvm
              sudo: True
    apt_setup:
        module: aptsetup
        chroot: *chroot
        sources-list: |
            deb http://deb.debian.org/debian unstable main
        sources-file: /etc/apt/sources.list
        update-sources: True
    install_linux:
        module: linux
        action: run
        chroot: *chroot
        package: linux-image-amd64
    setup_bind_mounts:
        module: mountfs
        partitions:
            - dev:
              source: /dev
              mountpoint: /tmp/disk/dev
              mountopts: ''
              fstype: devtmpfs
            - proc:
              source: /proc
              mountpoint: /tmp/disk/proc
              mountopts: ''
              fstype: proc
            - sys:
              source: /sys
              mountpoint: /tmp/disk/sys
              mountopts: ''
              fstype: sysfs
    setup_grub:
        module: grub
        action: install
        chroot: *chroot
        platform: efi
    setup_virtual_machine:
        module: vmsetup
        root-during-build: loop7p2
        root-dev-in-vm: vda2
        chroot: *chroot
    unmount_filesystems:
        module: umountfs
        mounts:
            - proc:
              mountpoint: /tmp/disk/proc
            - sys:
              mountpoint: /tmp/disk/sys
            - dev:
              mountpoint: /tmp/disk/dev
            - efi:
              mountpoint: /tmp/disk/boot/efi
            - rootfs:
              mountpoint: *chroot
    loop_disk_unmount:
        module: losetup
        function: unmount
        loopdev: *loopdev

