-
Notifications
You must be signed in to change notification settings - Fork 0
/
schroot-shell
executable file
·396 lines (358 loc) · 10.9 KB
/
schroot-shell
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/bin/sh -e
# vim:se sw=2 ts=2 sts=2 et ai tw=78:
# A convenience script to use schroot desktop profile
# You can run 'gitk' with this.
_help() {
cat << EOF
NAME
${0##*/} - start a shell in chroot with current home data.
SYNOPSIS
${0##*/} [DIST [CONFIG]]
${0##*/} --config [CONFIG]
${0##*/} [--init|--prep|--upgrade] [DIST [CONFIG]]
DESCRIPTION
This wrapper command of *schroot* offers a set of helper commands to work in
the persistent pre-loaded (source) chroot system environment while sharing the
user data from the user's home directory of the host system.
Here, if 'CONFIG' and 'DIST' are missing in the command line, 'desktop' for
'CONFIG' and 'unstable' for 'DIST' are used as the default values. Normally,
you skip specifying 'CONFIG' to use its default value. This command
automatically becomes root when needed.
The *${0##*/}* command is normally invoked as:
${0##*/} [DIST [CONFIG]]
This starts an interactive shell in the 'DIST-${ARCH}-CONFIG' source chroot
managed by the *schroot* command while bind mounting the user data from the
user's home directory of the host system. This is somewhat modeled after
*sbuild-shell*. While *sbuild-shell* is designed to work with the session
chroot and the changes made from *sbuild-shell* to the chroot is discarded,
the changes made from *${0##*/}* to the chroot is persistent.
In order for the *${0##*/}* command to function, you need to set up the
'DIST-${ARCH}-CONFIG' source chroot properly.
The 'DIST-${ARCH}-CONFIG' source chroot can be initialized in 3
steps.
1. Create the setup script configuration files for *schroot* under
'/etc/schroot/CONFIG/' as explained in the 'FILES' section under 'Setup
script configuration' in schroot.conf(5) for '/etc/schroot/default'.
${0##*/} --config [CONFIG]
2. Customize the setup script configuration files for *schroot* at
'/etc/schroot/CONFIG/fstab' to list all mounted devices in your home
directory.
3. Create a chroot file system for 'DIST' at
'/srv/chroot/DIST-${ARCH}-CONFIG' using the setup script configuration
files in '/etc/schroot/CONFIG'.
${0##*/} --init [DIST [CONFIG]]
Here, 'DIST' specifies base distribution of the chroot system. 'DIST'
can be 'unstable' (default), 'testing', 'stable', 'oldstable', or
'oldoldstable'. These can be shortened as 'u', 't', 's', 'o', or 'oo'
in the command line.
The 'DIST-${ARCH}-CONFIG' source chroot can be updated to the latest
state by:
${0##*/} --update [DIST [CONFIG]]
The 'DIST-${ARCH}-CONFIG' source chroot can be prepped up with a few
pre-defined extra packages desirable for the shell activities by:
${0##*/} --prep [DIST [CONFIG]]
For 'CONFIG', values 'default', 'minimal', 'buildd', and 'sbuild'
should be avoided unless you know what exactly happens since these setup
scripts are already used by *sbuild* and *schroot*.
The *${0##*/}* command offers a generarized and similar functionality of
combined *sbuild-createchroot*, *sbuild-update*, and *sbuild-shell* for the
persistent chroot shell environment instead of the session one.
Parametrs:
TYPE = '$TYPE'
DIST = '$DIST'
ARCH = '$ARCH'
CONFIG = '$CONFIG'
EOF
}
_noxhost() {
if [ "$XHOST" = "xhost" ]; then
$XHOST -
echo "I: closed xhost just to be sure"
fi
}
_distsh() {
$XHOST -
echo
echo "I: execute in ${TYPE}:${DIST}-${ARCH}-${CONFIG}"
echo " % $*"
$XHOST "+si:localuser:$USERNAME"
echo "I: allow xhost only for the localuser"
# COMMAND=$SHELL -i
schroot -c "${TYPE}:${DIST}-${ARCH}-${CONFIG}" -- "$@"
echo "I: normal exit from schroot"
}
_su() {
if [ "$(id -u)" != 0 ]; then
echo "I: execute as root: $*"
exec sudo "$@"
fi
}
_config() {
echo "I: Setting up $CONFIG"
if [ -d "/etc/schroot/$CONFIG" ]; then
echo "E: /etc/schroot/$CONFIG already exists."
exit 1
fi
mkdir -p "/etc/schroot/$CONFIG"
cat > "/etc/schroot/$CONFIG/copyfiles" << EOF
# Files to copy into the chroot from the host system.
#
# <source and destination>
/etc/resolv.conf
EOF
cat > "/etc/schroot/$CONFIG/fstab" << EOF
# fstab: static file system information for chroots.
# Note that the mount point will be prefixed by the chroot path
# (CHROOT_PATH)
#
# <file system> <mount point> <type> <options> <dump> <pass>
# I/O device
/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
# user data
/root /root none rw,bind 0 0
/home /home none rw,bind 0 0
$USERHOME $USERHOME none rw,bind 0 0
# Adjust for installation
#$USERHOME/Downloads $USERHOME/Downloads none rw,bind 0 0
#$USERHOME/github $USERHOME/github none rw,bind 0 0
#$USERHOME/salsa $USERHOME/salsa none rw,bind 0 0
#$USERHOME/tmp $USERHOME/tmp none rw,bind 0 0
# Runtime user data
# ?
/tmp /tmp none rw,bind 0 0
# If you use gdm3, uncomment this line to allow Xauth to work
/var/run/gdm3 /var/run/gdm3 none rw,bind 0 0
# For PulseAudio and other desktop-related things
/var/lib/dbus /var/lib/dbus none rw,bind 0 0
# For wayland on systemd ... maybe XXX FIXME XXX needs testing
/run/user/1000 /run/user/1000 none rw,bind 0 0
# It may be desirable to have access to /run, especially if you wish
# to run additional services in the chroot. However, note that this
# may potentially cause undesirable behaviour on upgrades, such as
# killing services on the host.
#/run /run none rw,bind 0 0
#/run/lock /run/lock none rw,bind 0 0
#/dev/shm /dev/shm none rw,bind 0 0
#/run/shm /run/shm none rw,bind 0 0
EOF
cat > "/etc/schroot/$CONFIG/nssdatabases" << EOF
# System databases to copy into the chroot from the host system.
#
# <database name>
passwd
shadow
group
gshadow
services
protocols
networks
hosts
EOF
}
_init_sbuild() {
# create a minimum schroot for sbuild
echo "I: setting up DIST=${DIST} for sbuild chroot."
SBUILD_CHROOT="${BASECHROOT}/${DIST}-${ARCH}-sbuild"
if [ -e "${SBUILD_CHROOT}" ]; then
echo "I: ${SBUILD_CHROOT} exists, so use it as the starting point."
else
DEBURL="http://deb.debian.org/debian"
sbuild-createchroot --include=eatmydata,ccache "${DIST}" "${SBUILD_CHROOT}" "${DEBURL}"
fi
}
_init() {
echo "I: _init ${DIST}-${ARCH}-${CONFIG}"
_init_sbuild
# create additional chroot for use by terminal and X (Yes: xwayland, No: pure wayland)
# $ xhost +si:localuser:<username> ; schroot -c chroot:${DESKTOP_CHROOT}-?? ; xhost -
echo "I: setting up DIST=${DIST} for ${CONFIG} ..."
DESKTOP_CHROOT="${BASECHROOT}/${DIST}-${ARCH}-${CONFIG}"
if [ ! -e "${DESKTOP_CHROOT}" ]; then
cp -a --reflink=always "${SBUILD_CHROOT}" "${DESKTOP_CHROOT}"
else
echo "I: chroot ${DESKTOP_CHROOT} already exists, so use it as is."
exit 0
fi
# create schroot data entry /etc/schroot/chroot.d/${DIST}-${ARCH}-${CONFIG}
# while using schroot $CONFIG with uniq filename
# shellcheck disable=SC2086
CONF_SCHROOT="$(ls -1 /etc/schroot/chroot.d/${DIST}-${ARCH}-${CONFIG}-* 2> /dev/null || true)"
if [ -z "$CONF_SCHROOT" ]; then
if [ "${DIST}" = unstable ]; then
ALIASES_LINE="aliases=sid-${ARCH}-${CONFIG},sid-${CONFIG},${CONFIG}"
else
ALIASES_LINE="#aliases=<please add your alias here and uncomment>"
fi
CONF_SCHROOT="/etc/schroot/chroot.d/${DIST}-${ARCH}-${CONFIG}-$$"
cat > "$CONF_SCHROOT" << EOF
[${DIST}-${ARCH}-${CONFIG}]
description=Debian ${DIST} ${ARCH} chroot with shared desktop data
groups=root,sbuild
root-groups=root,sbuild
source-groups=root,sbuild
source-root-groups=root,sbuild
profile=${CONFIG}
type=directory
preserve-environment=true
directory=/srv/chroot/${DIST}-${ARCH}-${CONFIG}
union-type=overlay
command-prefix=eatmydata
${ALIASES_LINE}
EOF
echo "I: ${CONF_SCHROOT} created"
else
echo "I: ${CONF_SCHROOT} already exists so use it"
fi
}
_upgrade() {
echo "I: _upgrade ${DIST}-${ARCH}-${CONFIG}"
# -udcar
_distsh apt-get update --yes
_distsh apt-get dist-upgrade --yes
_distsh apt-get clean --yes
_distsh apt-get autoclean --yes
_distsh apt-get autoremove --yes
}
_aptget() {
_distsh apt-get install --yes "$@"
}
_prep() {
echo "I: _prep ${DIST}-${ARCH}-${CONFIG}"
# Install piece-by-piece to help understand the unstable breakage situation
_aptget sudo x11-xserver-utils
_aptget locales-all
_aptget aptitude mc vim nano-
_aptget git gitk
_aptget imediff fzf rlwrap
_aptget eatmydata ccache
# netbase conflict with /etc/services
_aptget netbase
_aptget devscripts
# x11-xserver-util package is needed by xhost command
# xhost command is needed by gitk operation
}
#############################################################################
THIS_COMMAND="$0"
echo
echo "I: executing as USER='$(id -u -n)'"
echo " % $THIS_COMMAND $*"
if [ "$(id -u)" != 0 ] && type xhost > /dev/null; then
XHOST="xhost"
else
XHOST=":"
fi
# For ease of customization, here are key parameters
USERNAME="$(id -u -n)"
USERHOME="/home/$USERNAME"
BASECHROOT="/srv/chroot" # enough disk space here
TYPE="source" # chroot namespace
ARCH="amd64" # adjust for system
COMMAND_MODE="shell"
case "$1" in
-c | --co*)
COMMAND_MODE="config"
shift
;;
-i | --in*)
COMMAND_MODE="init"
shift
;;
-p | --pr*)
COMMAND_MODE="prep"
shift
;;
-u | --up*)
COMMAND_MODE="upgrade"
shift
;;
-h | --help)
COMMAND_MODE="help"
shift
;;
-*)
echo "W: unknown '${1}'"
echo
_help
echo
echo "W: unknown '${1}'"
exit 1
;;
esac
case "$1" in
'')
DIST="unstable"
;;
u)
DIST="unstable"
shift
;;
t)
DIST="testing"
shift
;;
s)
DIST="stable"
shift
;;
o)
DIST="oldstable"
shift
;;
oo)
DIST="oldoldstable"
shift
;;
unstable | testing | stable | oldstable | oldoldstable)
DIST="${1}"
shift
;;
sid | bookworm | bullseye | buster | strech)
DIST="${1}"
shift
;;
*)
echo "W: unknown distribution ${1}"
echo
_help
echo
echo "W: unknown distribution ${1}"
exit 1
;;
esac
if [ -n "$1" ]; then
CONFIG="${1}"
else
CONFIG="desktop"
fi
case "$COMMAND_MODE" in
co*) # config
_su "$THIS_COMMAND" "-c" "$CONFIG"
_config
;;
in*) # init
_su "$THIS_COMMAND" "-i" "$DIST" "$CONFIG"
_init
_upgrade
_prep
;;
pr*) # prep
_su "$THIS_COMMAND" "-p" "$DIST" "$CONFIG"
_upgrade
_prep
;;
up*) # upgrade
_su "$THIS_COMMAND" "-u" "$DIST" "$CONFIG"
_upgrade
;;
sh*) # shell
trap _noxhost 0
_distsh
;;
he*) # help
_help
exit 0
;;
esac