-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdchroot
executable file
·207 lines (176 loc) · 5.16 KB
/
dchroot
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
#!/bin/bash
#
# Chroot script
#
#
# You can make a directory tree in ${HOME}/.dchroot/rc.d/ containing files named for chroots. These
# will be sourced, allowing you to make local, persistant modifications. The most useful
# things to do here are:
#
# Set USER_MOUNT_DIRS to an array of <source,dest> pairs of directories to mount
# declare -A USER_MOUNT_DIRS=(["/opt/home/dang"]="/home/dang" ["/share"]="/share")
# Set BASEDIR to some other base directory
# Set NOPORTAGE, NOSCREEN, AUTOSTOP, DETACHED, or OVERLAYS to avoid having to
# use command line options
# Set USER_PRE_COMMAND to some command to run before chroot, say "linux32"
BASEDIR="/home/chroots"
# Set usage output
USAGE="[-h |--help] [-a | --autostop] [-d | --detached] [-o | --overlays] [-p | --noportage] [-s | --noscreen ] [-b <basedir> | --basedir=<basedir>] <chrootname> start | stop | join | run"
LONGUSAGE="\t-h, --help\n\t\tPrint this help message
\t-a, --autostop\n\t\tAutomatically stop when screen exits
\t-d, --detached\n\t\tStart screen detached (ignored for noscreen or autostop)
\t-o, --overlays\n\t\tMount overlays too
\t-p, --noportage\n\t\tDon't mount portage directories
\t-s, --noscreen\n\t\tDon't start screen
\t-b <basedir>, --basedir=<basedir>\n\t\tSet the base directory for chroots
\t<chrootname>\n\t\tName of the chroot
\tstart | stop | join | run\n\t\tStart, stop, or join the chroot, or run a command in it"
# Standard functions
source ${SCRIPTS}/functions.sh
# Script name
ME=$(basename $0)
# Parse arguments
ARGS=`getopt -o hadopsb: --long help,autostop,detached,overlays,noportage,noscreen,basedir: -n "${ME}" -- "$@"`
if [ $? != 0 ] ; then
usage
fi
eval set -- "$ARGS"
while true ; do
case "$1" in
-h|--help) usage; shift ;;
-a|--autostop) AUTOSTOP="yes"; shift ;;
-d|--detached) DETACHED="yes"; shift ;;
-o|--overlays) OVERLAYS="yes"; shift ;;
-p|--noportage) NOPORTAGE="yes"; shift ;;
-s|--noscreen) NOSCREEN="yes"; shift ;;
-b|--basedir) BASEDIR=$2 ; shift 2 ;;
--) shift ; break ;;
* ) usage "Invalid argument $1";;
esac
done
if [ -z "${1}" ]; then
usage "Must give chrootname"
fi
CHROOTNAME=${1}
if [ -z "${2}" ]; then
usage "Must give start, stop, or join"
fi
CMD=${2}
# We want parameters for run
shift 2
SYS_MOUNT_DIRS="/proc /dev /sys /dev/pts"
if [ -f ${HOME}/.dchroot/rc.d/${CHROOTNAME} ]; then
source ${HOME}/.dchroot/rc.d/${CHROOTNAME}
fi
if [ -z "${CHROOTDIR}" ]; then
CHROOTDIR="${BASEDIR}/${CHROOTNAME}"
fi
if [ ! -d ${CHROOTDIR} ]; then
usage "${CHROOTDIR} does't exist"
fi
if [ -z "${NOPORTAGE}" ]; then
portage_setup ${CHROOTDIR}
# Re-source after portage stuff, so that it can override
if [ -f ${HOME}/.dchroot.rc.d/${CHROOTNAME} ]; then
source ${HOME}/.dchroot.rc.d/${CHROOTNAME}
fi
SYS_MOUNT_DIRS="${SYS_MOUNT_DIRS} ${PORTDIR}"
SYS_MOUNT_DIRS="${SYS_MOUNT_DIRS} ${PORTAGE_TMPDIR}"
SYS_MOUNT_DIRS="${SYS_MOUNT_DIRS} ${DISTDIR}"
if [ -n "${OVERLAYS}" ]; then
for dir in ${PORTDIR_OVERLAY}; do
SYS_MOUNT_DIRS="${SYS_MOUNT_DIRS} ${dir}"
done
fi
fi
if [ -z "${NOSCREEN}" ]; then
SCREEN="screen -S ${CHROOTNAME} -t ${CHROOTNAME}"
if [ -z "${AUTOSTOP}" ]; then
if [ -n "${DETACHED}" ]; then
SCREEN="${SCREEN} -d -m"
fi
fi
fi
function start()
{
# Mount system directories
for dir in ${SYS_MOUNT_DIRS}; do
sudo mount -o bind ${dir} ${CHROOTDIR}/${dir} || die "Mount of ${dir} failed"
done
# Mount user directories
for dir in ${!USER_MOUNT_DIRS[@]}; do
destdir=${CHROOTDIR}/${USER_MOUNT_DIRS[$dir]}
if [[ "${dir}" == "0" ]]; then
dir=${USER_MOUNT_DIRS[$dir]}
fi
sudo mount -o bind ${dir} ${destdir} || die "Mount of ${dir} failed"
done
sudo cp /etc/resolv.conf ${CHROOTDIR}/etc || die "Couldn't cp /etc/resolv.conf"
# Now, start screen and chroot
cd ${BUILDDIR}
${SCREEN} ${USER_PRE_COMMAND} sudo -H chroot ${CHROOTDIR} /bin/bash -l
if [ -n "${AUTOSTOP}" ]; then
stop
fi
}
function stop()
{
SCREENUP=$(screen -ls | grep -e "\.${CHROOTNAME}[[:space:]]" | awk '{print $1}')
if [ -n "${SCREENUP}" ]; then
screen -S ${SCREENUP} -X quit
fi
# Unmount system directories
for dir in ${SYS_MOUNT_DIRS}; do
UMOUNT_DIRS="${UMOUNT_DIRS} ${CHROOTDIR}/${dir}"
done
# Mount user directories
for dir in ${USER_MOUNT_DIRS}; do
destdir=${CHROOTDIR}/${USER_MOUNT_DIRS[$dir]}
UMOUNT_DIRS="${UMOUNT_DIRS} ${destdir}"
done
sudo umount ${UMOUNT_DIRS} > /dev/null 2>&1
# catchall
UMOUNT_DIRS=""
DIRS=$(grep ${CHROOTDIR} /proc/mounts | awk '{print $2}' | sort -r)
for i in ${DIRS}; do
if [ "${i}" != "${CHROOTDIR}" ]; then
UMOUNT_DIRS="${UMOUNT_DIRS} ${i}"
fi
done
sudo umount ${UMOUNT_DIRS}
}
function join()
{
if [ -z "$(grep ${CHROOTDIR} /proc/mounts)" ]; then
die "${ME} ${BASENAME} is not running; use \"start\" rather than \"join\""
fi
# Now, start screen and chroot
cd ${BUILDDIR}
${SCREEN} sudo -H chroot ${CHROOTDIR} /bin/bash -l
}
function run()
{
if [ -z "$(grep ${CHROOTDIR} /proc/mounts)" ]; then
die "${ME} ${BASENAME} is not running; \"start\" must be run before \"run\""
fi
# Now, run the command in the chroot
cd ${BUILDDIR}
sudo -H chroot ${CHROOTDIR} /bin/bash -l -c "$*"
}
case "${CMD}" in
start)
start
;;
stop)
stop
;;
join)
join
;;
run)
run $@
;;
*)
usage "unknown command ${CMD}"
;;
esac