#!/usr/bin/bash
# link VM in User-Dir in /tmp or /var/vm

set -eu

source /etc/lmn/vm.conf

# change to image-directory
cd "${VM_SYSDIR}"

while getopts ':p' OPTION; do
    case "$OPTION" in
        p)
            VM_DIR="${VM_DIR_PERSISTENT}"
            ;;
    esac
done

shift "$((OPTIND -1))"

# link system-VM-Images to User VM Directory
for i in *.qcow2; do
  [[ -f "${VM_DIR}/${i}" ]] || ln "${i}" "${VM_DIR}/${i}"
done

# allow lmnsynci to remove old vm images 
chgrp lmnsynci "${VM_DIR}"
chmod g+w "${VM_DIR}"
