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

module_path:
    - local

global_settings:
    chroot: &chroot /srv/dmm/chroot
    diskimg: &diskimg /srv/dmm/disk.img
    loopdev: &loopdev loop7
    apt_depends: util-linux adduser parted e2fsprogs debootstrap

recipe:
    create_disk:
        module: files
        function: create_sparse_file
        destination: *diskimg
        size: "10G"
    create_partitions:
        module: disks
        function: run_parted_commands
        disk: *diskimg
        commands:
            - mklabel gpt
            - "mkpart primary 1M 50MB"
            - set 1 esp on
            - "mkpart primary 50MB 100%"
    loop_disk:
        module: disks
        function: losetup
        diskimg: *diskimg
        mountpoint: *chroot
        loopdev: *loopdev
    format_partitions:
        module: disks
        function: create_filesystems
        partitions:
            - esp:
              fstype: vfat
              partition: /dev/loop7p1
              options: ''
            - rootfs:
              fstype: ext4
              partition: /dev/loop7p2
              options: -b 4096
    mount_partitions:
        module: disks
        function: mount
        filesystems:
            - rootfs:
              source: /dev/loop7p2
              mountpoint: *chroot
              mountopts: -o noatime,delalloc,barrier=0
              fstype: ext4
            - esp:
              source: /dev/loop7p1
              mountpoint: /srv/dmm/chroot/boot/efi
              mountopts: ''
              fstype: vfat
    install_debian:
        module: debian_bootstrap
        function: debootstrap
        destination: /srv/dmm/chroot
        release: testing
        mirror: http://deb.debian.org/debian
        debootstrapopts: ''
    set_hostname:
        module: networking
        function: set_chroot_hostname
        chroot: *chroot
        hostname: buildvm
    add_users:
        module: users
        function: add_users
        users:
            - username: buildvm
              password: buildvm
              sudo: True
        chroot: *chroot
    apt_setup:
        module: debian
        function: update_sources
        chroot: *chroot
        sources_list: |
            deb http://deb.debian.org/debian unstable main
        sources_file: /etc/apt/sources.list
        update_sources: True
    install_linux:
        module: aptpkg 
        function: install
        chroot: *chroot
        packages: linux-image-amd64
    setup_bind_mounts:
        module: disks
        function: mount
        filesystems:
            - dev:
              source: /dev
              mountpoint: /srv/dmm/chroot/dev
              mountopts: ''
              fstype: devtmpfs
            - proc:
              source: /proc
              mountpoint: /srv/dmm/chroot/proc
              mountopts: ''
              fstype: proc
            - sys:
              source: /sys
              mountpoint: /srv/dmm/chroot/sys
              mountopts: ''
              fstype: sysfs
    setup_grub:
        module: grub
        function: grub_install
        chroot: *chroot
        platform: efi
    setup_virtual_machine:
        module: vmsetup
        function: vmsetup
        root_during_build: loop7p2
        root_dev_in_vm: vda2
        chroot: *chroot
    unmount_filesystems:
        module: disks
        function: umount
        mounts:
            - proc:
              mountpoint: /srv/dmm/chroot/proc
            - sys:
              mountpoint: /srv/dmm/chroot/sys
            - dev:
              mountpoint: /srv/dmm/chroot/dev
            - efi:
              mountpoint: /srv/dmm/chroot/boot/efi
            - rootfs:
              mountpoint: *chroot
    loop_disk_unmount:
        module: disks
        function: losetup_teardown
        loopdev: *loopdev

