summaryrefslogtreecommitdiff
path: root/enter-baserock
blob: 8f1cc78d9015c5f9f13cf802e26d28426e49a1c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/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