# -*- mode: yaml; indent-tabs-mode: nil; tab-width: 2; coding: utf-8-unix -*-
---
- name: "DEBUG: ansible-gentoo_install disk.yml"
  debug:
    verbosity: 1
    msg: "DEBUG: ansible-gentoo_install disk.yml"

- name: test we are NOT in the chroot
  shell: |
    grep '/dev/{{AGI_NBD_DEV}}' /proc/mounts && exit 1
    [ -n "{{AGI_NBD_MP}}" ] || exit 2
    [ -d "{{AGI_NBD_MP}}" ] || exit 3
    [ "{{ansible_distribution}}" == 'Gentoo' ] || \
      ( {{AGI_GENTOO_FROM_MP}} != '' && '{{AGI_GENTOO_FROM_MP}}' != '/' ) || \
      exit 4
    [ -d "{{AGI_GENTOO_FROM_MP}}" ] || exit 5
  check_mode: false

- name: check for partitions
  shell: |
    grep '/dev/{{AGI_NBD_DEV}}p3' /proc/partitions && exit 0
    exit 1
  register: partitions_out
  failed_when: false
    
- block:
  # old code
  
  - name: create disklabel
    command: parted -s {{ AGI_install_disk }} mklabel {{ AGI_install_disklabel }}
    register: disklabel_out
    #   stderr: 'Warning: Error fsyncing/closing /dev/nbd1: Input/output error'
    failed_when: false

  - name: disklabel_out
    debug:
      var: disklabel_out
  # you can get into a wierd state with /dev/nbd - partprobe reports
  # Error: Partition(s) 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64 on /dev/nbd3 have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use.  As a result, the old partition(s) will remain in use.  You should reboot now before making further changes.
  # and the same thing on all of the rest of your disks! All untrue
  # but the qemu -d /dev/nbd1 command will fail silently if busy
    when: disklabel_out.rc != 0

  - name: This is fatal - you must reboot
    fail:
      msg: "This is fatal - you must reboot. {{disklabel_out.stderr}}"
    when:
      - disklabel_out.rc == 1
    check_mode: no

  # We need to leave a small gap at the beginning of the disk, or grub won't be
  # able to install to the MBR
  - name: create boot partition
    shell: |
      parted -s {{ AGI_install_disk }} mkpart primary {{'ext2' if AGI_install_bootloader != 'syslinux' else 'fat32'}} 1M 200M
    args:
      creates: "{{ AGI_install_disk }}p1"

  - name: mark boot partition as active
    shell: |
      parted -s {{ AGI_install_disk }} set 1 boot on

  - name: create swap partition
    shell: |
      parted -s {{ AGI_install_disk }} -- mkpart primary linux-swap 201M 2200M
    args:
      creates: "{{ AGI_install_disk }}p2"

  - name: create root partition
    shell: |
      parted -s {{ AGI_install_disk }} -- mkpart primary ext4 2201M 20070M
    args:
      creates: "{{ AGI_install_disk }}p3"

  - name: format boot partition
    filesystem: dev={{ AGI_install_disk }}p1 fstype={{'ext2' if AGI_install_bootloader != 'syslinux' else 'vfat'}} force=yes
    check_mode: false
    when: not ansible_check_mode

  - name: format swap partition
    filesystem: dev={{ AGI_install_disk }}p2 fstype=swap force=yes
    check_mode: false
    when: false

  - name: format root partition
    filesystem: dev={{ AGI_install_disk }}p3 fstype=ext4 force=yes
    check_mode: false
    when: not ansible_check_mode

  - name: label partitions
    shell: |
      partprobe
      e2label  {{ AGI_install_disk }}p3 root
      e2label  {{ AGI_install_disk }}p1 boot
      mkswap -L swap  "{{ AGI_install_disk }}p2"
    when: AGI_install_bootloader != 'syslinux'

  - name: label partitions syslinux
    shell: |
      partprobe
      fatlabel  {{ AGI_install_disk }}p3 root
      e2label  {{ AGI_install_disk }}p1 boot
      mkswap -L swap  "{{ AGI_install_disk }}p2"
    when: AGI_install_bootloader == 'syslinux'
      
  when: false

- block:

  - name: create disk partitions
    shell: |
      sfdisk {{ AGI_install_disk }} << EOF
      label: dos
      label-id: 0x14a8b958
      device: {{ AGI_install_disk }}
      unit: sectors
      sector-size: 512

      {{ AGI_install_disk }}p1 : start=        2048, size=      819200, type={{'83' if AGI_install_bootloader != 'syslinux' else 'c'}}, bootable
      {{ AGI_install_disk }}p2 : start=      821248, size=     4096000, type=82
      {{ AGI_install_disk }}p3 : start=     4917248, size=    37025792, type=83
      EOF
      partprobe
      
  - name: format disk partitions grub:2
    shell: |
      mke2fs -L boot {{ AGI_install_disk }}p1
      mke2fs -L root {{ AGI_install_disk }}p3
      mkswap -L swap  "{{ AGI_install_disk }}p2"

    when: AGI_install_bootloader != 'syslinux'

  - name: format disk partitions syslinux
    shell: |
      mkfs.vfat -F 32 -n boot {{ AGI_install_disk }}p1
      mke2fs -L root {{ AGI_install_disk }}p3
      mkswap -L swap  "{{ AGI_install_disk }}p2"

    when: AGI_install_bootloader == 'syslinux'

  when:
    - partitions_out.rc != 0


