#!/bin/sh
# Copyright (c) 2017-2019, AT&T Intellectual Property.
# All rights reserved.
#
# SPDX-License-Identifier: GPL-2.0-only
#
# Generate dependency to mount System Libvirt Partition
#

DESTDIR="${1:-/tmp}"

systemd_dir="/lib/systemd/system"
fs_target_wants="${DESTDIR}/local-fs.target.wants"

setup_libvirt_systemd() {
    cat <<EOF > ${systemd_dir}/var-lib-libvirt-images.mount
[Unit]
Description = Mount rule for libvirt partition

[Mount]
What = LABEL=LIBVIRT
Where = /var/lib/libvirt/images
Type = ext4
Options = acl
EOF
}

if ! grep -q vyatta-union /proc/cmdline; then
    exit
fi

if [ -e /dev/disk/by-partlabel/LIBVIRT ]; then
    mkdir -p "${fs_target_wants}"
    setup_libvirt_systemd
    ln -sf "$systemd_dir/var-lib-libvirt-images.mount" "${fs_target_wants}/"
fi

if [ -e /dev/disk/by-label/LIBVIRT ]; then
    mkdir -p "${fs_target_wants}"
    setup_libvirt_systemd
    ln -sf "$systemd_dir/var-lib-libvirt-images.mount" "${fs_target_wants}/"
fi

if [ -e /dev/md/LIBVIRT ]; then
    mkdir -p "${fs_target_wants}"
    setup_libvirt_systemd
    ln -sf "$systemd_dir/var-lib-libvirt-images.mount" "${fs_target_wants}/"
fi
