forked from Thinstation/thinstation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-chroot
executable file
·198 lines (182 loc) · 6.14 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
#!/bin/bash
export INSTANCE=$$
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/lib/pkg && 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.
"
}
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 $# ;;
*) 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
else
if [ -n "$ftp_host" ] && [ -n "$ftp_port" ]; then
export ftp_proxy=http://$ftp_host:$ftp_port
fi
export http_proxy=http://$host:$port
fi
if [ -n "$secure_host" ] && [ -n "$secure_port" ]; then
export https_proxy=https://$secure_host:$secure_port
fi
fi
fi
}
mounted()
{
if [ "`cat /proc/mounts |grep -e $PWD/$1 -c`" -ne "0" ]; 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
}
copy_host_essential()
{
cp -f /etc/resolv.conf etc/resolv.conf
if [ -e ~/.ssh ]; then
mkdir -p root
cp -Prfd ~/.ssh root/.
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
chroot $PWD /ts/TS_ENV
howdiditgo=$?
if first_or_last ; then
do_unmounts
secure_keys
if [ -e cleanstage2 ] ; then
ts/bin/clean_chroot
fi
fi
}
secure_keys()
{
if [ -e root/.ssh ]; then
rm -rf root/.ssh
fi
}
do_unmounts()
{
for mount in dev/pts dev tmp proc sys ; do
while mounted $mount ; do
umount $mount
done
done
}
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