This repository has been archived by the owner on Jun 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-debian-mpd-chroot
executable file
·183 lines (163 loc) · 7.38 KB
/
make-debian-mpd-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
#!/usr/bin/env bash
##
## `make-debian-mpd-chroot' is a script to build the latest
## version of mpd for debian inside a temporary debian chroot on a
## non-debian build environment/host, like arch.
##
## Copyright (C) 2015 Ronald van Engelen <[email protected]>
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
##
## The main and helper scripts and documentation are published at
## https://github.com/ronalde/build-latest-mpd-for-debian
##
## Apart from a fairly recent versions of debootstrap, systemd and
## wget or curl, no additional packages are required for the build
## environment. When you use debian or ubuntu as your build
## enviroment, you should have a look at git-buildpackage.
##
## Also see `README'
LANG=C
appname="build-latest-mpd-for-debian"
DEBCHROOT_MIRROR="${DEBCHROOT_MIRROR:-http://ftp.debian.org/debian}"
## or use local apt cacher like approx
#DEBCHROOT_MIRROR="http://localnet:9999/debian"
DEBCHROOT_VERSION="${DEBCHROOT_VERSION:-jessie}"
DEBCHROOT_ARCH="${DEBCHROOT_ARCH:-amd64}"
debchroot_variant="buildd"
debchroot_additional_packages="devscripts,coreutils,dpkg-dev,apt-utils,openssh-client,libwww-perl"
helper_script_name=build-latest-debian-mpd-package
helper_script_downloadpath=/tmp
helper_script_downloadtarget=${helper_script_downloadpath}/${helper_script_name}
helper_script_path=/usr/local/bin
tempchroot_template="${appname}.${DEBCHROOT_VERSION}.${DEBCHROOT_ARCH}.XXXX"
url="https://raw.githubusercontent.com/ronalde/${appname}/master/${helper_script_name}"
scriptname="$(basename ${0})"
function create_chroot {
## download and install a virgin debian chroot
printf "** performing debootstrap in \`%s' ... " "${debchroot_targetdir}" 2>&1 | tee -a ${logfile}
res="$(debootstrap \
--arch=${DEBCHROOT_ARCH} \
--include="${debchroot_additional_packages}" \
--variant=${debchroot_variant} \
${DEBCHROOT_VERSION} \
${debchroot_targetdir} \
"${DEBCHROOT_MIRROR}" >> ${logfile} 2>&1)"
if [[ $? -ne 0 ]]; then
printf "ERROR:\n%s\n" "${res}" 2>&1 | tee -a ${logfile}
exit 1
else
printf "done.\n" 2>&1 | tee -a ${logfile}
fi
}
function download_helper_script {
## get the latest version of the helper script using wget or curl
wget -q "${url}" -O "${helper_script_downloadtarget}" >> ${logfile} 2>&1 || \
curl -s -k -o "${helper_script_downloadtarget}" "${url}" >> ${logfile} 2>&1
}
function install_helper_script {
## set up sources.list inside the chroot, install the helper
## script, add it to root's profile and set autologon
printf "downloading helper script from repository ..." 2>&1 | tee -a ${logfile}
res=$(download_helper_script)
if [[ $? -ne 0 ]]; then
printf "\nerror: could not download the the helper script from \n%s\n" \
"${url}" 2>&1 | tee -a ${logfile}
exit 1
else
printf "done.\n" 2>&1 | tee -a ${logfile}
printf "*** populate sources.list inside the chroot ... " 2>&1 | tee -a ${logfile}
debchroot_sourceslist="${debchroot_targetdir}/etc/apt/sources.list"
printf "deb %s %s main\n" \
"${DEBCHROOT_MIRROR}" "${DEBCHROOT_VERSION}" > ${debchroot_sourceslist}
printf "deb-src %s %s main\n" \
"${DEBCHROOT_MIRROR}" "${DEBCHROOT_VERSION}" >> ${debchroot_sourceslist}
printf "done.\n" 2>&1 | tee -a ${logfile}
printf "** installing the mpd builder script \`%s' inside the chroot ... " \
"${helper_script_name}" 2>&1 | tee -a ${logfile}
res=$(install --mode=0555 "${helper_script_downloadtarget}" "${debchroot_targetdir}${helper_script_path}" >> ${logfile} 2>&1)
if [[ $? -ne 0 ]]; then
printf "ERROR:\n%s\n" "${res}" 2>&1 | tee -a ${logfile}
exit 1
else
printf "done.\n" 2>&1 | tee -a ${logfile}
fi
dotprofile="${debchroot_targetdir}/root/.profile"
printf "** insert the script in to \`%s' ... " "${dotprofile}" 2>&1 | tee -a ${logfile}
echo "${helper_script_path}/${helper_script_name}" > ${dotprofile}
printf "done.\n" 2>&1 | tee -a ${logfile}
printf "** setting autologin for root inside chroot ... " 2>&1 | tee -a ${logfile}
console_getty=${debchroot_targetdir}/lib/systemd/system/console-getty.service
sed -i 's#^ExecStart.*#ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud console 115200,38400,9600 $TERM#' "${console_getty}"
printf "done.\n" 2>&1 | tee -a ${logfile}
fi
}
function spawn_chroot {
## boot the chroot using systemd-nspawn and wait for it to execute
## the helper script and finally halt, after which try to copy the
## created mpd package from the chroot to the current directory.
printf "** spawning the chroot.\n" 2>&1 | tee -a ${logfile}
## boot the chroot
systemd-nspawn -bD "${debchroot_targetdir}" 2>&1 | tee -a ${logfile}
printf "** done spawning.\n" 2>&1 | tee -a ${logfile}
## get the created mpd package
package_path="$(find "${debchroot_targetdir}/root" -name 'mpd_*.deb')"
if [[ $? -ne 0 ]]; then
printf "error: could not find a package in \`%s'.\n" "${package_path}" 2>&1 | tee -a ${logfile}
else
package_name="$(basename "${package_path}")"
res=$(cp ${package_path} ./2>&1 | tee -a ${logfile})
if [[ $? -ne 0 ]]; then
printf "error: could not copy package from \`%s'.\n" 2>&1 | tee -a ${logfile}
printf "inspect the chroot by running: \`%s'\n" \
"systemd-nspawn -bD "${debchroot_targetdir}"" 2>&1 | tee -a ${logfile}
else
printf "** all done; the resulting package is copied locally to:\n%s\n\n" \
"./${package_name}" 2>&1 | tee -a ${logfile}
printf "(you may delete the chroot \`%s')\n" \
"./${debchroot_targetdir}" 2>&1 | tee -a ${logfile}
fi
fi
}
## main
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
## we're being sourced (for testing purposes); do nothing
debchroot_targetdir="$1"
if [[ -z "${debchroot_targetdir}" ]]; then
printf "sourcing is only useful when you specify an existing chroot.\n"
else
printf "script \`%s' is being sourced\n" "${BASH_SOURCE[0]}"
if [[ ! -d "${debchroot_targetdir}" ]]; then
printf "warning: specified debchroot_targetdir \`%s' does not exist.\n" "${debchroot_targetdir}"
else
printf "using exisiting debchroot_targetdir \`%s'\n" "${debchroot_targetdir}"
install_helper_script
fi
fi
else
## we're being run
logfile=$(mktemp --tmpdir "${scriptname}.XXXX")
## start logging
printf "starting %s at %s\n" "${scriptname}" "$(date)" 2>&1 | tee ${logfile}
printf "** logging to \`%s'.\n" "${logfile}" 2>&1 | tee -a ${logfile}
printf "** creating a new temporary debchroot_targetdir.\n" 2>&1 | tee -a ${logfile}
debchroot_targetdir="$(mktemp -d ${tempchroot_template})"
if [[ $? -ne 0 ]]; then
printf "ERROR:\n%s\n" "${debchroot_targetdir}" 2>&1 | tee -a ${logfile}
else
printf "** debchroot_targetdir is \`%s'.\n" "${debchroot_targetdir}" 2>&1 | tee -a ${logfile}
create_chroot && \
install_helper_script && \
spawn_chroot
fi
fi