scripts: rewrite the desktop teardown for the mirror-based setup

cleanup-desktop.sh still described the era when Officer installed a desktop of
its own — XFCE on a TigerVNC Xvnc — so tearing the remote desktop down meant
deleting a desktop nobody else used. That stopped being true when the setup
moved to mirroring the machine's existing session with x11vnc, and the script
was left actively dangerous: it purged dbus-x11 and Brave, deleted ~/.vnc, and
reinstalled gnome-keyring, all of which the current GNOME setup depends on or
deliberately removes. Running it today broke the desktop rather than cleaning
it up.

Rewritten as the actual inverse of setup-desktop.sh:

- removes what Officer added — x11vnc, ~/.vnc, the GDM auto-login and forced
  Xorg keys, the forced EDID and its kernel command line, the login-time mode
  setter, the legacy officer-vnc unit, the VNC entries in .env
- keeps ubuntu-desktop, gdm3 and dbus-x11, which are the machine's own desktop
  and not Officer's to delete
- still purges XFCE and TigerVNC when present, since a host set up by the older
  script carries them and they are precisely what this undoes
- Brave is opt-in behind --purge-brave: setup installs it, but by teardown time
  it is usually just the user's browser with their profile in it

The GRUB and GDM edits were checked against copies of the real files: stripping
the EDID parameters leaves other kernel arguments intact wherever they sit in
the line, and the GDM revert does not disturb the commented examples that ship
in custom.conf. The package matcher names each xfce-family prefix rather than
globbing '^libxf', which would have taken libxfixes, libxft and libxfont with it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-04 11:02:16 +00:00
co-authored by Claude Opus 5
parent 4d2ec215a2
commit 16f5175205
+133 -34
View File
@@ -1,48 +1,147 @@
#!/bin/bash
set -euo pipefail
echo "=== Cleaning up Remote Desktop setup ==="
# Officer Remote Desktop teardown — the inverse of setup-desktop.sh.
#
# WHAT THIS DOES AND DOES NOT REMOVE, and why the distinction matters:
#
# Officer used to install a desktop of its OWN — XFCE on a TigerVNC Xvnc server — so tearing the
# remote desktop down meant deleting a desktop environment nobody else was using. That is no longer
# the model. Officer now MIRRORS the machine's existing physical session with x11vnc, which means
# ubuntu-desktop, gdm3 and dbus-x11 are the machine's own desktop, not Officer's. Removing them here
# would take the user's graphical session with it.
#
# So this script removes Officer's *configuration of* the desktop and the mirroring pieces, and
# leaves the desktop itself alone:
#
# removed x11vnc, ~/.vnc, the GDM auto-login + forced-Xorg keys, the forced EDID and its kernel
# command line, the login-time mode setter, the legacy officer-vnc systemd unit, and the
# VNC entries in .env
# kept ubuntu-desktop, gdm3, dbus-x11 — the machine's desktop
# legacy XFCE and TigerVNC are purged if present, since a host set up by an older version of
# setup-desktop.sh still carries them and they are exactly what this is undoing
#
# Brave is only removed with --purge-brave. setup-desktop.sh installs it, so removing it is the
# symmetric thing to do, but by the time anyone runs this it is usually just the user's browser with
# their profile in it. Opt in rather than surprise someone.
#
# Usage:
# bash scripts/cleanup-desktop.sh [--purge-brave]
#
# Reboot afterwards: the GDM and GRUB changes are both boot-time.
# Remove VNC config
echo "[1/5] Removing VNC config..."
PURGE_BRAVE=false
for arg in "$@"; do
case "$arg" in
--purge-brave) PURGE_BRAVE=true ;;
*) echo "unknown option: $arg" >&2; exit 2 ;;
esac
done
echo "=== Officer Remote Desktop teardown ==="
echo ""
# --- Step 1: stop the mirror and remove x11vnc ---
echo "[1/7] Stopping the screen mirror..."
# The VNC sidecar spawns x11vnc; kill the server itself rather than the sidecar, which PM2 owns.
pkill -x x11vnc 2>/dev/null || true
sudo DEBIAN_FRONTEND=noninteractive apt-get remove -y --purge x11vnc >/dev/null 2>&1 || true
rm -rf ~/.vnc
echo " x11vnc removed, ~/.vnc deleted."
# Remove Brave
echo "[2/5] Removing Brave..."
sudo apt remove -y --purge brave-browser 2>/dev/null || true
sudo rm -f /usr/share/keyrings/brave-browser-archive-keyring.gpg
sudo rm -f /etc/apt/sources.list.d/brave-browser-release.list
# --- Step 2: revert the GDM session configuration ---
# setup-desktop.sh forces Xorg (x11vnc cannot capture Wayland) and enables auto-login (so a user
# session owns the display for the mirror to attach to). Both are Officer's doing, so both go back.
echo "[2/7] Reverting GDM auto-login and the forced Xorg session..."
GDM_CONF=/etc/gdm3/custom.conf
if [ -f "$GDM_CONF" ]; then
sudo cp "$GDM_CONF" "$GDM_CONF.bak-$(date +%Y%m%d%H%M%S)"
# Auto-login off, and drop the username line entirely rather than leave a dangling account name.
sudo sed -i 's|^[[:space:]]*AutomaticLoginEnable=.*|AutomaticLoginEnable=false|' "$GDM_CONF"
sudo sed -i '/^[[:space:]]*AutomaticLogin=/d' "$GDM_CONF"
# Comment the Xorg forcing so the distro default (Wayland on Ubuntu) applies again.
sudo sed -i 's|^[[:space:]]*WaylandEnable=false.*|#WaylandEnable=false|' "$GDM_CONF"
echo " Auto-login disabled; Wayland restored to the distro default."
echo " ! The next boot will use Wayland. Nothing can mirror it — that is the point of this script,"
echo " but if you want the screen back later, re-run setup-desktop.sh."
else
echo " No $GDM_CONF — skipping."
fi
# --- Step 3: drop the forced EDID from the kernel command line ---
echo "[3/7] Removing the headless display forcing..."
if grep -q "drm.edid_firmware=" /etc/default/grub 2>/dev/null; then
sudo cp /etc/default/grub "/etc/default/grub.bak-$(date +%Y%m%d%H%M%S)"
# Strip both parameters, then collapse the double space they leave behind.
sudo sed -i -E 's/[[:space:]]*drm\.edid_firmware=[^" ]*//g; s/[[:space:]]*video=[^" ]*//g' /etc/default/grub
sudo sed -i -E 's/GRUB_CMDLINE_LINUX_DEFAULT="[[:space:]]+/GRUB_CMDLINE_LINUX_DEFAULT="/; s/[[:space:]]+"$/"/' /etc/default/grub
sudo update-grub >/dev/null 2>&1
echo " Kernel command line cleaned and GRUB regenerated (takes effect on reboot)."
else
echo " No forced EDID in GRUB — skipping."
fi
sudo rm -f /lib/firmware/edid/officer-screen.bin /lib/firmware/edid/officer-connector
# Only remove the directory if Officer's files were the only thing in it.
sudo rmdir /lib/firmware/edid 2>/dev/null || true
# --- Step 4: remove the login-time mode setter ---
echo "[4/7] Removing the display mode setter..."
rm -f ~/.local/bin/officer-set-display.sh ~/.config/autostart/officer-set-display.desktop
echo " Done."
# --- Step 5: legacy XFCE / TigerVNC residue ---
# Only relevant on a host set up by the pre-mirror version of setup-desktop.sh. Harmless elsewhere.
echo "[5/7] Removing legacy XFCE / TigerVNC residue..."
# Anchored and spelled out on purpose. The tempting shortening to '^libxf' also matches libxfixes,
# libxft and libxfont — core X11 libraries GNOME needs — so each xfce-family prefix is named instead.
XFCE_PKGS=$(dpkg-query -W -f='${Package}\n' 2>/dev/null | grep -E '^(xfce|xfdesktop|xfwm|xfconf|thunar|libxfce|libxfconf|libgarcon|libexo|exo-utils|tumbler|elementary-xfce)' || true)
if [ -n "$XFCE_PKGS" ]; then
# shellcheck disable=SC2086
sudo DEBIAN_FRONTEND=noninteractive apt-get remove -y --purge $XFCE_PKGS >/dev/null 2>&1 || true
echo " Purged $(echo "$XFCE_PKGS" | wc -l) XFCE package(s)."
else
echo " No XFCE packages."
fi
sudo DEBIAN_FRONTEND=noninteractive apt-get remove -y --purge \
tigervnc-standalone-server tigervnc-common tigervnc-tools >/dev/null 2>&1 || true
sudo DEBIAN_FRONTEND=noninteractive apt-get autoremove -y --purge >/dev/null 2>&1 || true
rm -rf ~/.config/xfce4 ~/.cache/xfce4 ~/.local/share/xfce4 ~/.config/Thunar ~/.cache/sessions ~/.dmrc.bak-xfce
# The pre-mirror setup ran x11vnc/Xvnc from a systemd unit rather than the sidecar.
if systemctl list-unit-files officer-vnc.service &>/dev/null; then
sudo systemctl stop officer-vnc 2>/dev/null || true
sudo systemctl disable officer-vnc 2>/dev/null || true
sudo rm -f /etc/systemd/system/officer-vnc.service
sudo systemctl daemon-reload
echo " Removed the legacy officer-vnc systemd unit."
fi
# --- Step 6: Brave (opt-in) ---
echo "[6/7] Browser..."
if [ "$PURGE_BRAVE" = true ]; then
sudo DEBIAN_FRONTEND=noninteractive apt-get remove -y --purge brave-browser >/dev/null 2>&1 || true
sudo rm -f /usr/share/keyrings/brave-browser-archive-keyring.gpg \
/etc/apt/sources.list.d/brave-browser-release.list \
/usr/bin/brave-browser-stable
sudo rm -rf /etc/brave
sudo rm -f /usr/bin/brave-browser-stable
rm -rf ~/.config/BraveSoftware
echo " Brave removed, including its profile."
else
echo " Brave kept (pass --purge-brave to remove it and its profile)."
fi
# Remove Chromium (snap + deb)
echo "[3/5] Removing Chromium..."
sudo snap remove chromium 2>/dev/null || true
sudo apt remove -y --purge chromium-browser 2>/dev/null || true
rm -rf ~/.config/chromium
# Remove XFCE, TigerVNC, dbus-x11
echo "[4/5] Removing XFCE, TigerVNC, dbus-x11..."
sudo apt remove -y --purge xfce4 xfce4-goodies tigervnc-standalone-server tigervnc-common dbus-x11 2>/dev/null || true
sudo apt autoremove -y 2>/dev/null || true
rm -rf ~/.config/xfce4
rm -rf ~/.cache/xfce4
# Remove keyring data
echo "[5/5] Removing keyring data..."
rm -rf ~/.local/share/keyrings
rm -f ~/.config/autostart/gnome-keyring-*.desktop
sudo apt install -y -qq gnome-keyring > /dev/null 2>&1 || true # Restore if needed by other apps
# Remove env vars
# --- Step 7: .env ---
echo "[7/7] Cleaning .env..."
ENV_FILE="$(cd "$(dirname "$0")/.." && pwd)/.env"
if [ -f "$ENV_FILE" ]; then
sed -i '/^VNC_PASSWORD=/d' "$ENV_FILE"
sed -i '/^VNC_PORT=/d' "$ENV_FILE"
echo "Removed VNC vars from .env"
sed -i '/^VNC_PASSWORD=/d; /^VNC_PORT=/d' "$ENV_FILE"
echo " Removed VNC entries."
else
echo " No .env — skipping."
fi
echo ""
echo "Done. All desktop packages and config removed."
echo "The app code (routes, components) is still in place."
echo "Teardown complete. ubuntu-desktop, gdm3 and dbus-x11 were left in place — they are this"
echo "machine's desktop, not Officer's. The app code is untouched: the VNC sidecar keeps running under"
echo "PM2, but with x11vnc gone it can no longer start a mirror, so opening /desktop will fail to"
echo "connect rather than take the app down with it."
echo "REBOOT to drop auto-login and the forced EDID."