From 84797ecf96f96c8659fbfc16f88577ab0b4125ca Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Fri, 28 Feb 2014 09:31:50 +0000 Subject: Add chroot sync tool --- br-ct-sync-chroots | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100755 br-ct-sync-chroots diff --git a/br-ct-sync-chroots b/br-ct-sync-chroots new file mode 100755 index 0000000..b6b38fc --- /dev/null +++ b/br-ct-sync-chroots @@ -0,0 +1,99 @@ +#!/bin/bash + +ACTION="$1" + +SCHROOT_BASE="${SCHROOT_BASE:-/etc/schroot}" +SCHROOT_CONF="${SCHROOT_CONF:-${SCHROOT_BASE}/schroot.conf}" +BASEROCK_BASE="${BASEROCK_BASE:-/opt/baserock/chroots}" + +clear_entries () { + sed -e'/BASEROCK_ENTRIES_BEGIN/,/BASEROCK_ENTRIES_END/d' -i "${SCHROOT_CONF}" +} + +find_default () { + grep "^aliases" "$SCHROOT_CONF" | grep -q "default" +} + +add_conf_line () { + echo "$@" >> "${SCHROOT_CONF}" +} + +list_baserocks () { + (cd "${BASEROCK_BASE}" && ls -d */baserock) 2>/dev/null | sed -e's@/baserock$@@' +} + +add_entries () { + add_conf_line "# BASEROCK_ENTRIES_BEGIN" + while read entry; do + add_conf_line "[baserock-$entry]" + add_conf_line "type=directory" + add_conf_line "directory=${BASEROCK_BASE}/${entry}" + add_conf_line "description=Baserock in $entry" + add_conf_line "users=root" + add_conf_line "script-config=baserock-$entry/config" + if ! find_default; then + test -e "${BASEROCK_BASE}/$entry/default" && add_conf_line "aliases=default" + fi + done < <(list_baserocks) + add_conf_line "# BASEROCK_ENTRIES_END" +} + +find_configs () { + (cd "${SCHROOT_BASE}"; ls -d baserock-*/config) 2>/dev/null | sed -e's@/config$@@' +} + +find_used_configs () { + grep "^script-config" "${SCHROOT_CONF}" | grep baserock | sed -e's@script-config *= *@@' +} + +is_config_used () { + grep "script-config *= *" "${SCHROOT_CONF}" | grep -q -F "$1/config" +} + +add_new_configs () { + for entry in $(list_baserocks); do + if ! test -e "${SCHROOT_BASE}/baserock-${entry}/config"; then + mkdir -p "${SCHROOT_BASE}/baserock-${entry}" + cat > "${SCHROOT_BASE}/baserock-${entry}/config" < "${SCHROOT_BASE}/baserock-${entry}/fstab" <<'EOF' +/proc /proc none rw,bind 0 0 +/sys /sys none rw,bind 0 0 +/dev /dev none rw,bind 0 0 +/dev/pts /dev/pts none rw,bind 0 0 +/tmp /tmp none rw,bind 0 0 +EOF + cat > "${SCHROOT_BASE}/baserock-${entry}/copyfiles" <<'EOF' +/etc/resolv.conf +EOF + cat > "${SCHROOT_BASE}/baserock-${entry}/nssdatabase" <<'EOF' +networks +hosts +EOF + fi + done +} + +clear_unused_configs () { + while read config; do + if ! is_config_used "$config"; then + rm -rf "${SCHROOT_BASE}/${config}" + fi + done < <(find_configs) +} + +case "$ACTION" in + purge) + clear_entries + clear_unused_configs + ;; + *) + clear_entries + add_entries + add_new_configs + clear_unused_configs + ;; +esac -- cgit v1.2.1