Skip to content

Commit

Permalink
Add fdocker CLI to help manage Docker daemon (#106)
Browse files Browse the repository at this point in the history
Co-authored-by: Steven Briscoe <me@stevenbriscoe.com>
Co-authored-by: Luke Childs <lukechilds123@gmail.com>
  • Loading branch information
3 people authored Sep 28, 2022
1 parent a741734 commit 178ee29
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
98 changes: 98 additions & 0 deletions scripts/umbrel-os/fdocker
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/usr/bin/env bash
set -euo pipefail

DOCKER_ROOT="/var/lib/docker"

if [[ "$EUID" -ne 0 ]]; then
echo "Please run as root"
exit
fi

show_help() {
cat << EOF
fdocker 0.0.1
CLI for managing Docker
Usage: docker <command> <arguments...>
Commands:
clean Cleanup containers using 'containers' as argument
EOF
}

if [ -z ${2+x} ]; then
command=""
else
command="${1}"
fi

function delete_mount() {
id_path="${1}"

overlay_path="${DOCKER_ROOT}/overlay2"

if [[ -f "${id_path}" ]]; then
overlay_id=$(cat "${id_path}")

overlay_path="${overlay_path}/${overlay_id}"

if [[ -d "${overlay_path}" ]]; then
echo "Deleting: ${overlay_path}"

rm -rf "${overlay_path}"
fi

rm -rf "${id_path}"
fi
}

function cleanup_containers() {
echo "Cleaning up containers..."

containers_path="${DOCKER_ROOT}/containers"
mounts_path="${DOCKER_ROOT}/image/overlay2/layerdb/mounts"

# Loop through all the containers
containers="${containers_path}/*"
for dir in $containers; do
container_id=$(basename "${dir}")

if [[ ! -d "${dir}" ]]; then
continue
fi

echo "Removing container: ${container_id}"

# Determine the overlay IDs from the mounts
delete_mount "${mounts_path}/${container_id}/init-id"
delete_mount "${mounts_path}/${container_id}/mount-id"

# Delete the container folder
rm -rf "${dir}"

echo
done

echo "Done"
}

if [[ "${command}" == "clean" ]]; then
resource="${2}"

if [[ "${resource}" == "containers" ]]; then
cleanup_containers
else
echo "Cannot cleanup: ${resource}"

exit 1
fi

exit
fi

# If we get here it means no valid command was supplied
# Show help and exit
show_help
exit 1
19 changes: 19 additions & 0 deletions scripts/umbrel-os/services/umbrel-docker-cleanup.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Umbrel Docker Cleanup
# Installed at /etc/systemd/system/umbrel-docker-cleanup.service

[Unit]
Description=Umbrel Docker Cleanup
After=umbrel-external-storage.service
Before=docker.service

[Service]
Type=oneshot
ExecStart=/home/umbrel/umbrel/scripts/umbrel-os/fdocker clean containers
User=root
Group=root
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=umbrel docker cleanup

[Install]
WantedBy=multi-user.target

0 comments on commit 178ee29

Please sign in to comment.