libdevuansdk

common library for devuan's simple distro kits
git clone https://git.parazyd.org/libdevuansdk
Log | Files | Refs | Submodules | README | LICENSE

vm (2964B)


      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 vars+=(vmname)
     20 
     21 vm_inject_overrides()
     22 {
     23 	fn vm_inject_overrides
     24 	req=(strapdir bootfs rootfs bootpart rootpart)
     25 	ckreq || return 1
     26 
     27 	notice "Injecting rootfs overrides"
     28 
     29 	bootuuid="$(lsblk "$bootpart" -no UUID)"
     30 	rootuuid="$(lsblk "$rootpart" -no UUID)"
     31 
     32 	cat <<EOF | sudo tee -a "$strapdir/etc/fstab" >/dev/null
     33 UUID=${rootuuid} /     ${rootfs} defaults 0 1
     34 UUID=${bootuuid} /boot ${bootfs} defaults 0 1
     35 EOF
     36 
     37 	cat <<EOF | sudo tee "$strapdir/etc/rc.local" >/dev/null
     38 #!/bin/sh
     39 # rc.local for base images
     40 
     41 [ -f /etc/ssh/ssh_host_rsa_key.pub ] || ssh-keygen -A
     42 
     43 exit 0
     44 EOF
     45 	sudo chmod +x "$strapdir/etc/rc.local"
     46 
     47 	sudo sed -i "$strapdir/etc/ssh/sshd_config" \
     48 		-e 's/#PermitRootLogin .*/PermitRootLogin yes/' \
     49 		-e 's/PermitRootLogin .*/PermitRootLogin yes/' || { zerr; return 1; }
     50 }
     51 
     52 vm_setup_grub()
     53 {
     54 	fn vm_setup_grub
     55 	req=(workdir loopdevice bootfs)
     56 	ckreq || return 1
     57 
     58 	notice "Setting up grub"
     59 
     60 	cat <<EOF | sudo tee "$workdir/mnt/setupgrub" >/dev/null
     61 #!/bin/sh
     62 grub-install "${loopdevice}" || exit 1
     63 grub-mkconfig -o /boot/grub/grub.cfg || exit 1
     64 EOF
     65 	chroot-script -d "$workdir/mnt/setupgrub" || { zerr; return 1; }
     66 }
     67 
     68 vm_pack_dist()
     69 {
     70 	fn vm_pack_dist
     71 	req=(R workdir image_name imageformat)
     72 	ckreq || return 1
     73 
     74 	notice "Packing up built images"
     75 
     76 	local _xzcomp=""
     77 	local _rsuffix="${imageformat}"
     78 	local _vsuffix="vdi"
     79 
     80 	if [[ -n "$COMPRESS_IMAGE" ]]; then
     81 		if command -v pixz >/dev/null; then
     82 			_xzcomp="$(command -v pixz)"
     83 		else
     84 			_xzcomp="$(command -v xz)"
     85 		fi
     86 		_rsuffix="${imageformat}.xz"
     87 		_vsuffix="vdi.xz"
     88 	fi
     89 
     90 	pushd "$workdir" || { zerr; return 1; }
     91 
     92 	if [[ -n "$COMPRESS_IMAGE" ]]; then
     93 		act "Compressing images with $_xzcomp"
     94 		silly
     95 		$_xzcomp "${image_name}.${imageformat}" || { zerr; return 1; }
     96 		$_xzcomp "${image_name}.vdi" || { zerr; return 1; }
     97 	fi
     98 
     99 	if [[ "$imageformat" != raw ]]; then
    100 		rm -f "${image_name}.img"
    101 	fi
    102 
    103 	act "Calculating sha256 checksums"
    104 	silly
    105 	sha256sum "${image_name}.${_rsuffix}" > "${image_name}.${_rsuffix}.sha256"
    106 	sha256sum "${image_name}.${_vsuffix}" > "${image_name}.${_vsuffix}.sha256"
    107 
    108 	mkdir -p "$R/dist"
    109 	mv -v "${image_name}".* "$R/dist" || { zerr; return 1; }
    110 
    111 	notice "Done! Thanks for being patient!"
    112 
    113 	popd
    114 }