#!/bin/bash
### BEGIN INIT INFO
# Provides:          lxc
# Required-Start:    $network $syslog
# Required-Stop:     $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: LXC Container Initialization and Autoboot Code
# Description:       Manage LXC containers and related services.
### END INIT INFO

# Paths to executables and PID files
LXCFS_EXEC="/usr/bin/lxcfs"
LXCFS_PIDFILE="/var/run/lxcfs.pid"
LXCFS_MOUNT="/sys/fs/cgroup"
LXC_NET_EXEC="/usr/libexec/lxc/lxc-net"
LXCMON_EXEC="/usr/libexec/lxc/lxc-monitord"
LXC_APPARMOR_EXEC="/usr/libexec/lxc/lxc-apparmor-load"
LXC_CONTAINERS_EXEC="/usr/libexec/lxc/lxc-containers"
PVE_SYSCALLD_EXEC="/usr/lib/x86_64-linux-gnu/pve-lxc-syscalld/pve-lxc-syscalld"
PVE_SYSCALLD_SOCKET="/run/pve/lxc-syscalld.sock"

case "$1" in
  start)
    echo "Starting LXC services..."

    # Mount cgroup2 if not already mounted
    if ! mountpoint -q $LXCFS_MOUNT; then
      echo "Mounting cgroup2..."
      mount -t cgroup2 none $LXCFS_MOUNT
      if [ $? -ne 0 ]; then
        echo "Failed to mount cgroup2 on $LXCFS_MOUNT"
        exit 1
      fi
    else
      echo "cgroup2 already mounted on $LXCFS_MOUNT"
    fi

    # Start lxcfs
    if [ ! -f $LXCFS_PIDFILE ]; then
      echo "Starting lxcfs..."
      start-stop-daemon --start --background --make-pidfile --pidfile $LXCFS_PIDFILE --exec $LXCFS_EXEC -- /var/lib/lxcfs
    fi

    # Start lxc-net
    echo "Starting lxc-net..."
    $LXC_NET_EXEC start

    # Start lxc-monitord
    if [ ! -f $LXCMON_PIDFILE ]; then
      echo "Starting lxc-monitord..."
      start-stop-daemon --start --background --exec $LXCMON_EXEC -- --daemon
    fi

    # Start pve-lxc-syscalld
    if [ ! -S $PVE_SYSCALLD_SOCKET ]; then
      echo "Starting pve-lxc-syscalld..."
      mkdir -p /run/pve
      start-stop-daemon --start --background --exec $PVE_SYSCALLD_EXEC -- --system $PVE_SYSCALLD_SOCKET
      if [ $? -ne 0 ]; then
        echo "Failed to start pve-lxc-syscalld"
        exit 1
      fi
    fi

    # Load AppArmor profiles
    echo "Loading AppArmor profiles for LXC..."
    $LXC_APPARMOR_EXEC

    # Start LXC containers
    echo "Starting LXC containers..."
    $LXC_CONTAINERS_EXEC start
    ;;
  
  stop)
    echo "Stopping LXC services..."

    # Stop LXC containers
    echo "Stopping LXC containers..."
    $LXC_CONTAINERS_EXEC stop

    # Stop lxc-net
    echo "Stopping lxc-net..."
    $LXC_NET_EXEC stop

    # Stop lxc-monitord
    echo "Stopping lxc-monitord..."
    start-stop-daemon --stop $LXCMON_EXEC

    # Stop pve-lxc-syscalld
    echo "Stopping pve-lxc-syscalld..."
    start-stop-daemon --stop $PVE_SYSCALLD_EXEC

    # Stop lxcfs
    if [ -f $LXCFS_PIDFILE ]; then
      echo "Stopping lxcfs..."
      start-stop-daemon --stop --pidfile $LXCFS_PIDFILE
      rm -f $LXCFS_PIDFILE
    fi
    ;;
  
  restart)
    echo "Restarting LXC services..."
    $0 stop
    sleep 2
    $0 start
    ;;
  
  status)
    echo "Checking status of LXC services..."
    if [ -f $LXCFS_PIDFILE ]; then
      echo "lxcfs is running (PID $(cat $LXCFS_PIDFILE))"
    else
      echo "lxcfs is not running"
    fi


    $LXC_NET_EXEC status
    $LXC_CONTAINERS_EXEC status
    ;;
  
  *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
    ;;
esac

exit 0
