libdevuansdk/zlibs/helpers
2021-04-12 17:22:04 +01:00

332 lines
9.2 KiB
Bash

#!/usr/bin/env zsh
# shellcheck shell=bash
# Copyright (c) 2016-2021 Ivan J. <parazyd@dyne.org>
# This file is part of libdevuansdk
#
# This source code 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 software 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 source code. If not, see <http://www.gnu.org/licenses/>.
build_arm_dist()
{
fn build_arm_dist
req=(workdir strapdir os arch size parted_type)
case "$parted_type" in
gpt) req+=(gpt_boot gpt_root) ;;
dos) req+=(dos_boot dos_root) ;;
*) die "Unknown parted_type: $parted_type. Supported is gpt|dos."
zerr; return 1
;;
esac
ckreq || return 1
notice "Building complete Arm image(s)"
bootstrap_complete_base || { zerr; return 1; }
blend_preinst || { zerr; return 1; }
image_prepare_raw || { zerr; return 1; }
image_connect_raw || { zerr; return 1; }
image_partition_${parted_type} || { zerr; return 1; }
image_format_partitions || { zerr; return 1; }
build_kernel_${arch} || { zerr; return 1; }
image_mount || { zerr; return 1; }
strapdir_to_image || { zerr; return 1; }
blend_postinst || { zerr; return 1; }
image_umount || { zerr; return 1; }
image_disconnect_raw || { zerr; return 1; }
image_pack_dist || { zerr; return 1; }
clean_strapdir || { zerr; return 1; }
}
build_iso_dist()
{
fn build_iso_dist
req=(workdir strapdir os arch)
ckreq || return 1
notice "Building complete iso image(s)"
bootstrap_complete_base || { zerr; return 1; }
blend_preinst || { zerr; return 1; }
iso_prepare_strap || { zerr; return 1; }
iso_setup_isolinux || { zerr; return 1; }
iso_write_isolinux_cfg || { zerr; return 1; }
blend_postinst || { zerr; return 1; }
iso_squash_strap || { zerr; return 1; }
iso_xorriso_build || { zerr; return 1; }
}
build_vm_dist()
{
fn build_vm_dist
req=(workdir strapdir os arch size imageformat parted_type)
case "$parted_type" in
gpt) req+=(gpt_boot gpt_root) ;;
dos) req+=(dos_boot dos_root) ;;
*) die "Unknown parted_type: $parted_type. Supported is gpt|dos."
zerr; return 1
;;
esac
ckreq || return 1
notice "Building complete VM image(s)"
bootstrap_complete_base || { zerr; return 1; }
blend_preinst || { zerr; return 1; }
image_prepare_raw || { zerr; return 1; }
image_connect_raw || { zerr; return 1; }
image_partition_${parted_type} || { zerr; return 1; }
image_format_partitions || { zerr; return 1; }
image_mount || { zerr; return 1; }
vm_inject_overrides || { zerr; return 1; }
strapdir_to_image || { zerr; return 1; }
vm_setup_grub || { zerr; return 1; }
blend_postinst || { zerr; return 1; }
image_umount || { zerr; return 1; }
image_disconnect_raw || { zerr; return 1; }
if [[ "$imageformat" = qcow2 ]]; then
image_raw_to_qcow2 || { zerr; return 1; }
fi
image_raw_to_vdi || { zerr; return 1; }
vm_pack_dist || { zerr; return 1; }
clean_strapdir || { zerr; return 1; }
}
clean_strapdir()
{
fn clean_strapdir
req=(strapdir)
ckreq || return 1
if [[ "$DEBUG" = 1 ]]; then
return
fi
notice "Cleaning strapdir"
# Danger Will Robinson
# Check for (bind) mounts as sudo rm -rf will trash the host
for m in sys proc dev; do
if [[ $(mountpoint -q "${strapdir}/$m") ]]; then
zerr
return 1
fi
done
sudo rm -rf "$strapdir"
}
devprocsys()
{
fn devprocsys "$*"
local watdo="$1"
local werdo="$2"
req=(watdo werdo)
ckreq || return 1
if [[ $watdo = mount ]]; then
sudo mount -t proc proc,ro $werdo/proc && act "mounted proc" && \
sudo mount -o bind /sys $werdo/sys && \
sudo mount -o remount,bind,ro /sys $werdo/sys && act "mounted sys" && \
sudo mount -o bind /dev $werdo/dev && \
sudo mount -o remount,bind,ro /dev $werdo/dev && act "mounted dev" && \
sudo mount -o bind /dev/pts $werdo/dev/pts && \
sudo mount -o remount,bind,ro /dev/pts $werdo/dev/pts && act "mounted devpts" && \
return 0
elif [[ $watdo = umount ]]; then
sudo umount $werdo/dev/pts && act "umounted devpts"
sudo umount $werdo/dev && act "umounted dev"
sudo umount $werdo/proc && act "umounted proc"
sudo umount $werdo/sys && act "umounted sys"
return 0
fi
zerr; return 1
}
dpkgdivert()
{
fn dpkgdivert "$@"
req=(watdo werdo)
local watdo="$1"
local werdo="$2"
ckreq || return 1
if [[ $watdo = on ]]; then
cat <<EOF | sudo tee ${werdo}/dpkgdivert >/dev/null
#!/bin/sh
dpkg-divert --add --local \
--divert /usr/sbin/invoke-rc.d.chroot \
--rename /usr/sbin/invoke-rc.d
cp /bin/true /usr/sbin/invoke-rc.d
echo -e "#!/bin/sh\nexit 101" > /usr/sbin/policy-rc.d
chmod +x /usr/sbin/policy-rc.d
EOF
elif [[ $watdo = off ]]; then
cat <<EOF | sudo tee ${werdo}/dpkgdivert >/dev/null
#!/bin/sh
rm -f /usr/sbin/policy-rc.d
rm -f /usr/sbin/invoke-rc.d
dpkg-divert --remove --rename /usr/sbin/invoke-rc.d
EOF
fi
# NOTE: tread carefully, potential for recursive calling
chroot-script "$werdo/dpkgdivert" || { zerr; return 1; }
}
chroot-script()
{
fn chroot-script "$*"
req=(R workdir strapdir)
ckreq || return 1
mkdir -p "$R/log"
local _divert=""
local _path=""
local _script=""
case "x$1" in
x-d)
_divert=1
shift
;;
esac
if [[ "$(dirname "$1")" = "." ]]; then
_path="$strapdir"
else
_path="$(dirname "$1")"
fi
_script="$(basename "$1")"
if [[ -n "$_divert" ]]; then
# NOTE: tread carefully, potential for recursive calling
devprocsys mount "$_path" || { zerr; return 1; }
dpkgdivert on "$_path" || { zerr; return 1; }
fi
sudo sed -i "$_path/$_script" \
-e 's@^#!/bin/sh@&\nexport DEBIAN_FRONTEND=noninteractive@' \
-e 's@^#!/bin/sh@&\nexport SOURCE_DATE_EPOCH=1610550434@' \
-e 's@^#!/bin/sh@&\nexport LC_ALL=C@' \
-e 's@^#!/bin/sh@&\nexport LANG=C@' \
-e 's@^#!/bin/sh@&\nset -x ; exec 2>/'$_script'.log@'
notice "Chrooting to execute '$_script' ..."
sudo chmod +x "$_path/$_script" || { zerr; return 1; }
sudo chroot "$_path" "/$_script" || { zerr; return 1; }
sudo mv -f "$_path/${_script}.log" "$R/log/"
# Some dpkg Debian scripts now expect "/tmp/user/0"
sudo mkdir -p "${_path}/tmp/user/0"
sudo chmod 0700 "${_path}/tmp/user/0"
sudo chmod 0600 "${_path}/tmp/user"
sudo chmod 1777 "${_path}/tmp"
if [[ -n "$_divert" ]]; then
# NOTE: tread carefully, potential for recursive calling
dpkgdivert off "$_path" || { zerr; return 1; }
devprocsys umount "$_path" || { zerr; return 1; }
fi
sudo rm -rf "$_path/tmp/user"
sudo rm -f "$_path/$_script"
}
findloopdev()
{
fn findloopdev
req=(workdir image_name)
ckreq || return 1
local _l="$(sudo losetup -f --show "$workdir/${image_name}.img")"
if [[ -z "$_l" ]]; then
zerr; return 1
fi
echo "$_l"
}
findnbddev()
{
fn findnbddev
notice "Finding a free /dev/nbd device"
for i in $(seq 0 8); do
grep -q "^/dev/nbd${i}" /proc/mounts || {
echo "/dev/nbd${i}"
return
}
done
zerr; return 1
}
silly()
{
fn silly "$@"
local arg1="$1"
local arg2="$2"
# Cheers Mailpile!
funneh=(
"do not think of purple hippos"
"increasing entropy & scrambling bits"
"indexing kittens..."
"patching bugs..."
"spinning violently around the y-axis"
"warming up particle accelerator"
"this server is powered by a lemon and two electrodes"
"becoming self-aware"
"BCC-ing ALL THE SPIES!"
"all of your config settings & passwords are encrypted with AES256"
"the most common password is 123456, hopefully yours is different"
"good things come to those who wait"
"Make Free Software and be happy"
"We like volcanos, do you like volcanos?"
"Crossing out swear words..."
"Informing David Cameron of suspicious ac^H^H^H ... naaah :)"
"Abandon all hope, ye who enter here"
"Compiling bullshit bingo grid..."
"Estimating chance of asteroid hitting Earth"
"Applying coupons..."
"Backing up the entire Internet..."
"Making you wait for no reason"
"Doing nothing"
"Pay no attention to the man behind the curtain"
"You are great just the way you are"
"Supplying monkeys with typewriters"
"Swapping time and space"
"Self potato"
"God is porco"
"A million hamsters are spinning their wheels right now"
"Launching global internet Denial Of Service, googling 'Google'"
"These are not the Devs you are looking for"
"herding cats ..."
"Illegitimi Non Carborundum"
"GNU Terry Pratchett"
)
local rnd=$(shuf -i1-$#funneh -n 1)
act "${funneh[$rnd]}"
[[ $arg1 = sleep ]] && sleep $arg2 || true
}
blend_preinst()
{
fn blend_preinst "(noop)"
}
blend_postinst()
{
fn blend_postinst "(noop)"
}