libdevuansdk/zlibs/helpers

332 lines
9.2 KiB
Plaintext
Raw Permalink Normal View History

2016-09-29 19:22:00 +00:00
#!/usr/bin/env zsh
# shellcheck shell=bash
2020-12-23 08:10:47 +00:00
# Copyright (c) 2016-2021 Ivan J. <parazyd@dyne.org>
2016-09-29 19:22:00 +00:00
# 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
2016-09-30 08:50:15 +00:00
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; }
2016-09-30 08:50:15 +00:00
}
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
2017-02-14 16:23:23 +00:00
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; }
2017-02-14 16:23:23 +00:00
}
clean_strapdir()
{
fn clean_strapdir
req=(strapdir)
2017-02-23 14:06:28 +00:00
ckreq || return 1
if [[ "$DEBUG" = 1 ]]; then
return
fi
2017-02-23 14:06:28 +00:00
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"
2017-02-23 14:06:28 +00:00
}
devprocsys()
{
fn devprocsys "$*"
2016-09-29 19:22:00 +00:00
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" && \
2016-09-29 20:59:37 +00:00
return 0
2016-09-29 19:22:00 +00:00
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"
2016-09-29 20:59:37 +00:00
return 0
2016-09-29 19:22:00 +00:00
fi
zerr; return 1
2017-05-03 12:04:59 +00:00
}
dpkgdivert()
{
2017-01-20 14:09:27 +00:00
fn dpkgdivert "$@"
req=(watdo werdo)
local watdo="$1"
local werdo="$2"
ckreq || return 1
if [[ $watdo = on ]]; then
2017-01-30 15:32:07 +00:00
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
2017-01-30 15:32:07 +00:00
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
2016-12-18 12:19:45 +00:00
# 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
2016-10-16 17:22:42 +00:00
_script="$(basename "$1")"
2016-10-16 17:22:42 +00:00
if [[ -n "$_divert" ]]; then
# NOTE: tread carefully, potential for recursive calling
devprocsys mount "$_path" || { zerr; return 1; }
dpkgdivert on "$_path" || { zerr; return 1; }
fi
2016-12-18 12:19:45 +00:00
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@'
2016-12-18 12:19:45 +00:00
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"
2017-05-21 17:11:10 +00:00
if [[ -n "$_divert" ]]; then
# NOTE: tread carefully, potential for recursive calling
dpkgdivert off "$_path" || { zerr; return 1; }
devprocsys umount "$_path" || { zerr; return 1; }
fi
2017-02-14 19:03:12 +00:00
sudo rm -rf "$_path/tmp/user"
sudo rm -f "$_path/$_script"
2016-10-16 17:22:42 +00:00
}
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
2017-02-22 13:09:20 +00:00
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
2017-02-22 13:09:20 +00:00
}
silly()
{
2016-09-29 19:22:00 +00:00
fn silly "$@"
local arg1="$1"
local arg2="$2"
# Cheers Mailpile!
funneh=(
"do not think of purple hippos"
2016-09-29 19:22:00 +00:00
"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"
2016-09-29 19:22:00 +00:00
"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"
2016-09-29 19:22:00 +00:00
)
local rnd=$(shuf -i1-$#funneh -n 1)
2017-10-26 19:27:58 +00:00
act "${funneh[$rnd]}"
[[ $arg1 = sleep ]] && sleep $arg2 || true
2016-09-29 19:22:00 +00:00
}
blend_preinst()
{
fn blend_preinst "(noop)"
}
blend_postinst()
{
fn blend_postinst "(noop)"
}