#!/bin/bash

disable_lvm_lock(){
    tmpfile=$(/usr/bin/mktemp)
    sed -e 's/\(^[[:space:]]*\)locking_type[[:space:]]*=[[:space:]]*[[:digit:]]/\1locking_type = 1/' /etc/lvm/lvm.conf >"$tmpfile"
    status=$?
    if [[ status -ne 0 ]]; then
     echo "Failed to disable lvm lock: $status" >/dev/kmsg
     exit 1
    fi
    # replace lvm.conf. There is no need to keep a backup since it's an ephemeral file, we are not replacing the original in the initramfs image file
    mv "$tmpfile" /etc/lvm/lvm.conf
}

main() {
    name=$(basename "$0")
    start=$(/usr/bin/date +%s)
    disable_lvm_lock
    # run bigboot.sh to increase boot partition and file system size
    ret=$(sh /usr/bin/bigboot.sh -d="{{ bigboot_boot_device_name }}" -s="{{ bigboot_size }}" -b="{{ bigboot_boot_partition_number }}" -p="{{ bigboot_boot_device_partition_prefix }}" 2>/dev/kmsg)
    status=$?
    end=$(/usr/bin/date +%s)
    # write the log file
    if [[ $status -eq 0 ]]; then
        echo "[$name] Boot partition {{ bigboot_boot_device_name }} successfully increased by {{ bigboot_size }} ("$((end-start))" seconds) " >/dev/kmsg
    else
        echo "[$name] Failed to extend boot partition: $ret ("$((end-start))" seconds)" >/dev/kmsg
    fi
}

main "$0"
