-
Notifications
You must be signed in to change notification settings - Fork 0
/
deb-builder.sh
executable file
·198 lines (158 loc) · 4.51 KB
/
deb-builder.sh
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
set -ex
if [ $(id -u) != 0 ]; then
>&2 echo -e "\e[91mERROR (!) Required: id == 0\e[0m"
exit 1
fi
usage()
{
if (( $1 )); then
>&2 echo "Try '$(basename $0) --help' for more information"
exit 1
else
cat << EOF
$(echo -e "\e[96mUsage: $(basename $0) [options]\e[0m")
Wrapper for pbuilder, pdebuild and debootstrap.
Build a Debian package in a chroot.
Options:
--mirror [suite] create system from 'http://deb.debian.org/debian' URL
--iso [suite] [iso-full-path] create system from ISO file 'file://media/cdrom' URL
--login [path-to-tgz] login to tgz
--login-save [path-to-tgz] login to tgz with save mode
--build [path-to-tgz] default build package (clean)
--build-debug [path-to-tgz] package build with including debug symbols
--sound make sound after build (use together with build options)
EOF
exit 0
fi
}
arch=amd64
debootstrap_cmd()
{
trap 'rm -rf $target; umount /media/cdrom &>/dev/null' SIGINT ERR
debootstrap --no-check-gpg --components=main,contrib,non-free --arch=$arch $suite $target "$1"
}
mount_staff()
{
mount --bind /dev $1/dev
mount --bind /dev/pts $1/dev/pts
mount --bind /proc $1/proc
mount --bind /sys $1/sys
}
create_chroot()
{
suite="$2"
iso_name="${3##*/}"
[ -z $iso_name ] && target="/$suite-chroot${iso_name/.iso}" || \
target="/$suite-chroot-${iso_name/.iso}"
if mount | grep $target &>/dev/null; then
>&2 echo -e "\e[91mERROR (!) chroot '$target' already to use\e[0m"
exit 1
fi
if [ ! -d $target ]; then
if [ "$1" == "--mirror" ]; then
debootstrap_cmd "http://deb.debian.org/debian"
else
mount $3 /media/cdrom
debootstrap_cmd "file:///media/cdrom"
umount /media/cdrom
fi
fi
cp /usr/local/bin/chroot-env.sh $target/usr/local/bin
cp /etc/hosts $target/etc
mount_staff $target
}
umount_staff()
{
for mp in proc dev/pts dev sys; do
umount $1/$mp &>/dev/null
done
}
launch()
{
create_chroot $1 $2 $3
trap 'umount_staff $target' SIGINT ERR
LANG=C chroot $target chroot-env.sh $suite
umount_staff $target
[ -e /var/cache/pbuilder${target}.tgz ] && rm /var/cache/pbuilder${target}.tgz
cd $target; tar -cvzf /var/cache/pbuilder${target}.tgz *
[ -e /var/cache/pbuilder${target}.tgz ] && rm -rf $target
}
_pdebuild()
{
dir_format=$(date "+D:%d-%m-%y+T:%T")
result_dir=/var/cache/pbuilder/result
_uniq=$(mktemp)
pdebuild --debbuildopts --source-option='-itags' \
-- --no-auto-cross --basetgz $1 --buildresult $result_dir/$dir_format 2>&1 | tee $_uniq
mkdir $result_dir/$dir_format/bin
mkdir $result_dir/$dir_format/build
mv $result_dir/$dir_format/*.deb $result_dir/$dir_format/bin
cp ../*orig.tar* $result_dir/$dir_format
mv $_uniq $result_dir/$dir_format/build
find $result_dir/$dir_format -type f -exec chmod 644 {} +
chown -R ${SUDO_USER}: $result_dir/$dir_format
for link in last prelast; do
[ -L $result_dir/$link ] && rm $result_dir/$link
done
# get recently added dirs
ldir=$(find /var/cache/pbuilder/result/* -maxdepth 0 -type d -exec ls -1td {} +)
ln -s $(basename $(echo "$ldir" | head -1)) $result_dir/last
if (( $(echo "$ldir" | wc -l) > 1 )); then
ln -s $(basename $(echo "$ldir" | sed -n 2p)) $result_dir/prelast
fi
unset DEB_BUILD_OPTIONS
if (( $sound_option )); then
wav_path=/usr/share/sounds/for-deb-builder/prompt.wav
[ -e $wav_path ] && aplay $wav_path
fi
}
case $1 in
"--mirror")
[ $# != 2 ] && usage 1
launch $1 $2
;;
"--iso")
[ $# != 3 ] && usage 1
launch $1 $2 $3
;;
"--login")
[ $# != 2 ] && usage 1
pbuilder --login --basetgz $2
;;
"--login-save")
[ $# != 2 ] && usage 1
pbuilder --login --save-after-login --basetgz $2
;;
"--build")
[ -z $2 ] && usage 1
[ "$3" == "--sound" ] && sound_option=1
# nocheck - not run tests
#export DEB_BUILD_OPTIONS='nocheck'
_pdebuild $2
upload=/srv/ftp/upload
if [ -d $upload ]; then
f=$(basename $(find $result_dir/$dir_format -name *.dsc -type f))
ff=${f/.dsc}
[ -d $upload/$ff ] && rm -rf $upload/$ff
mkdir $upload/$ff
cp -r $result_dir/$dir_format/* $upload/$ff
chown -R ${SUDO_USER}: $upload/$ff
fi
;;
"--build-debug")
[ -z $2 ] && usage 1
[ "$3" == "--sound" ] && sound_option=1
# noopt - O0 / nostrip - debug symbols have / debug - enable debug info
export DEB_BUILD_OPTIONS='nocheck noopt nostrip debug'
_pdebuild $2
touch $result_dir/$dir_format/bin/DEBUG
chown ${SUDO_USER}: $result_dir/$dir_format/bin/DEBUG
;;
"-h"|"--help")
usage 0
;;
*)
usage 1
;;
esac