Skip to content

Instantly share code, notes, and snippets.

@Frederick888
Created January 28, 2020 04:02
Show Gist options
  • Save Frederick888/caa65717541dab3bb7f827d9f281f1ca to your computer and use it in GitHub Desktop.
Save Frederick888/caa65717541dab3bb7f827d9f281f1ca to your computer and use it in GitHub Desktop.
Offloading XDG cache home to tmpfs
  1. Configure /tmp as per fstab, adjust size if needed
  2. Copy xdg-cache.service to /etc/systemd/user/xdg-cache.service
  3. Copy xdg-cache.sh to /usr/local/bin/xdg-cache.sh and then chmod +x /usr/local/bin/xdg-cache.sh
  4. Run systemctl --user enable xdg-cache.service as the user that needs this service
tmpfs /tmp tmpfs defaults,noatime,nosuid,nodev,mode=1777,size=24G 0 0
[Unit]
Description=Synchronises XDG cache between disk and tmpfs for %u
Before=default.target
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/usr/local/bin/xdg-cache.sh ram
ExecStop=/usr/local/bin/xdg-cache.sh
StandardOutput=journal
StandardError=journal
TimeoutStopSec=1m
[Install]
WantedBy=default.target
#!/usr/bin/env bash
PERSIST_DIR="$HOME/.cache"
TMP_DIR="/tmp/$USER/.cache"
if [[ "$1" == "ram" ]]; then
# to ram
if [ -d "$PERSIST_DIR" ]; then
#TIME='%C %Us user %Ss system %es total' time rsync -aq "$PERSIST_DIR/" "$TMP_DIR"
mkdir -p "$TMP_DIR" "$TMP_DIR-upper"
TIME='%C %Us user %Ss system %es total' time unionfs \
-o rw,async,exec,suid,dev,nonempty,default_permissions,allow_other \
-o use_ino,cow,statfs_omit_ro,hide_meta_files,max_files=65536 \
"$TMP_DIR-upper=RW:$PERSIST_DIR=RO" "$TMP_DIR"
fi
elif [ -d "$TMP_DIR" ]; then
# to disk
TIME='%C %Us user %Ss system %es total' time rsync -ai --delete --delete-excluded --ignore-missing-args \
--exclude='/google-chrome' \
--exclude='/yay/*/' \
--exclude='/pip' \
--exclude='/kioexec' \
--exclude='/wine' \
--exclude='/go-build' \
--exclude='/rclone' \
--exclude='/spotify' \
--exclude='/.nv' \
"$TMP_DIR/" "$PERSIST_DIR"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment