helpers (9425B)
1 #!/usr/bin/env zsh 2 # shellcheck shell=bash 3 # Copyright (c) 2016-2021 Ivan J. <parazyd@dyne.org> 4 # This file is part of libdevuansdk 5 # 6 # This source code is free software: you can redistribute it and/or modify 7 # it under the terms of the GNU General Public License as published by 8 # the Free Software Foundation, either version 3 of the License, or 9 # (at your option) any later version. 10 # 11 # This software is distributed in the hope that it will be useful, 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 # GNU General Public License for more details. 15 # 16 # You should have received a copy of the GNU General Public License 17 # along with this source code. If not, see <http://www.gnu.org/licenses/>. 18 19 build_arm_dist() 20 { 21 fn build_arm_dist 22 req=(workdir strapdir os arch size parted_type) 23 case "$parted_type" in 24 gpt) req+=(gpt_boot gpt_root) ;; 25 dos) req+=(dos_boot dos_root) ;; 26 *) die "Unknown parted_type: $parted_type. Supported is gpt|dos." 27 zerr; return 1 28 ;; 29 esac 30 ckreq || return 1 31 32 notice "Building complete Arm image(s)" 33 34 bootstrap_complete_base || { zerr; return 1; } 35 blend_preinst || { zerr; return 1; } 36 image_prepare_raw || { zerr; return 1; } 37 image_connect_raw || { zerr; return 1; } 38 image_partition_${parted_type} || { zerr; return 1; } 39 image_format_partitions || { zerr; return 1; } 40 build_kernel_${arch} || { zerr; return 1; } 41 image_mount || { zerr; return 1; } 42 strapdir_to_image || { zerr; return 1; } 43 blend_postinst || { zerr; return 1; } 44 image_umount || { zerr; return 1; } 45 image_disconnect_raw || { zerr; return 1; } 46 image_pack_dist || { zerr; return 1; } 47 clean_strapdir || { zerr; return 1; } 48 } 49 50 build_iso_dist() 51 { 52 fn build_iso_dist 53 req=(workdir strapdir os arch) 54 ckreq || return 1 55 56 notice "Building complete iso image(s)" 57 58 bootstrap_complete_base || { zerr; return 1; } 59 blend_preinst || { zerr; return 1; } 60 iso_prepare_strap || { zerr; return 1; } 61 iso_setup_isolinux || { zerr; return 1; } 62 iso_write_isolinux_cfg || { zerr; return 1; } 63 blend_postinst || { zerr; return 1; } 64 iso_squash_strap || { zerr; return 1; } 65 iso_xorriso_build || { zerr; return 1; } 66 } 67 68 build_vm_dist() 69 { 70 fn build_vm_dist 71 req=(workdir strapdir os arch size imageformat parted_type) 72 case "$parted_type" in 73 gpt) req+=(gpt_boot gpt_root) ;; 74 dos) req+=(dos_boot dos_root) ;; 75 *) die "Unknown parted_type: $parted_type. Supported is gpt|dos." 76 zerr; return 1 77 ;; 78 esac 79 ckreq || return 1 80 81 notice "Building complete VM image(s)" 82 83 bootstrap_complete_base || { zerr; return 1; } 84 blend_preinst || { zerr; return 1; } 85 image_prepare_raw || { zerr; return 1; } 86 image_connect_raw || { zerr; return 1; } 87 image_partition_${parted_type} || { zerr; return 1; } 88 image_format_partitions || { zerr; return 1; } 89 image_mount || { zerr; return 1; } 90 vm_inject_overrides || { zerr; return 1; } 91 strapdir_to_image || { zerr; return 1; } 92 vm_setup_grub || { zerr; return 1; } 93 blend_postinst || { zerr; return 1; } 94 image_umount || { zerr; return 1; } 95 image_disconnect_raw || { zerr; return 1; } 96 if [[ "$imageformat" = qcow2 ]]; then 97 image_raw_to_qcow2 || { zerr; return 1; } 98 fi 99 image_raw_to_vdi || { zerr; return 1; } 100 vm_pack_dist || { zerr; return 1; } 101 clean_strapdir || { zerr; return 1; } 102 } 103 104 clean_strapdir() 105 { 106 fn clean_strapdir 107 req=(strapdir) 108 ckreq || return 1 109 110 if [[ "$DEBUG" = 1 ]]; then 111 return 112 fi 113 114 notice "Cleaning strapdir" 115 116 # Danger Will Robinson 117 # Check for (bind) mounts as sudo rm -rf will trash the host 118 for m in sys proc dev; do 119 if [[ $(mountpoint -q "${strapdir}/$m") ]]; then 120 zerr 121 return 1 122 fi 123 done 124 sudo rm -rf "$strapdir" 125 } 126 127 devprocsys() 128 { 129 fn devprocsys "$*" 130 local watdo="$1" 131 local werdo="$2" 132 req=(watdo werdo) 133 ckreq || return 1 134 135 if [[ $watdo = mount ]]; then 136 sudo mount -t proc proc,ro $werdo/proc && act "mounted proc" && \ 137 sudo mount -o bind /sys $werdo/sys && \ 138 sudo mount -o remount,bind,ro /sys $werdo/sys && act "mounted sys" && \ 139 sudo mount -o bind /dev $werdo/dev && \ 140 sudo mount -o remount,bind,ro /dev $werdo/dev && act "mounted dev" && \ 141 sudo mount -o bind /dev/pts $werdo/dev/pts && \ 142 sudo mount -o remount,bind,ro /dev/pts $werdo/dev/pts && act "mounted devpts" && \ 143 return 0 144 elif [[ $watdo = umount ]]; then 145 sudo umount $werdo/dev/pts && act "umounted devpts" 146 sudo umount $werdo/dev && act "umounted dev" 147 sudo umount $werdo/proc && act "umounted proc" 148 sudo umount $werdo/sys && act "umounted sys" 149 return 0 150 fi 151 zerr; return 1 152 } 153 154 dpkgdivert() 155 { 156 fn dpkgdivert "$@" 157 req=(watdo werdo) 158 local watdo="$1" 159 local werdo="$2" 160 ckreq || return 1 161 162 if [[ $watdo = on ]]; then 163 cat <<EOF | sudo tee ${werdo}/dpkgdivert >/dev/null 164 #!/bin/sh 165 dpkg-divert --add --local \ 166 --divert /usr/sbin/invoke-rc.d.chroot \ 167 --rename /usr/sbin/invoke-rc.d 168 cp /bin/true /usr/sbin/invoke-rc.d 169 echo -e "#!/bin/sh\nexit 101" > /usr/sbin/policy-rc.d 170 chmod +x /usr/sbin/policy-rc.d 171 EOF 172 elif [[ $watdo = off ]]; then 173 cat <<EOF | sudo tee ${werdo}/dpkgdivert >/dev/null 174 #!/bin/sh 175 rm -f /usr/sbin/policy-rc.d 176 rm -f /usr/sbin/invoke-rc.d 177 dpkg-divert --remove --rename /usr/sbin/invoke-rc.d 178 EOF 179 fi 180 181 # NOTE: tread carefully, potential for recursive calling 182 chroot-script "$werdo/dpkgdivert" || { zerr; return 1; } 183 } 184 185 chroot-script() 186 { 187 fn chroot-script "$*" 188 req=(R workdir strapdir) 189 ckreq || return 1 190 191 mkdir -p "$R/log" 192 193 local _divert="" 194 local _path="" 195 local _script="" 196 197 case "x$1" in 198 x-d) 199 _divert=1 200 shift 201 ;; 202 esac 203 204 if [[ "$(dirname "$1")" = "." ]]; then 205 _path="$strapdir" 206 else 207 _path="$(dirname "$1")" 208 fi 209 210 _script="$(basename "$1")" 211 212 if [[ -n "$_divert" ]]; then 213 # NOTE: tread carefully, potential for recursive calling 214 devprocsys mount "$_path" || { zerr; return 1; } 215 dpkgdivert on "$_path" || { zerr; return 1; } 216 fi 217 218 sudo sed -i "$_path/$_script" \ 219 -e 's@^#!/bin/sh@&\nexport DEBIAN_FRONTEND=noninteractive@' \ 220 -e 's@^#!/bin/sh@&\nexport SOURCE_DATE_EPOCH=1610550434@' \ 221 -e 's@^#!/bin/sh@&\nexport LC_ALL=C@' \ 222 -e 's@^#!/bin/sh@&\nexport LANG=C@' \ 223 -e 's@^#!/bin/sh@&\nset -x ; exec 2>/'$_script'.log@' 224 225 notice "Chrooting to execute '$_script' ..." 226 sudo chmod +x "$_path/$_script" || { zerr; return 1; } 227 sudo chroot "$_path" "/$_script" || { zerr; return 1; } 228 sudo mv -f "$_path/${_script}.log" "$R/log/" 229 # Some dpkg Debian scripts now expect "/tmp/user/0" 230 sudo mkdir -p "${_path}/tmp/user/0" 231 sudo chmod 0700 "${_path}/tmp/user/0" 232 sudo chmod 0600 "${_path}/tmp/user" 233 sudo chmod 1777 "${_path}/tmp" 234 235 if [[ -n "$_divert" ]]; then 236 # NOTE: tread carefully, potential for recursive calling 237 dpkgdivert off "$_path" || { zerr; return 1; } 238 devprocsys umount "$_path" || { zerr; return 1; } 239 fi 240 241 sudo rm -rf "$_path/tmp/user" 242 sudo rm -f "$_path/$_script" 243 } 244 245 findloopdev() 246 { 247 fn findloopdev 248 req=(workdir image_name) 249 ckreq || return 1 250 251 local _l="$(sudo losetup -f --show "$workdir/${image_name}.img")" 252 if [[ -z "$_l" ]]; then 253 zerr; return 1 254 fi 255 256 echo "$_l" 257 } 258 259 findnbddev() 260 { 261 fn findnbddev 262 263 notice "Finding a free /dev/nbd device" 264 265 for i in $(seq 0 8); do 266 grep -q "^/dev/nbd${i}" /proc/mounts || { 267 echo "/dev/nbd${i}" 268 return 269 } 270 done 271 272 zerr; return 1 273 } 274 275 silly() 276 { 277 fn silly "$@" 278 local arg1="$1" 279 local arg2="$2" 280 # Cheers Mailpile! 281 funneh=( 282 "do not think of purple hippos" 283 "increasing entropy & scrambling bits" 284 "indexing kittens..." 285 "patching bugs..." 286 "spinning violently around the y-axis" 287 "warming up particle accelerator" 288 "this server is powered by a lemon and two electrodes" 289 "becoming self-aware" 290 "BCC-ing ALL THE SPIES!" 291 "all of your config settings & passwords are encrypted with AES256" 292 "the most common password is 123456, hopefully yours is different" 293 "good things come to those who wait" 294 "Make Free Software and be happy" 295 "We like volcanos, do you like volcanos?" 296 "Crossing out swear words..." 297 "Informing David Cameron of suspicious ac^H^H^H ... naaah :)" 298 "Abandon all hope, ye who enter here" 299 "Compiling bullshit bingo grid..." 300 "Estimating chance of asteroid hitting Earth" 301 "Applying coupons..." 302 "Backing up the entire Internet..." 303 "Making you wait for no reason" 304 "Doing nothing" 305 "Pay no attention to the man behind the curtain" 306 "You are great just the way you are" 307 "Supplying monkeys with typewriters" 308 "Swapping time and space" 309 "Self potato" 310 "God is porco" 311 "A million hamsters are spinning their wheels right now" 312 "Launching global internet Denial Of Service, googling 'Google'" 313 "These are not the Devs you are looking for" 314 "herding cats ..." 315 "Illegitimi Non Carborundum" 316 "GNU Terry Pratchett" 317 ) 318 local rnd=$(shuf -i1-$#funneh -n 1) 319 act "${funneh[$rnd]}" 320 [[ $arg1 = sleep ]] && sleep $arg2 || true 321 } 322 323 blend_preinst() 324 { 325 fn blend_preinst "(noop)" 326 } 327 328 blend_postinst() 329 { 330 fn blend_postinst "(noop)" 331 }