-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
build.sh
executable file
·356 lines (317 loc) · 10.6 KB
/
build.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/usr/bin/env sh
set -e -u
cwd="$(realpath)"
export cwd
# Only run as superuser
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
kernel="$(uname -r)"
case $kernel in
'13.3-STABLE' | '14.1-STABLE' | '15.0-CURRENT') ;;
*)
echo "FreeBSD or GhostBSD release is not supported."
exit 1
;;
esac
# Use find to locate base files and extract filenames directly, converting newlines to spaces
desktop_list=$(find packages -type f -name '*base*' -exec basename {} \; | tr '\n' ' ')
# Find all files in the desktop_config directory
desktop_config_list=$(find desktop_config -type f)
help_function()
{
printf "Usage: %s -d desktop -r release type\n" "$0"
printf "\t-h for help\n"
printf "\t-d Desktop: %s\n" "${desktop_list}"
printf "\t-b Build type: unstable or release\n"
printf "\t-t Test: FreeBSD os packages\n"
exit 1 # Exit script after printing help
}
# Set mate and release to be default
export desktop="mate"
export build_type="release"
while getopts "d:b:th" opt
do
case "$opt" in
'd') export desktop="$OPTARG" ;;
'b') export build_type="$OPTARG" ;;
't') export desktop="test" ; build_type="test";;
'h') help_function ;;
'?') help_function ;;
*) help_function ;;
esac
done
if [ "${build_type}" = "test" ] ; then
PKG_CONF="FreeBSD"
elif [ "${build_type}" = "release" ] ; then
PKG_CONF="GhostBSD"
elif [ "${build_type}" = "unstable" ] ; then
PKG_CONF="GhostBSD_Unstable"
else
printf "\t-b Build type: unstable or release"
exit 1
fi
# validate desktop packages
if [ ! -f "${cwd}/packages/${desktop}" ] ; then
echo "The packages/${desktop} file does not exist."
echo "Please create a package file named '${desktop}'and place it under packages/."
echo "Or use a valid desktop below:"
echo "$desktop_list"
echo "Usage: ./build.sh -d desktop"
exit 1
fi
# validate desktop
if [ ! -f "${cwd}/desktop_config/${desktop}.sh" ] ; then
echo "The desktop_config/${desktop}.sh file does not exist."
echo "Please create a config file named '${desktop}.sh' like these config:"
echo "$desktop_config_list"
exit 1
fi
if [ "${desktop}" != "mate" ] ; then
DESKTOP=$(echo "${desktop}" | tr '[:lower:]' '[:upper:]')
community="-${DESKTOP}"
else
community=""
fi
workdir="/usr/local"
livecd="${workdir}/ghostbsd-build"
base="${livecd}/base"
iso="${livecd}/iso"
software_packages="${livecd}/software_packages"
base_packages="${livecd}/base_packages"
release="${livecd}/release"
export release
cd_root="${livecd}/cd_root"
live_user="ghostbsd"
export live_user
time_stamp=""
release_stamp=""
label="GhostBSD"
workspace()
{
# Unmount any existing mounts and clean up
umount ${software_packages} >/dev/null 2>/dev/null || true
umount ${base_packages} >/dev/null 2>/dev/null || true
umount ${release}/dev >/dev/null 2>/dev/null || true
zpool destroy ghostbsd >/dev/null 2>/dev/null || true
umount ${release} >/dev/null 2>/dev/null || true
# Remove old build directory if it exists
if [ -d "${cd_root}" ] ; then
chflags -R noschg ${cd_root}
rm -rf ${cd_root}
fi
# Detach memory device if previously attached
mdconfig -d -u 0 >/dev/null 2>/dev/null || true
# Remove old pool image if it exists
if [ -f "${livecd}/pool.img" ] ; then
rm ${livecd}/pool.img
fi
# Create necessary directories for the build
mkdir -p ${livecd} ${base} ${iso} ${software_packages} ${base_packages} ${release}
# Create a new pool image file of 6GB
POOL_SIZE='6g'
truncate -s ${POOL_SIZE} ${livecd}/pool.img
# Attach the pool image as a memory disk
mdconfig -f ${livecd}/pool.img -u 0
# Attempt to create the ZFS pool with error handling
if ! zpool create -O mountpoint="${release}" -O compression=zstd-9 ghostbsd /dev/md0; then
# Provide detailed error message in case of failure
echo "Error: Failed to create ZFS pool 'ghostbsd' with the following command:"
echo "zpool create -O mountpoint='${release}' -O compression=zstd-9 ghostbsd /dev/md0"
# Clean up resources in case of failure
zpool destroy ghostbsd 2>/dev/null || true
mdconfig -d -u 0 2>/dev/null || true
rm -f ${livecd}/pool.img 2>/dev/null || true
# Exit with an error code
exit 1
fi
}
base()
{
if [ "${desktop}" = "test" ] ; then
base_list="$(cat "${cwd}/packages/test_base")"
vital_base="$(cat "${cwd}/packages/vital/test_base")"
else
base_list="$(cat "${cwd}/packages/base")"
vital_base="$(cat "${cwd}/packages/vital/base")"
fi
mkdir -p ${release}/etc
cp /etc/resolv.conf ${release}/etc/resolv.conf
mkdir -p ${release}/var/cache/pkg
mount_nullfs ${base_packages} ${release}/var/cache/pkg
# shellcheck disable=SC2086
pkg-static -r ${release} -R "${cwd}/pkg/" install -y -r ${PKG_CONF}_base ${base_list}
# shellcheck disable=SC2086
pkg-static -r ${release} -R "${cwd}/pkg/" set -y -v 1 ${vital_base}
rm ${release}/etc/resolv.conf
umount ${release}/var/cache/pkg
touch ${release}/etc/fstab
mkdir ${release}/cdrom
}
set_ghostbsd_version()
{
if [ "${desktop}" = "test" ] ; then
version="$(date +%Y-%m-%d)"
else
version="-$(cat ${release}/etc/version)"
fi
iso_path="${iso}/${label}${version}${release_stamp}${time_stamp}${community}.iso"
}
packages_software()
{
if [ "${build_type}" = "unstable" ] ; then
cp pkg/GhostBSD_Unstable.conf ${release}/etc/pkg/GhostBSD.conf
fi
cp /etc/resolv.conf ${release}/etc/resolv.conf
mkdir -p ${release}/var/cache/pkg
mount_nullfs ${software_packages} ${release}/var/cache/pkg
mount -t devfs devfs ${release}/dev
packages="$(cat "${cwd}/packages/${desktop}")"
vital_packages="$(cat "${cwd}/packages/vital/${desktop}")"
# shellcheck disable=SC2086
pkg-static -c ${release} install -y ${packages}
# shellcheck disable=SC2086
pkg-static -c ${release} set -y -v 1 ${vital_packages}
mkdir -p ${release}/proc
mkdir -p ${release}/compat/linux/proc
rm ${release}/etc/resolv.conf
umount ${release}/var/cache/pkg
}
fetch_x_drivers_packages()
{
if [ "${build_type}" = "release" ] ; then
pkg_url=$(pkg-static -R pkg/ -vv | grep '/stable.*/latest' | cut -d '"' -f2)
else
pkg_url=$(pkg-static -R pkg/ -vv | grep '/unstable.*/latest' | cut -d '"' -f2)
fi
mkdir ${release}/xdrivers
yes | pkg -R "${cwd}/pkg/" update
echo """$(pkg -R "${cwd}/pkg/" rquery -x -r ${PKG_CONF} '%n %n-%v.pkg' 'nvidia-driver' | grep -v libva)""" > ${release}/xdrivers/drivers-list
pkg_list="""$(pkg -R "${cwd}/pkg/" rquery -x -r ${PKG_CONF} '%n-%v.pkg' 'nvidia-driver' | grep -v libva)"""
for line in $pkg_list ; do
fetch -o ${release}/xdrivers "${pkg_url}/All/$line"
done
}
rc()
{
chroot ${release} touch /etc/rc.conf
chroot ${release} sysrc hostname='livecd'
chroot ${release} sysrc zfs_enable="YES"
chroot ${release} sysrc kld_list="linux linux64 cuse fusefs hgame"
chroot ${release} sysrc linux_enable="YES"
chroot ${release} sysrc devfs_enable="YES"
chroot ${release} sysrc devfs_system_ruleset="devfsrules_common"
chroot ${release} sysrc moused_enable="YES"
chroot ${release} sysrc dbus_enable="YES"
chroot ${release} sysrc lightdm_enable="NO"
chroot ${release} sysrc webcamd_enable="YES"
chroot ${release} sysrc ipfw_enable="YES"
chroot ${release} sysrc firewall_enable="YES"
chroot ${release} sysrc cupsd_enable="YES"
chroot ${release} sysrc avahi_daemon_enable="YES"
chroot ${release} sysrc avahi_dnsconfd_enable="YES"
chroot ${release} sysrc ntpd_enable="YES"
chroot ${release} sysrc ntpd_sync_on_start="YES"
}
ghostbsd_config()
{
# echo "gop set 0" >> ${release}/boot/loader.rc.local
mkdir -p ${release}/usr/local/share/ghostbsd
echo "${desktop}" > ${release}/usr/local/share/ghostbsd/desktop
# bypass automount for live iso
mv ${release}/usr/local/etc/devd/automount_devd.conf ${release}/usr/local/etc/devd/automount_devd.conf.skip
mv ${release}/usr/local/etc/devd/automount_devd_localdisks.conf ${release}/usr/local/etc/devd/automount_devd_localdisks.conf.skip
# Mkdir for linux compat to ensure /etc/fstab can mount when booting LiveCD
chroot ${release} mkdir -p /compat/linux/dev/shm
# Add /boot/entropy file
chroot ${release} touch /boot/entropy
# default GhostBSD to local time instead of UTC
chroot ${release} touch /etc/wall_cmos_clock
}
desktop_config()
{
# run config for GhostBSD flavor
sh "${cwd}/desktop_config/${desktop}.sh"
}
uzip()
{
install -o root -g wheel -m 755 -d "${cd_root}"
mkdir "${cd_root}/data"
zfs snapshot ghostbsd@clean
zfs send -p -c -e ghostbsd@clean | dd of=/usr/local/ghostbsd-build/cd_root/data/system.img status=progress bs=1M
}
ramdisk()
{
ramdisk_root="${cd_root}/data/ramdisk"
mkdir -p "${ramdisk_root}"
cd "${release}"
tar -cf - rescue | tar -xf - -C "${ramdisk_root}"
cd "${cwd}"
install -o root -g wheel -m 755 "init.sh.in" "${ramdisk_root}/init.sh"
sed "s/@VOLUME@/GHOSTBSD/" "init.sh.in" > "${ramdisk_root}/init.sh"
mkdir "${ramdisk_root}/dev"
mkdir "${ramdisk_root}/etc"
touch "${ramdisk_root}/etc/fstab"
install -o root -g wheel -m 755 "rc.in" "${ramdisk_root}/etc/rc"
cp ${release}/etc/login.conf ${ramdisk_root}/etc/login.conf
makefs -b '10%' "${cd_root}/data/ramdisk.ufs" "${ramdisk_root}"
gzip "${cd_root}/data/ramdisk.ufs"
rm -rf "${ramdisk_root}"
}
boot()
{
cd "${release}"
tar -cf - boot | tar -xf - -C "${cd_root}"
cp COPYRIGHT ${cd_root}/COPYRIGHT
cd "${cwd}"
cp LICENSE ${cd_root}/LICENSE
cp -R boot/ ${cd_root}/boot/
mkdir ${cd_root}/etc
# Try to unmount dev and release if mounted
umount ${release}/dev >/dev/null 2>/dev/null || true
umount ${release} >/dev/null 2>/dev/null || true
# Export ZFS pool and ensure it's clean
zpool export ghostbsd
timeout=10
while zpool status ghostbsd >/dev/null 2>&1; do
sleep 1
timeout=$((timeout - 1))
if [ $timeout -eq 0 ]; then
echo "Failed to cleanly export ZFS pool within timeout"
break
fi
done
}
image()
{
cd script
sh mkisoimages.sh -b $label "$iso_path" ${cd_root}
cd -
ls -lh "$iso_path"
cd ${iso}
shafile=$(echo "${iso_path}" | cut -d / -f6).sha256
torrent=$(echo "${iso_path}" | cut -d / -f6).torrent
tracker1="http://tracker.openbittorrent.com:80/announce"
tracker2="udp://tracker.opentrackr.org:1337"
tracker3="udp://tracker.coppersurfer.tk:6969"
echo "Creating sha256 \"${iso}/${shafile}\""
sha256 "$(echo "${iso_path}" | cut -d / -f6)" > "${iso}/${shafile}"
transmission-create -o "${iso}/${torrent}" -t ${tracker1} -t ${tracker2} -t ${tracker3} "${iso_path}"
chmod 644 "${iso}/${torrent}"
cd -
}
workspace
base
set_ghostbsd_version
if [ "${desktop}" != "test" ] ; then
packages_software
fetch_x_drivers_packages
rc
desktop_config
ghostbsd_config
fi
uzip
ramdisk
boot
image