2020-05-09 09:00:02 +00:00
#!/bin/bash
set -x
set -e
2018-06-21 16:33:30 +00:00
# Build fs, image
2020-06-15 21:27:21 +00:00
# This file is part of PrawnOS (https://www.prawnos.com)
2018-10-11 20:09:25 +00:00
# Copyright (c) 2018 Hal Emmerich <hal@halemmerich.com>
# PrawnOS is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2
# as published by the Free Software Foundation.
# PrawnOS 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 PrawnOS. If not, see <https://www.gnu.org/licenses/>.
2018-06-21 16:33:30 +00:00
2018-08-24 20:05:17 +00:00
#Ensure Sudo
2018-09-05 23:35:33 +00:00
if [ ! $UID = "0" ]
then
echo "Please run this script with sudo, or as root:"
2018-08-24 20:05:17 +00:00
echo " sudo $0 $* "
exit 1
fi
2019-02-27 19:20:11 +00:00
if [ -z " $1 " ]
then
echo "No kernel version supplied"
exit 1
fi
2020-01-11 20:56:45 +00:00
if [ -z " $2 " ]
then
echo "No debian suite supplied"
exit 1
fi
if [ -z " $3 " ]
then
echo "No base file system image filename supplied"
exit 1
fi
2020-06-17 06:51:39 +00:00
if [ -z " $4 " ]
then
echo "No prawnos_root path supplied"
exit 1
fi
2020-06-20 23:44:09 +00:00
if [ -z " $5 " ]
then
echo "No shared scripts path supplied"
exit 1
fi
2020-06-30 03:39:56 +00:00
if [ -z " $6 " ]
then
echo "No Filesystem resources path supplied"
exit 1
fi
2020-07-18 06:36:04 +00:00
if [ -z " $7 " ]
then
echo "No TARGET arch supplied"
exit 1
fi
if [ -z " $8 " ]
then
echo "No Prawnos build directory supplied"
exit 1
fi
2020-06-20 23:44:09 +00:00
2019-02-27 19:20:11 +00:00
KVER = $1
2020-01-11 20:56:45 +00:00
DEBIAN_SUITE = $2
BASE = $3
2020-06-17 06:51:39 +00:00
PRAWNOS_ROOT = $4
2020-06-20 23:44:09 +00:00
PRAWNOS_SHARED_SCRIPTS = $5
2020-06-30 03:39:56 +00:00
PRAWNOS_FILESYSTEM_RESOURCES = $6
2020-07-18 06:36:04 +00:00
TARGET_ARCH = $7
PRAWNOS_BUILD = $8
2019-02-27 19:20:11 +00:00
2020-05-20 18:00:00 +00:00
outmnt = $( mktemp -d -p " $( pwd ) " )
2018-06-21 16:33:30 +00:00
2020-08-05 06:06:44 +00:00
outdev = $( losetup -f)
2018-09-05 23:35:33 +00:00
2020-06-30 03:39:56 +00:00
build_resources = $PRAWNOS_FILESYSTEM_RESOURCES
build_resources_apt = $build_resources /apt
2020-06-11 22:55:01 +00:00
2020-06-20 23:44:09 +00:00
# Import the package lists, shared scripts
source $PRAWNOS_SHARED_SCRIPTS /*
2018-06-21 16:33:30 +00:00
#A hacky way to ensure the loops are properly unmounted and the temp files are properly deleted.
2019-10-12 22:39:17 +00:00
#Without this, a reboot is sometimes required to properly clean the loop devices and ensure a clean build
2018-08-24 20:05:17 +00:00
cleanup( ) {
set +e
2018-06-21 16:33:30 +00:00
2018-08-24 20:05:17 +00:00
umount -l $outmnt > /dev/null 2>& 1
rmdir $outmnt > /dev/null 2>& 1
losetup -d $outdev > /dev/null 2>& 1
2018-06-21 16:33:30 +00:00
umount -l $outmnt > /dev/null 2>& 1
rmdir $outmnt > /dev/null 2>& 1
losetup -d $outdev > /dev/null 2>& 1
2020-05-25 20:48:18 +00:00
#delete the base file, we didn't complete our work
rm -rf $BASE
echo "FILESYSTEM BUILD FAILED"
exit 1
2018-06-21 16:33:30 +00:00
}
2018-08-24 20:05:17 +00:00
trap cleanup INT TERM EXIT
2018-06-21 16:33:30 +00:00
2020-09-06 18:26:38 +00:00
# Retry a command up to 5 times, else fail
retry_until( ) {
2020-09-07 07:33:13 +00:00
#must clear and unclear the "e" flag to avoid trapping into cleanup before retrying
set +e
2020-09-06 18:26:38 +00:00
command = ( " $@ " )
NUM_RETRIES = 0
MAX_RETRIES = 5
2020-09-07 07:33:13 +00:00
until [ " $NUM_RETRIES " -eq 5 ] || ${ command [@] } ; do
echo Apt failure, NUM_RETRIES = $NUM_RETRIES , trying again in 5 seconds
2020-09-06 18:26:38 +00:00
( ( NUM_RETRIES++) )
sleep 5
done
if [ " $NUM_RETRIES " -ge " $MAX_RETRIES " ] ; then
exit 1
fi
2020-09-07 07:33:13 +00:00
set -e
2020-09-06 18:26:38 +00:00
}
2020-06-30 03:39:56 +00:00
# Download, cache externally, and optionally install the specified packages
2020-09-06 18:26:38 +00:00
# also implements retries since the build dockers seem to fail randomly
2020-06-30 03:39:56 +00:00
# 2: mount of the chroot
# 3: list of packages in install
# 4: if true, download, cache, and install. If false just download and cache
apt_install( ) {
2020-07-18 06:36:04 +00:00
PRAWNOS_BUILD = $1
2020-06-30 03:39:56 +00:00
shift
outmnt = $1
shift
install = $1
shift
package_list = ( " $@ " )
2020-09-06 18:26:38 +00:00
retry_until chroot $outmnt apt install -y -d ${ package_list [@] }
2020-07-18 06:36:04 +00:00
cp " $outmnt /var/cache/apt/archives/ " * " $PRAWNOS_BUILD /chroot-apt-cache/ " || true
2020-06-30 03:39:56 +00:00
if [ " $install " = true ] ; then
2020-09-06 18:26:38 +00:00
retry_until chroot $outmnt apt install -y ${ package_list [@] }
2020-06-30 03:39:56 +00:00
fi
}
2019-10-06 18:13:33 +00:00
#layout the partitons and write filesystem information
2018-06-21 16:33:30 +00:00
create_image( ) {
dd if = /dev/zero of = $1 bs = $3 count = $4 conv = sparse
parted --script $1 mklabel gpt
cgpt create $1
2019-10-06 18:13:33 +00:00
kernel_start = 8192
2020-10-27 02:25:06 +00:00
kernel_size = 65536
2019-10-06 18:13:33 +00:00
cgpt add -i 1 -t kernel -b $kernel_start -s $kernel_size -l Kernel -S 1 -T 5 -P 10 $1
#Now the main filesystem
2019-10-12 22:39:17 +00:00
root_start = $(( $kernel_start + $kernel_size ))
2018-06-21 16:33:30 +00:00
end = ` cgpt show $1 | grep 'Sec GPT table' | awk '{print $1}' `
2019-10-06 18:13:33 +00:00
root_size = $(( $end - $root_start ))
2019-10-15 13:51:16 +00:00
cgpt add -i 2 -t data -b $root_start -s $root_size -l Root $1
# $root_size is in 512 byte blocks while ext4 uses a block size of 1024 bytes
2018-06-21 16:33:30 +00:00
losetup -P $2 $1
2019-10-15 13:51:16 +00:00
mkfs.ext4 -F -b 1024 ${ 2 } p2 $(( $root_size / 2 ))
2018-06-21 16:33:30 +00:00
# mount the / partition
2019-10-12 22:39:17 +00:00
mount -o noatime ${ 2 } p2 $5
2018-06-21 16:33:30 +00:00
}
2020-10-27 04:09:54 +00:00
# create a 2.5GB image with the Chrome OS partition layout
# Bumped to keep both Gnome and Xfce
#TODO: change back to 40 (2GB)
2020-11-11 06:15:03 +00:00
create_image $BASE $outdev 50M 60 $outmnt
2018-06-21 16:33:30 +00:00
2018-12-01 16:33:00 +00:00
# use default debootstrap mirror if none is specified
if [ " $PRAWNOS_DEBOOTSTRAP_MIRROR " = "" ]
then
PRAWNOS_DEBOOTSTRAP_MIRROR = http://ftp.us.debian.org/debian
fi
2018-06-21 16:33:30 +00:00
# install Debian on it
2018-06-28 00:46:28 +00:00
export DEBIAN_FRONTEND = noninteractive
2020-05-25 20:48:18 +00:00
# need ca-certs, gnupg, openssl to handle https apt links and key adding for deb.prawnos.com
2020-06-19 03:50:20 +00:00
printf -v debootstrap_debs_install_joined '%s,' " ${ debootstrap_debs_install [@] } "
2021-03-04 04:56:07 +00:00
debootstrap \
--arch $TARGET_ARCH \
--cache-dir= $PRAWNOS_BUILD /debootstrap-apt-cache/ \
2020-06-19 03:50:20 +00:00
--include ${ debootstrap_debs_install_joined %, } \
2020-06-30 03:39:56 +00:00
--keyring= $build_resources_apt /debian-archive-keyring.gpg \
2021-03-04 04:56:07 +00:00
$DEBIAN_SUITE \
2020-06-19 00:36:55 +00:00
$outmnt \
2021-03-04 04:56:07 +00:00
$PRAWNOS_DEBOOTSTRAP_MIRROR
2018-08-24 20:05:17 +00:00
2020-06-19 00:36:55 +00:00
chroot $outmnt passwd -d root
2021-01-17 22:41:06 +00:00
echo -n CrawfishOS > $outmnt /etc/hostname
2018-08-24 20:05:17 +00:00
2019-08-22 01:07:29 +00:00
#Setup the chroot for apt
2018-08-24 20:05:17 +00:00
#This is what https://wiki.debian.org/EmDebian/CrossDebootstrap suggests
cp /etc/hosts $outmnt /etc/
2020-06-30 03:39:56 +00:00
cp $build_resources_apt /sources.list $outmnt /etc/apt/sources.list
2020-07-02 02:23:09 +00:00
cp $build_resources_apt /prawnos.list $outmnt /etc/apt/sources.list.d/
2020-01-11 20:56:45 +00:00
sed -i -e " s/suite/ $DEBIAN_SUITE /g " $outmnt /etc/apt/sources.list
2020-06-28 22:44:59 +00:00
sed -i -e " s/suite/ $DEBIAN_SUITE /g " $outmnt /etc/apt/sources.list.d/prawnos.list
2020-01-11 20:56:45 +00:00
if [ " $DEBIAN_SUITE " != "sid" ]
2019-06-07 17:05:58 +00:00
then
2019-10-07 02:57:19 +00:00
# Install sid (unstable) as an additional source for bleeding edge packages.
2020-06-30 03:39:56 +00:00
cp $build_resources_apt /sid.list $outmnt /etc/apt/sources.list.d/
2019-06-07 17:05:58 +00:00
#setup apt pinning
2020-06-30 03:39:56 +00:00
cp $build_resources_apt /sid.pref $outmnt /etc/apt/preferences.d/
2019-06-07 17:05:58 +00:00
fi
2020-01-11 20:56:45 +00:00
if [ " $DEBIAN_SUITE " = "buster" ]
2019-06-07 17:05:58 +00:00
then
2020-10-21 19:16:58 +00:00
# sid and bullseye don't have backports or security; it's present for all other suites
cat $build_resources_apt /updates.list >> $outmnt /etc/apt/sources.list
sed -i -e " s/suite/ $DEBIAN_SUITE /g " $outmnt /etc/apt/sources.list
cp $build_resources_apt /backports.list $outmnt /etc/apt/sources.list.d/
sed -i -e " s/suite/ $DEBIAN_SUITE /g " $outmnt /etc/apt/sources.list.d/backports.list
cp $build_resources_apt /backports.pref $outmnt /etc/apt/preferences.d/
sed -i -e " s/suite/ $DEBIAN_SUITE /g " $outmnt /etc/apt/preferences.d/backports.pref
2019-10-07 02:57:19 +00:00
# Install bullseye (testing) as an additional source
2020-06-30 03:39:56 +00:00
cp $build_resources_apt /bullseye.list $outmnt /etc/apt/sources.list.d/
cp $build_resources_apt /bullseye.pref $outmnt /etc/apt/preferences.d/
2019-06-07 17:05:58 +00:00
fi
2018-08-24 20:05:17 +00:00
2020-12-03 22:25:08 +00:00
2020-12-04 00:08:59 +00:00
install_dir = $outmnt /etc/prawnos/install
install_dir_direct = /etc/prawnos/install
2020-12-03 22:25:08 +00:00
#Add the items required for installation
mkdir -p $install_dir
mkdir -p $install_dir /resources
mkdir -p $install_dir /scripts
## installation resources
2020-12-03 22:36:31 +00:00
cp $build_resources /logo/icons/ascii/* $install_dir /resources/
cp $build_resources /partmaps/* $install_dir /resources/
cp $build_resources_apt /deb.prawnos.com.gpg.key $install_dir /resources/
2020-12-03 22:25:08 +00:00
## installation scripts
cp scripts/InstallScripts/* $install_dir /scripts/
cp $PRAWNOS_SHARED_SCRIPTS /package_lists.sh $install_dir /scripts/
2020-12-04 00:08:59 +00:00
ln -s $install_dir_direct /scripts/InstallPrawnOS.sh $outmnt /bin/InstallPrawnOS
2020-12-03 22:25:08 +00:00
2021-01-17 22:41:06 +00:00
# Just in case:
ln -s $install_dir_direct /scripts/InstallPrawnOS.sh $outmnt /bin/InstallCrawfishOS
2021-03-30 07:40:16 +00:00
ln -s prawnos $outmnt /etc/crawfishos
2021-01-17 22:41:06 +00:00
2020-05-25 19:45:09 +00:00
#Bring in the deb.prawnos.com gpg keyring
2020-12-03 22:36:31 +00:00
chroot $outmnt apt-key add /etc/prawnos/install/resources/deb.prawnos.com.gpg.key
2020-05-25 19:45:09 +00:00
chroot $outmnt apt update
2018-08-24 20:05:17 +00:00
#Setup the locale
2019-01-10 19:43:03 +00:00
cp $build_resources /locale.gen $outmnt /etc/locale.gen
2018-10-02 02:01:08 +00:00
chroot $outmnt locale-gen
2018-08-24 20:05:17 +00:00
2020-06-30 03:39:56 +00:00
#Copy in the apt cache
2020-07-18 06:36:04 +00:00
cp " $PRAWNOS_BUILD /chroot-apt-cache/ " * " $outmnt /var/cache/apt/archives/ " || true
2020-06-30 03:39:56 +00:00
echo IMAGE SIZE
df -h
#Make apt retry on download failure
chroot $outmnt echo "APT::Acquire::Retries \"3\";" > /etc/apt/apt.conf.d/80-retries
2018-08-24 20:05:17 +00:00
#Install the base packages
2018-06-21 16:33:30 +00:00
chroot $outmnt apt update
2020-07-18 06:36:04 +00:00
apt_install $PRAWNOS_BUILD $outmnt true ${ base_debs_install [@] }
2019-09-18 01:50:27 +00:00
2019-05-23 05:19:32 +00:00
#add the live-boot fstab
cp -f $build_resources /external_fstab $outmnt /etc/fstab
2020-04-01 06:13:20 +00:00
chmod 644 $outmnt /etc/fstab
2019-05-23 05:19:32 +00:00
2018-08-24 20:05:17 +00:00
#Cleanup to reduce install size
2018-06-21 16:33:30 +00:00
chroot $outmnt apt-get autoremove --purge
chroot $outmnt apt-get clean
2018-08-24 20:05:17 +00:00
2020-11-28 21:24:24 +00:00
#Download the shared packages to be installed by InstallPackages.sh:
2020-12-22 09:52:24 +00:00
apt_install $PRAWNOS_BUILD $outmnt false ${ shared_desktop_debs_download [@] }
2019-01-10 19:43:03 +00:00
2020-11-11 06:15:03 +00:00
#DEs
2020-11-28 21:24:24 +00:00
#Download the xfce packages to be installed by InstallPackages.sh:
2020-07-18 06:36:04 +00:00
apt_install $PRAWNOS_BUILD $outmnt false ${ xfce_debs_download [@] }
2020-05-26 23:54:00 +00:00
2020-11-28 21:24:24 +00:00
#Download the gnome packages to be installed by InstallPackages.sh:
2020-10-27 04:09:54 +00:00
apt_install $PRAWNOS_BUILD $outmnt false ${ gnome_debs_download [@] }
2019-05-22 01:00:07 +00:00
2020-06-19 00:36:55 +00:00
# we want to include all of our built packages in the apt cache for installation later, but we want to let apt download dependencies
# if required
# this gets tricky when we build some of the dependencies. To avoid issues
# first, manually cache the deb
# apt install ./local-package.deb alone doesn't work because apt will resort to downloading it from deb.prawnos.com, which we dont want
# copy into /var/cache/apt/archives to place it in the cache
#next call apt install -d on the ./filename or on the package name and apt will recognize it already has the package cached, so will only cache the dependencies
#Copy the built prawnos debs over to the image, and update apts cache
2020-07-18 06:36:04 +00:00
cd $PRAWNOS_ROOT && make filesystem_packages_install TARGET = $TARGET_ARCH INSTALL_TARGET = $outmnt /var/cache/apt/archives/
2020-06-19 00:36:55 +00:00
chroot $outmnt apt install -y ${ prawnos_base_debs_prebuilt_install [@] }
chroot $outmnt apt install -y -d ${ prawnos_base_debs_prebuilt_download [@] }
chroot $outmnt apt install -y -d ${ prawnos_xfce_debs_prebuilt_download [@] }
2020-12-01 06:14:01 +00:00
chroot $outmnt apt install -y -d ${ prawnos_gnome_debs_prebuilt_download [@] }
2020-12-01 05:39:19 +00:00
if [ $TARGET_ARCH = "armhf" ]
then
chroot $outmnt apt install -y -d ${ prawnos_armhf_debs_prebuilt_download [@] }
fi
if [ $TARGET_ARCH = "arm64" ]
then
chroot $outmnt apt install -y -d ${ prawnos_arm64_debs_prebuilt_download [@] }
fi
2020-06-19 00:36:55 +00:00
2020-11-28 21:20:13 +00:00
## PrawnOS mesa packages
# PrawnOS only has specific mesa packages when the debian upstream versions are too old.
# commented out until we need it again
# chroot $outmnt apt install -y -d ${prawnos_mesa_prebuilt_install[@]}
2019-01-10 19:43:03 +00:00
2020-11-14 20:04:29 +00:00
#Setup console font size
cp -f $build_resources /console-font.sh $outmnt /etc/profile.d/console-font.sh
2018-08-24 20:05:17 +00:00
#Cleanup hosts
rm -rf $outmnt /etc/hosts #This is what https://wiki.debian.org/EmDebian/CrossDebootstrap suggests
2021-01-17 22:41:06 +00:00
echo -n "127.0.0.1 CrawfishOS" > $outmnt /etc/hosts
2018-06-28 00:46:28 +00:00
2020-06-30 03:39:56 +00:00
#Cleanup apt retry
chroot $outmnt rm -f /etc/apt/apt.conf.d/80-retries
2020-05-25 20:48:18 +00:00
# do a non-error cleanup
2018-09-13 01:38:20 +00:00
umount -l $outmnt > /dev/null 2>& 1
rmdir $outmnt > /dev/null 2>& 1
losetup -d $outdev > /dev/null 2>& 1
echo "DONE!"
trap - INT TERM EXIT