#!/bin/bash # enter-baserock -- Tool to enter a Baserock chroot using schroot # Copyright (C) 2014 Codethink Ltd # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. SCHROOT_BASE="${SCHROOT_BASE:-/etc/schroot}" SCHROOT_CONF="${SCHROOT_CONF:-${SCHROOT_BASE}/schroot.conf}" BASEROCK_BASE="${BASEROCK_BASE:-/opt/baserock/chroots}" baserock_default () { (cd "${BASEROCK_BASE}" && ls -d */default) 2>/dev/null | sed -e's@/default$@@' | head -1 } CHOSEN="$1" if test "x$CHOSEN" = "x"; then CHOSEN=$(baserock_default) fi if test "x$CHOSEN" = "x"; then echo "No default found, cannot enter baserock without a name" exit 1 fi cd / if ! grep -q btrfs /proc/filesystems; then echo "No btrfs support detected in kernel, attempting to load btrfs module" if ! sudo -p "Password (to run 'modprobe -v btrfs'): " modprobe -v btrfs; then echo "Loading module failed, looks like your kernel doesn't support Btrfs." echo "Deploying systems to Btrfs disk images will not work correctly." fi fi if test "x$SSH_AUTH_SOCK" != "x"; then echo "Forwarding SSH agent into chroot" TDIR=$(mktemp --tmpdir=/tmp -d) SOCK="${TDIR}/auth-sock" socat "UNIX-LISTEN:${SOCK},fork" "UNIX-CONNECT:${SSH_AUTH_SOCK}" >/dev/null 2>/dev/null & SOCAT_PID=$! cleanup () { echo "Clearing up SSH agent forwarding" kill $SOCAT_PID >/dev/null 2>/dev/null rm -rf "${TDIR}" } trap cleanup 0 sudo -H schroot -c "baserock-$CHOSEN" -- /usr/bin/env "SSH_AUTH_SOCK=$SOCK" /bin/bash -l else exec sudo -H schroot -c "baserock-$CHOSEN" fi