mooooore stuffs

This commit is contained in:
2026-04-11 00:26:51 -04:00
parent 6c728033f2
commit 128e85d98e
10 changed files with 184 additions and 6 deletions
+20 -1
View File
@@ -26,6 +26,25 @@
netcat
neovim
firefox
];
]
++ (
with lib; let
# this function extracts the base file name from a path.
basename = path: lib.lists.last (lib.strings.splitString "/" (toString path));
files = lib.filesystem.listFilesRecursive ./scripts;
in
# for each script found, create a derivation installed in $PATH
lib.lists.forEach files (
file: let
scriptName = strings.removeSuffix ".sh" (basename file);
in
pkgs.writeScriptBin
# (basename file) # the new package's name
scriptName
(builtins.readFile file)
)
);
}
+47
View File
@@ -0,0 +1,47 @@
#!/usr/bin/env bash
set -euo pipefail
USER="$1"
KEYFILE="$2"
CONTAINER="lxc-${USER}"
echo "Creating LXC container ${CONTAINER}..."
lxc-create -n "$CONTAINER" -t download -- -d ubuntu -r noble -a amd64
# start it
lxc-start -n "$CONTAINER"
# wait for networking
sleep 5
# set root password, install SSH, inject key
lxc-attach -n "$CONTAINER" -- bash -c "
apt-get update && apt-get install -y openssh-server
mkdir -p /root/.ssh
chmod 700 /root/.ssh
sed -i 's/#PermitRootLogin.*/PermitRootLogin prohibit-password/' /etc/ssh/sshd_config
systemctl enable ssh
systemctl restart ssh
"
# push the key in
cat "$KEYFILE" | lxc-attach -n "$CONTAINER" -- tee /root/.ssh/authorized_keys > /dev/null
lxc-attach -n "$CONTAINER" -- chmod 600 /root/.ssh/authorized_keys
# auto-start on boot
echo "lxc.start.auto = 1" >> "/var/lib/lxc/${CONTAINER}/config"
# get container IP
CONTAINER_IP=$(lxc-info -n "$CONTAINER" -iH | head -1)
# create host user that maps to this container
useradd -m -s /bin/bash -G labmates "$USER" 2>/dev/null || true
mkdir -p "/home/${USER}/.ssh"
cp "$KEYFILE" "/home/${USER}/.ssh/authorized_keys"
chown -R "${USER}:${USER}" "/home/${USER}/.ssh"
chmod 700 "/home/${USER}/.ssh"
# store mapping
echo "$CONTAINER" > "/home/${USER}/.lxc-container"
echo "Done. ${USER} SSH -> root@${CONTAINER} (${CONTAINER_IP})"
+11
View File
@@ -0,0 +1,11 @@
#!/usr/bin/env bash
set -euo pipefail
USER="$1"
CONTAINER=$(cat "/home/${USER}/.lxc-container" 2>/dev/null)
# [[ -n "$CONTAINER" ]] && lxc-stop -n "$CONTAINER" 2>/dev/null; lxc-destroy -n "$CONTAINER"
if [[ -n "$CONTAINER" ]]; then
lxc-stop -n "$CONTAINER" 2>/dev/null
lxc-destroy -n "$CONTAINER"
fi
userdel -r "$USER" 2>/dev/null || true
echo "Removed ${USER} and ${CONTAINER}"
+10
View File
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
printf "%-15s %-20s %-16s %s\n" "USER" "CONTAINER" "IP" "STATE"
for f in /home/*/.lxc-container; do
[[ -f "$f" ]] || continue
U=$(basename "$(dirname "$f")")
C=$(cat "$f")
IP=$(lxc-info -n "$C" -iH 2>/dev/null | head -1)
STATE=$(lxc-info -n "$C" -sH 2>/dev/null)
printf "%-15s %-20s %-16s %s\n" "$U" "$C" "${IP:-n/a}" "${STATE:-n/a}"
done
+17
View File
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
# CONTAINER=$(cat "/home/${USER}/.lxc-container" 2>/dev/null)
CONTAINER=$(cat "/home/$(whoami)/.lxc-container" 2>/dev/null)
if [[ -z "$CONTAINER" ]]; then
echo "No container assigned. Contact admin."
exit 1
fi
# ensure running
lxc-start -n "$CONTAINER" 2>/dev/null || true
if [[ -n "$SSH_ORIGINAL_COMMAND" ]]; then
exec lxc-attach -n "$CONTAINER" -- bash -c "$SSH_ORIGINAL_COMMAND"
else
exec lxc-attach -n "$CONTAINER" -- login -f root
fi