#!/usr/bin/env bash
# Real demos for P064 "How to Fix No Space Left on Device in Linux".
# One Ubuntu 24.04 LXD system container, "shop" (Ana's tea shop server), whose root disk is a real 2 GiB ext4
# volume: an LXD LVM storage pool (volume group ubuntu-vg, like Ubuntu Server's own) on a loop file on render01.
# The disk is filled on purpose, off-screen, as the shop's user (so ext4's 5% root reserve stays, as on a real
# server): first by the shop's request log, which the running app holds open, then by one session file per
# visitor until no inodes are left. Every terminal is a real login shell in a pty (demo_driver.py). Nothing on
# render01 itself is filled: the pool, its loop file and the container are deleted at the end.
#
# Needs: the LXD snap on render01 (ubuntu:24.04 image) and its lxdbr0 bridge (render01's Docker sets FORWARD to
# DROP, so a new bridge can't reach the Ubuntu archive; lxdbr0 can).
set -euo pipefail
cd "$(dirname "$0")"
C=shop
POOL=ce-p064
cleanup() {
  lxc delete -f $C >/dev/null 2>&1 || true
  lxc storage delete $POOL >/dev/null 2>&1 || true
}
if lxc info $C >/dev/null 2>&1 && ! lxc config get $C user.ce-demo 2>/dev/null | grep -q p064; then
  echo "an LXD container named $C exists and isn't ours: stop" >&2; exit 1
fi
cleanup
[ -n "${KEEP:-}" ] || trap cleanup EXIT
lxc storage create $POOL lvm size=4GiB lvm.vg_name=ubuntu-vg >/dev/null
lxc init -q ubuntu:24.04 $C --network lxdbr0 -s $POOL -d root,size=2GiB -c user.ce-demo=p064
lxc start $C
until lxc exec $C -- systemctl is-system-running 2>/dev/null | grep -qE 'running|degraded'; do sleep 1; done
lxc exec $C -- cloud-init status --wait >/dev/null || true
lxc exec $C -- hostnamectl set-hostname shop
lxc file push -q lab/shop.py lab/shop.service lab/logrotate-shop lab/fill_log.py lab/fill_sessions.py $C/root/
python3 - "$C" <demo_driver.py
