forked from rwbaumg/admin-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
do-chroot.sh
executable file
·52 lines (43 loc) · 1.25 KB
/
do-chroot.sh
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
#!/bin/bash
# runs a command in the specified schroot
# rwb [ rwb[at]0x19e.net ]
if [[ -z "$1" ]]; then
echo "Usage: $0 <schroot> [COMMAND]"
exit 1
fi
# The full path to the schroot binary
SCHROOT_BIN="/usr/bin/schroot"
# The name of the schroot to jail the daemon in
SCHROOT_NAME="$1"
# Get the command to run
CMD_ARGS="${*:2}"
echo -e "schroot \t : $SCHROOT_NAME"
echo -e "command \t : $CMD_ARGS"
# check to make sure the specified schroot actually exists
CHROOT_EXISTS=""
for ch in $($SCHROOT_BIN -l -a|grep "chroot"|awk -F: '{print $2}'); do
if [[ $ch == "$SCHROOT_NAME"* ]]; then
CHROOT_EXISTS="y"
fi
done
if [[ -z "$CHROOT_EXISTS" ]]; then
echo "ERROR: The specified chroot, '$SCHROOT_NAME', does not exist."
exit 1
fi
# Get the schroot session (or create a new one)
SESSION=""
for sn in $($SCHROOT_BIN -l -a|grep "session:"|awk -F: '{print $2}'); do
if [[ $sn == "$SCHROOT_NAME"* ]]; then
SESSION=$sn
fi
done
if [[ -z "$SESSION" ]]; then
echo "Creating new schroot session for '$SCHROOT_NAME' ..."
SESSION=$(schroot --begin-session -c "$SCHROOT_NAME")
fi
echo "Using schroot session '$SESSION' ..."
echo "Running command '$CMD_ARGS' ..."
if ! $SCHROOT_BIN --directory / --run-session -c "$SESSION" "$CMD_ARGS"; then
exit 1
fi
exit 0