-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-chroot
executable file
·280 lines (263 loc) · 7.45 KB
/
setup-chroot
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
#!/bin/bash
export INSTANCE=$$
umask 022
state_checks()
{
test $(id -u) -ne 0 && echo "please run as root" && exit 1
test ! -e setup-chroot && echo "This utility must be run in the directory in which it exists." && exit 1
test "$PWD" == "/" && echo "You have already started the session." && exit 1
test ! -d var/cache/dnf && ts/bin/install_chroot
}
help()
{
echo -e "
This script sets up the environment to work well with build. On it's initial run, it
will decompress all the packages necessary to develop and build images. It will
also populate the ts build env with any files that it needs. If the setup is already
completed, it will only take you into the chroot env for building. You will need to 'exit'
this env when you are done working in it. It takes the following options.
-a : Automaticly download and build during unwind.
-h : Show this help message.
-i : Install the env only. Don't enter it after-wards.
-b : Run build after entering chroot and exit after-wards.
-o : Options to pass to build. Must be the last option,
as anything after it will be passed to build.
-c : Clean the chroot env.
-e : Execute a command after entering chroot. Must
be the last option, as anything after it will be
executed in the chroot, including arguments.
-k : Try and import ssh keys.
"
}
get_opts()
{
until [ -z "$1" ] ; do
case $1 in
-a|--autodl) export autodl=true;;
-i|--install) install_only=true; export autodl=true;;
-b|--build) export build=true ;;
-h|--help) help ; exit 255 ;;
-o|--options) shift ; export build_opts=$@ ; shift $# ;;
-c|--clean) unset build exec_cmd; shift $# ; export clean=true ;;
-e|--exec) shift ; export exec_cmd=$@ ; shift $# ;;
-u|--user) shift ; export chuser=$1 ;;
-k|--keys) shift ; export keys=true;;
*) echo "Invalid Argument: $1" ; help ; exit 255 ;;
esac
shift
done
get_gnome_proxy
set_proxy
}
first_or_last()
{
sessions="`ps x -o pid,ppid,comm |grep -e 'setup-chroot' |grep -v grep |grep -v $INSTANCE -c`"
return $sessions
}
get_gnome_proxy()
{
if which gconftool-2 &>/dev/null; then
mode=`gconftool-2 -g /system/proxy/mode 2>/dev/null`
use_http_proxy=`gconftool-2 -g /system/http_proxy/use_http_proxy 2>/dev/null`
use_same_proxy=`gconftool-2 -g /system/http_proxy/use_same_proxy 2>/dev/null`
use_authentication=`gconftool-2 -g /system/http_proxy/use_authentication 2>/dev/null`
authentication_user=`gconftool-2 -g /system/http_proxy/authentication_user 2>/dev/null`
authentication_password=`gconftool-2 -g /system/http_proxy/authentication_password 2>/dev/null`
host=`gconftool-2 -g /system/http_proxy/host 2>/dev/null`
port=`gconftool-2 -g /system/http_proxy/port 2>/dev/null`
ignore_hosts=`gconftool-2 -g /system/http_proxy/ignore_hosts 2>/dev/null`
socks_host=`gconftool-2 -g /system/proxy/socks_host 2>/dev/null`
socks_port=`gconftool-2 -g /system/proxy/socks_port 2>/dev/null`
secure_host=`gconftool-2 -g /system/proxy/secure_host 2>/dev/null`
secure_port=`gconftool-2 -g /system/proxy/secure_port 2>/dev/null`
ftp_host=`gconftool-2 -g /system/proxy/ftp_host 2>/dev/null`
ftp_port=`gconftool-2 -g /system/proxy/ftp_port 2>/dev/null`
fi
if [ -z "$use_authentication" ]; then use_authentication=false ; fi
}
set_proxy()
{
if [ "$mode" == "manual" ]; then
if $use_http_proxy && [ -n "$host" ] && [ -n "$port" ] ; then
no_proxy=`echo "$ignore_hosts" |sed -e 's/\[//g' |sed -e 's/\]//g'`
export no_proxy
if $use_authentication; then
if [ -n "$authentication_user" ] && [ -n "$authentication_password" ]; then
PROXY_AUTH="$authentication_user:$authentication_password"
elif [ -n "$authentication_user" ]; then
PROXY_AUTH="$authentication_user"
fi
export http_proxy=http://${PROXY_AUTH}@$host:$port
if [ -n "$ftp_host" ] && [ -n "$ftp_port" ]; then
export ftp_proxy=http://${PROXY_AUTH}@$ftp_host:$ftp_port
fi
if [ -n "$secure_host" ] && [ -n "$secure_port" ]; then
export https_proxy=https://${PROXY_AUTH}@$secure_host:$secure_port
fi
else
if [ -n "$ftp_host" ] && [ -n "$ftp_port" ]; then
export ftp_proxy=http://$ftp_host:$ftp_port
fi
export http_proxy=http://$host:$port
if [ -n "$secure_host" ] && [ -n "$secure_port" ]; then
export https_proxy=https://$secure_host:$secure_port
fi
fi
fi
fi
}
mounted()
{
if mountpoint $1 >/dev/null 2>&1; then
return 0
else
return 1
fi
}
do_mounts()
{
if ! mounted dev ; then mount --bind /dev dev ; fi
if ! mounted tmp ; then mount -t tmpfs /tmp tmp ; fi
if ! mounted proc ; then mount -t proc proc proc ; fi
if ! mounted sys ; then mount -t sysfs none sys ; fi
if ! mounted dev/pts ; then mount --bind /dev/pts dev/pts ; fi
if ! mounted run ; then mount --bind /run run ; fi
if [ -e /run/pcscd ]; then
if ! mounted run/pcscd ; then
mkdir -p run/pcscd
mount --bind /run/pcscd run/pcscd
fi
elif [ -e /var/run/pcscd ]; then
if ! mounted run/pcscd ; then
mkdir -p run/pcscd
mount --bind /var/run/pcscd run/pcscd
fi
fi
if [ -e /run/user/0 ]; then
if ! mounted run/user/0; then
mkdir -p run/user/0
mount --bind /run/user/0 run/user/0
fi
fi
if [ -n "$SUDO_UID" ]; then
if ! mounted run/user/$SUDO_UID; then
mkdir -p run/user/$SUDO_UID
mount --bind /run/user/$SUDO_UID run/user/$SUDO_UID
fi
fi
if [ -e /mnt/wsl ]; then
for mount in `ls -A /mnt`; do
if mounted /mnt/$mount; then
if ! mounted mnt/$mount; then
mkdir -p mnt/$mount
mount --bind /mnt/$mount mnt/$mount
fi
fi
done
fi
}
copy_host_essential()
{
# if which xauth >/dev/null 2>&1 && [ -n "`xauth list`" ]; then
# auth_file=$(xauth info | grep "Authority file" | awk '{print $3}')
# auth_dir="`dirname $auth_file`"
# mkdir -p $PWD$auth_dir
# cp -a $auth_file $PWD$auth_dir/.
# fi
# cp -f /etc/resolv.conf etc/resolv.conf
rm -f etc/resolv.conf
cp -a /etc/resolv.conf etc/resolv.conf
if ${keys:=false}; then
if [ -e ~/.ssh ]; then
mkdir -p root
cp -Prfd ~/.ssh root/.
fi
fi
}
launch_chroot()
{
do_mounts
if first_or_last; then
copy_host_essential
if which dbus-uuidgen > /dev/null; then
mkdir -p var/lib/dbus
dbus-uuidgen --get > var/lib/dbus/machine-id
fi
fi
if [ -n "$chuser" ]; then
chroot_cmd="chroot --userspec=$chuser"
else
chroot_cmd="chroot"
fi
$chroot_cmd "$PWD" /ts/TS_ENV
howdiditgo=$?
if ${keys:=false}; then
kill_ssh_agent
secure_keys
fi
if first_or_last ; then
do_unmounts
if [ -n "$auth_file" ]; then
rm $PWD$auth_file
fi
if [ -e cleanstage2 ] ; then
ts/bin/clean_chroot
fi
fi
}
kill_ssh_agent()
{
if [ -f root/.agent.env ] ; then
. root/.agent.env > /dev/null
kill -9 $SSH_AGENT_PID > /dev/null 2>&1
fi
}
secure_keys()
{
if [ -e root/.ssh ]; then
rm -rf root/.ssh
fi
}
do_unmounts()
{
for mount in dev/pts dev tmp proc sys run/pcscd run/user/0 run/user/$SUDO_UID run; do
if [ -e $mount ]; then
while mounted $mount ; do
umount -l $mount
if mounted $mount; then
fuser -kv "$PWD/$mount" > /dev/null 2>&1
fi
done
fi
done
if [ -e mnt/wsl ]; then
for mount in `ls -A mnt`; do
while mounted mnt/$mount; do
umount -l mnt/$mount
done
done
fi
}
main()
{
get_opts $@
state_checks
launch_chroot
if [ -e dostage4 ] ; then
copy_host_essential
launch_chroot
fi
if [ -e installed ] ; then
rm installed
if [ "$install_only" == "true" ] ; then
exit 0
else
copy_host_essential
launch_chroot
fi
fi
}
trap "do_unmounts; exit 1" SIGINT SIGHUP SIGTERM
main $@
if [ "$howdiditgo" == "0" ] ; then echo -e "\nGoodbye. \n" ; fi
exit $howdiditgo