-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathradish-build-dir
executable file
·242 lines (194 loc) · 5.78 KB
/
radish-build-dir
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
#!/bin/bash
termprocesses()
{
for i in /proc/[0-9]*
do readlink "${i}/root" | fgrep -q "${1}" && (\
pid=`echo ${i} | cut -d/ -f3`
execfile=`readlink /proc/${pid}/exe`
echo "${execfile}" | fgrep -q "${1}" && (\
echo "Running process: ${execfile}"
mv "${execfile}" "${execfile}.backup"
kill "${2}" "${pid}" || true
sleep 2
mv "${execfile}.backup" "${execfile}" ) || true ) || true
done
}
cleanup()
{
set +e
if [ "${mountdir}" != "" -a -d "${mountdir}" ]
then
termprocesses "${mountdir}" -KILL
[ -d "${mountdir}/proc" ] && umount "${mountdir}/proc" 2>/dev/null
[ -d "${mountdir}/sys" ] && umount "${mountdir}/sys" 2>/dev/null
fi
}
if [ "`id -u`" != "0" ]
then
echo "This script can only run as root"
exit 1
fi
check="true"
for ex in \
echo pwd mktemp mount debootstrap mv cat chroot readlink \
kill cut grep fgrep rm umount sleep rmdir
do
loc=`which "${ex}"`
if [ "${loc}" = "" ]
then
echo "${ex} is missing"
check="false"
fi
done
if [ "${check}" = "false" ]
then
echo "Some utilities are missing"
exit 1
fi
suite="wheezy"
kernel_package_ubuntu_lts=""
url=""
if [ "$1" != "" ]
then
suite="$1"
if [ "$2" != "" ]
then
kernel_package_ubuntu_lts="$2"
fi
if [ "$3" != "" ]
then
url="$3"
fi
fi
set -e
unset LC_CTYPE LC_MESSAGES LC_ALL
export LANG=C
trap cleanup EXIT
currdir=`pwd`
mountdir=`mktemp -d --tmpdir="${currdir}" radish-mount.XXXXXXXXXX`
echo "Installing the base system..."
debootstrap "${suite}" "${mountdir}" "${url}"
if grep -Eq '^Label: *Ubuntu' "${mountdir}/var/lib/apt/lists/"*_*Release
then
is_debian="false"
if [ "${kernel_package_ubuntu_lts}" = "" ]
then
kernel_package="linux-generic"
else
kernel_package="linux-generic-lts-${kernel_package_ubuntu_lts}"
fi
else
is_debian="true"
if [ `uname -m` = "x86_64" ]
then
kernel_package="linux-image-amd64"
else
kernel_package="linux-image-486"
fi
fi
echo "Configuring package repositories..."
if [ "${is_debian}" = "true" ]
then
if [ "${url}" = "" ]
then
url="http://ftp.us.debian.org/debian"
fi
cat > "${mountdir}/etc/apt/sources.list" << __EOF__
deb ${url} ${suite} main contrib non-free
deb-src ${url} ${suite} main contrib non-free
deb ${url} ${suite}-updates main contrib non-free
deb-src ${url} ${suite}-updates main contrib non-free
__EOF__
else
if [ "${url}" = "" ]
then
url="http://us.archive.ubuntu.com/ubuntu/"
fi
cat > "${mountdir}/etc/apt/sources.list" << __EOF__
deb ${url} ${suite} main restricted universe multiverse
deb-src ${url} ${suite} main restricted universe multiverse
deb ${url} ${suite}-updates main restricted universe multiverse
deb-src ${url} ${suite}-updates main restricted universe multiverse
deb ${url} ${suite}-backports main restricted universe multiverse
deb-src ${url} ${suite}-backports main restricted universe multiverse
deb ${url} ${suite}-security main restricted universe multiverse
deb-src ${url} ${suite}-security main restricted universe multiverse
__EOF__
fi
echo "Preparing system for update..."
mount --bind /proc "${mountdir}/proc"
mount --bind /sys "${mountdir}/sys"
invoke_rc_d_renamed="false"
initctl_renamed="false"
if [ -f "${mountdir}/usr/sbin/invoke-rc.d" ]
then
invoke_rc_d_renamed="true"
mv "${mountdir}/usr/sbin/invoke-rc.d" \
"${mountdir}/usr/sbin/invoke-rc.d.backup"
cat > "${mountdir}/usr/sbin/invoke-rc.d" << __EOF__
#!/bin/sh
exit 0
__EOF__
chmod +x "${mountdir}/usr/sbin/invoke-rc.d"
fi
if [ -f "${mountdir}/sbin/initctl" ]
then
initctl_renamed="true"
mv "${mountdir}/sbin/initctl" \
"${mountdir}/sbin/initctl.backup"
cat > "${mountdir}/sbin/initctl" << __EOF__
#!/bin/sh
exit 0
__EOF__
chmod +x "${mountdir}/sbin/initctl"
fi
echo "Updating repositories..."
chroot "${mountdir}" apt-get update
echo "Updating installed packages..."
chroot "${mountdir}" apt-get upgrade -y
chroot "${mountdir}" apt-get dist-upgrade -y
echo "Installing kernel and bootloader..."
chroot "${mountdir}" sh -c "DEBIAN_FRONTEND=noninteractive \
apt-get install -y --no-install-recommends \
${kernel_package} grub-pc"
echo "Installing additional packages..."
chroot "${mountdir}" apt-get install -y --no-install-recommends \
avahi-autoipd avahi-daemon avahi-utils dbus isc-dhcp-server \
lighttpd openssh-client openssh-server screen tftp-hpa tftpd-hpa \
bzip2 dialog partclone parted less psmisc initramfs-tools
chroot "${mountdir}" apt-get -f install -y
echo "Restoring filesystem into usable state..."
if [ "${invoke_rc_d_renamed}" = "true" ]
then
mv "${mountdir}/usr/sbin/invoke-rc.d.backup" \
"${mountdir}/usr/sbin/invoke-rc.d"
fi
if [ "${initctl_renamed}" = "true" ]
then
mv "${mountdir}/sbin/initctl.backup" \
"${mountdir}/sbin/initctl"
fi
echo "Updating initial ramdisk(s)..."
chroot "${mountdir}" update-initramfs -u -k all
echo "Terminating processes left from installation..."
termprocesses "${mountdir}" -TERM
sleep 5
termprocesses "${mountdir}" -KILL
if [ -f "${mountdir}/lib/udev/rules.d/75-persistent-net-generator.rules" ]
then
echo "Disabling persistent network device generator rules..."
echo 'GOTO="persistent_net_generator_end"' > \
"${mountdir}/lib/udev/rules.d/75-persistent-net-generator.rules.tmp"
cat "${mountdir}/lib/udev/rules.d/75-persistent-net-generator.rules" >> \
"${mountdir}/lib/udev/rules.d/75-persistent-net-generator.rules.tmp"
cat "${mountdir}/lib/udev/rules.d/75-persistent-net-generator.rules.tmp" > \
"${mountdir}/lib/udev/rules.d/75-persistent-net-generator.rules"
rm "${mountdir}/lib/udev/rules.d/75-persistent-net-generator.rules.tmp"
fi
echo "Adding regular user..."
chroot "${mountdir}" adduser --disabled-password --gecos "User" user
echo "Un-mounting filesystem image..."
umount "${mountdir}/proc"
umount "${mountdir}/sys"
trap "" EXIT
echo "Directory ${mountdir} is ready."