forked from Thinstation/thinstation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup-chroot
executable file
·158 lines (141 loc) · 3.38 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
#!/bin/bash
INSTANCE=$$
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.
-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.
-d : Use an alternate build directory. Default = /ts/5.1
-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
-i|--install) install_only=true ;;
-d|--directory) shift ; build_dir=$1 ;;
-b|--build) build=true ;;
-h|--help) help ; exit 255 ;;
-o|--options) shift ; build_opts=$@ ; shift $# ;;
-c|--clean) unset build ; shift $# ; touch ./CLEAN ;;
-e|--exec) shift ; exec_cmd=$@ ; shift $# ;;
*) echo "Invalid Argument: $1" ; help ; exit 255 ;;
esac
shift
done
}
first_or_last()
{
sessions="`ps x -o pid,ppid,comm |grep -e 'setup-chroot' |grep -v grep |grep -v $INSTANCE -c`"
return $sessions
}
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 --bind /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
fi
chroot /$PWD /bin/bash --rcfile ./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
}
set_cmd()
{
if [ ! -z "$exec_cmd" ]; then
echo "$exec_cmd" > BUILD.$INSTANCE
fi
}
set_build()
{
if [ "$build" == "true" ] ; then
if [ -z "$build_dir" ] ; then
build_dir=/ts/5.1
fi
echo "cd $build_dir" > BUILD.$INSTANCE
echo "./build $build_opts" >> BUILD.$INSTANCE
fi
}
main()
{
get_opts $@
state_checks
set_build
set_cmd
launch_chroot
if [ -e installed ] ; then
copy_host_essential
rm installed
if [ "$install_only" == "true" ] ; then
exit 0
else
launch_chroot
fi
fi
}
main $@
if [ "$howdiditgo" == "0" ] ; then echo -e "\nGoodbye. \n" ; fi
exit 0