kernel:
  #image: nemunaire/kernel:5.10.62-0b705d955f5e283f62583c4e227d64a7924c138f-amd64
  image: linuxkit/kernel:5.15.110
  cmdline: "console=ttyS0 console=tty0"


init:
  - nemunaire/mdadm:8c8a476336087b395878e02e1d89008da6a38044
  - nemunaire/syslinux:086f221f281d577d300949aa1094fb20c5cd90dc
  - linuxkit/format:9c40b556691c1bf47394603aeb2dbdba21e7e32e
  - linuxkit/dm-crypt:526d32351c8246431be8e1a168cb514ff3c365af
  - linuxkit/metadata:501144d47215671e77b9cac44748a04f21236195
  - alpine:latest

files:
  - path: /init
    contents: |
      #!/bin/sh
      modprobe xhci_pci
      modprobe ahci
      modprobe megaraid_sas
      modprobe e1000e
      modprobe tg3
      modprobe bnxt_en

      echo -n "Waiting module loading... "
      sleep 3
      echo

      mount -t devtmpfs none /dev
      mount -t proc none /proc
      mount -t sysfs none /sys

      mdev -s
      mdadm --auto-detect

      if [ -b /dev/sdb ]; then
          DISKS="/dev/sda /dev/sdb"
          BOOT_PART=/dev/md2
          META_PART=/dev/md3
          SWAP_PART=/dev/md1
          ROOT_PART=/dev/md0
          RAID=1
      else
          DISKS="/dev/sda"
          BOOT_PART=/dev/sda1
          META_PART=/dev/sda2
          SWAP_PART=/dev/sda3
          ROOT_PART=/dev/sda4
          RAID=0
      fi

      ip link set eth0 up
      udhcpc -i eth0

      # /proc/cmdline parser (from Gentoo Wiki)
      cmdline() {
          local value
          value=" $(cat /proc/cmdline) "
          value="${value##* $1=}"
          value="${value%% *}"
          [ "$value" != "" ] && echo "$value"
      }

      # Retrieve metadata
      wget -O /tmp/metadata.iso "$(ip r | grep default | awk '{ print $3 }')/fickit-metadata.iso"
      mount /tmp/metadata.iso /mnt
      /usr/bin/metadata -v file=/mnt/user-data

      AUTOPREPARE=$(cmdline fickit.autoprepare)
      if [ -z "${AUTOPREPARE}" ]
      then
          # Try to detect backend/frontend setup
          if ip l | grep -q eth3
          then
              DEFAULT_BOOT=1
              echo -n "Detected: FRONTEND host "
          else
              DEFAULT_BOOT=0
              echo -n "Detected: BACKEND host "
          fi

          [ "${RAID}" -eq 1 ] && echo "with RAID setup" || echo "without raid"

          echo
          read -p "Proceed? (y/N/Front/Back) " V
          if [ "$V" == "F" ] || [ "$V" == "f" ]; then
              DEFAULT_BOOT=1
          elif [ "$V" == "B" ] || [ "$V" == "b" ]; then
              DEFAULT_BOOT=0
          elif [ "$V" != "y" ]; then
              while true; do
                  /bin/ash
              done
          fi
      elif [ "${AUTOPREPARE}" == "backend" ]
      then
          DEFAULT_BOOT=1
      elif [ "${AUTOPREPARE}" == "frontend" ]
      then
          DEFAULT_BOOT=0
      else
          echo
          echo "Invalid fickit.autoprepare value: got $AUTOPREPARE, expected frontend or backend."
          echo

          while true; do
              /bin/ash
          done
      fi

      # Create partition table and boot records
      for DISK in ${DISKS}
      do
        cat /etc/fdisk_cmd | fdisk "${DISK}" &&
          cat /etc/sfdisk_schema | sfdisk --force "${DISK}" ||
          /bin/ash
      done

      # Create RAID arrays
      if [ "${RAID}" -eq 1 ]; then
          /sbin/mdadm --create "${BOOT_PART}" --run --level=1 --metadata=1.0 --raid-devices=2 /dev/sda1 /dev/sdb1
          /sbin/mdadm --create "${META_PART}" --run --level=1 --metadata=1.1 --raid-devices=2 /dev/sda2 /dev/sdb2
          /sbin/mdadm --create "${SWAP_PART}" --run --level=1 --metadata=1.1 --raid-devices=2 /dev/sda3 /dev/sdb3
          /sbin/mdadm --create "${ROOT_PART}" --run --level=1 --metadata=0 --raid-devices=2 /dev/sda4 /dev/sdb4
      fi

      # Format partitions
      mkswap "${SWAP_PART}"
      #mkfs.ext4 -F "${ROOT_PART}"
      cryptsetup -q -s 512 luksFormat "${ROOT_PART}" /run/config/dm-crypt/key
      cryptsetup luksOpen -d /run/config/dm-crypt/key "${ROOT_PART}" crypt_fic
      mkfs.ext4 -F /dev/mapper/crypt_fic
      sync

      mkfs.vfat "${BOOT_PART}"
      mkdir -p /boot

      mount "${BOOT_PART}" /boot/ && {
          for DISK in ${DISKS}
          do
              /root/install_grub ${DEFAULT_BOOT} "${DISK}"
          done
          /root/update_imgs "$(ip r | grep default | awk '{ print $3 }')"
          dd if=/boot/imgs/fickit-metadata.iso of=${META_PART}
        } ||
        /bin/ash

      umount /boot &&
        sync

      echo "System is ready. You can now reboot."
      /bin/ash
    mode: "0755"

  - path: root/update_imgs
    source: configs/update_imgs.sh
    mode: "0755"

  - path: root/install_syslinux
    contents: |
      #!/bin/sh
      mkdir -p /boot/EFI/boot /boot/imgs

      [ $1 == "0" ] && ONTIMEOUT="backend" || ONTIMEOUT="frontend"

      cd /usr/share/syslinux/efi64
      cp ldlinux.e64 menu.c32 libcom32.c32 libutil.c32 vesamenu.c32 poweroff.c32 /boot/EFI/boot
      cp syslinux.efi /boot/EFI/boot/bootx64.efi
      cat <<EOF > /boot/syslinux.cfg
      TIMEOUT 30
      ONTIMEOUT ${ONTIMEOUT}

      MENU background         #00000000 * *
      MENU color title        * #FF22BBCC *
      MENU color sel          * #FFFFFFFF #FF22BBCC *
      MENU color hotsel       1;7;37;40 #ffffffff #76a1d0ff *

      UI vesamenu.c32
      MENU TITLE Server FIC Challenge

      LABEL backend
            MENU LABEL FIC Backend
            LINUX /imgs/fickit-kernel
            INITRD /imgs/fickit-boot-initrd.img
            APPEND console=ttyS0 console=tty0 root=fickit-backend-squashfs.img
      LABEL frontend
            MENU LABEL FIC Frontend
            LINUX /imgs/fickit-kernel
            INITRD /imgs/fickit-boot-initrd.img
            APPEND console=ttyS0 console=tty0 root=fickit-frontend-squashfs.img
      LABEL update
            MENU LABEL Update images
            LINUX /imgs/fickit-kernel
            INITRD /imgs/fickit-update-initrd.img
            APPEND console=ttyS0 console=tty0
      MENU SEPARATOR
      LABEL poweroff
            MENU LABEL ^Shutdown
            KERNEL poweroff.c32
      EOF

      cp /usr/share/syslinux/libcom32.c32 /usr/share/syslinux/libutil.c32 /usr/share/syslinux/poweroff.c32 /usr/share/syslinux/vesamenu.c32 /boot/

      shift
      for p
      do
          # BIOS part
          dd bs=440 conv=notrunc count=1 if=/usr/share/syslinux/mbr.bin of=${p}
          syslinux --install ${p}
      done
    mode: "0550"
  - path: root/install_grub
    contents: |
      #!/bin/sh
      mkdir -p /boot/EFI/boot /boot/grub /boot/imgs

      cat <<EOF > /boot/grub/grub.cfg
      set timeout=3
      set default=$1

      menuentry 'FIC Backend' {
      set root=(hd0,1)
      linux /imgs/fickit-kernel console=ttyS0 console=tty0 quiet root=fickit-backend-squashfs.img
      initrd /imgs/fickit-boot-initrd.img
      }

      menuentry 'FIC Frontend' {
      set root=(hd0,1)
      linux /imgs/fickit-kernel console=ttyS0 console=tty0 quiet root=fickit-frontend-squashfs.img
      initrd /imgs/fickit-boot-initrd.img
      }

      menuentry 'Update images' {
      set root=(hd0,1)
      linux /imgs/fickit-kernel console=ttyS0 console=tty0 quiet
      initrd /imgs/fickit-update-initrd.img
      }
      EOF
      cp "/boot/grub/grub.cfg" "/boot/EFI/boot/grub.cfg"

      shift
      for p
      do
          grub-mkimage -o "/boot/EFI/boot/bootx64.efi" -p /efi/boot -O x86_64-efi fat iso9660 part_gpt part_msdos normal boot linux configfile loopback chain efifwsetup efi_gop efi_uga ls search search_label search_fs_uuid search_fs_file gfxterm gfxterm_background gfxterm_menu test all_video loadenv exfat ext2
          grub-install --boot-directory="/boot/" --target=i386-pc "${p}"
      done
    mode: "0550"
  - path: etc/sfdisk_schema
    contents: |
      ,750M,U,*
      ,5M,L,-
      ,4G,S,-
      ,+,R,-
    mode: "0440"
  - path: etc/fdisk_cmd
    contents: |
      o
      w
    mode: "0440"
