/
slingring
executable file
·136 lines (117 loc) · 3.66 KB
/
slingring
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/bash
SEU_NAME=$1
if [ "$SEU_NAME" = "" ]; then
echo "Usage: slingring UNIVERSE [COMMAND]"
exit 1
fi
SCHROOT_NAME=${SEU_NAME}
SESSION_NAME=${SEU_NAME}-seu-session
COMMAND_ARGUMENT=$2
if [ "$COMMAND_ARGUMENT" = "" ]; then
COMMAND_ARGUMENT="/bin/bash -l"
fi
check_session_exists()
{
SESSION_ID=session:$1
SESSION_MATCH=$(schroot --all-sessions -l|grep ^${SESSION_ID}$)
if [ "$SESSION_MATCH" = "$SESSION_ID" ] ; then
return 0
else
return 1
fi
}
check_processes_stopped()
{
schroot-process-check -q "$1"
RESULT=$?
if [ "$RESULT" = "0" ] ; then
return 0
elif [ "$RESULT" = "3" ] ; then
return 1
else
echo "The schroot session could not be checked for running processes."
echo "This could leave your universe in an inconsistent state."
echo
echo "Please run schroot-process-check "$1" manually to find out more about the error."
exit 1
fi
}
check_session_running()
{
RUNNING_SCHROOT_MOUNT=$(ps aux|grep "\ schroot -r -c $1\ ")
if [ "$RUNNING_SCHROOT_MOUNT" = "" ] ; then
return 1
else
return 0
fi
}
check_session_valid()
{
ROOT_MOUNT_MATCH=$(mount|grep "$1"\ )
PROC_MOUNT_MATCH=$(mount|grep "$1"\/proc)
SYS_MOUNT_MATCH=$(mount|grep "$1"\/sys)
DEV_MOUNT_MATCH=$(mount|grep "$1"\/dev)
DEVPTS_MOUNT_MATCH=$(mount|grep "$1"\/dev\/pts)
X11_MOUNT_MATCH=$(mount|grep "$1"\/tmp\/.X11-unix)
if [ "$ROOT_MOUNT_MATCH" = "" ] ||
[ "$PROC_MOUNT_MATCH" = "" ] ||
[ "$SYS_MOUNT_MATCH" = "" ] ||
[ "$DEV_MOUNT_MATCH" = "" ] ||
[ "$DEVPTS_MOUNT_MATCH" = "" ] ||
[ "$X11_MOUNT_MATCH" = "" ] ; then
return 1
else
return 0
fi
}
if ! check_session_exists "$SESSION_NAME"; then
echo Session does not exist. Creating...
# get X11 authority into the chroot
COOKIE_NAME=$(xauth list ${DISPLAY}|head -1 |sed "s/[\:\ ]*MIT-MAGIC-COOKIE-[\ a-f0-9]*/${DISPLAY}/")
if ! [ -z "${COOKIE_NAME}" ]; then
CHROOT_DIR=$(LANG="C" schroot -i -c ${SCHROOT_NAME}|grep Directory|sed s/[\ ]Directory[\ ]//)
xauth extract ${CHROOT_DIR}${HOME}/.Xauthority ${COOKIE_NAME}
fi
# create session
schroot -b -c "$SCHROOT_NAME" -n "$SESSION_NAME" > /dev/null
fi
while ! check_session_valid "$SESSION_NAME"; do
if ! check_processes_stopped "$SESSION_NAME"; then
echo "The universe seems to be in an invalid state, but there are still processes"\
"running inside it. Please make sure that all services are stopped and all"\
"portals are closed."
echo
read -p "Press any key to retry or CTRL+C to abort."
else
schroot --recover-session -c "$SESSION_NAME"
if ! check_session_valid "$SESSION_NAME"; then
echo "The universe seems to be in an invalid state. That usually means that there"\
"is a schroot session but some expected mounts are missing. Unfortunately,"\
"slingring could not automatically recover the session."
echo
read -p "Press any key to retry or CTRL+C to abort."
fi
fi
done
EXPORT_STRING=""
if ! [ -z ${DISPLAY} ] ; then
EXPORT_STRING="$EXPORT_STRING export DISPLAY=\"$DISPLAY\";"
fi
if ! [ -z ${SSH_AUTH_SOCK} ] ; then
EXPORT_STRING="$EXPORT_STRING export SSH_AUTH_SOCK=\"$SSH_AUTH_SOCK\";"
fi
if ! [ -z ${DBUS_SESSION_BUS_ADDRESS} ] ; then
EXPORT_STRING="$EXPORT_STRING export DBUS_SESSION_BUS_ADDRESS=\"$DBUS_SESSION_BUS_ADDRESS\";"
fi
schroot -r -c "$SESSION_NAME" --directory="$HOME" -- /bin/bash -c "$EXPORT_STRING $COMMAND_ARGUMENT"
if check_session_running "$SESSION_NAME"; then
exit 0
fi
if ! check_processes_stopped "$SESSION_NAME"; then
echo
echo "INFO: You closed the last portal to this universe but there are still processes"\
"running inside it."
echo "Please make sure to stop them before shutting down your computer to avoid umount locks."
exit 0
fi
schroot -e -c "$SESSION_NAME"