Added build and startup scripts

This commit is contained in:
Mattia Giambirtone 2022-03-12 15:53:31 +01:00
parent d240d05cef
commit df1371c13f
4 changed files with 123 additions and 3 deletions

3
.gitignore vendored
View File

@ -5,7 +5,6 @@ main
*.iso
nimd
main
boot.sh
rootfs
packervm
test
@ -14,7 +13,5 @@ initrd*
vmlinuz-linux
vmlinuz*
debian*
start.sh
rebuild.sh
vm.qcow2
*.tar.*

115
scripts/boot.sh Executable file
View File

@ -0,0 +1,115 @@
#!/usr/bin/env bash
set -Eeuo pipefail
trap cleanup SIGINT SIGTERM ERR EXIT
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
usage() {
cat << EOF
Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-v] [-m] [-b] -k kernel -i initrd -r rootfs
Alpinemini vm
Available options:
-h, --help Print this help and exit
-v, --verbose Print script debug info
-k, --kernel Specify kernel file
-i, --initrd Specify initrd file
-m, --memory Set maximum vm memory
-b, --build Build a new disk (and then boot)
EOF
exit
}
cleanup() {
trap - SIGINT SIGTERM ERR EXIT
}
setup_colors() {
if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then
NOFORMAT='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' ORANGE='\033[0;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' YELLOW='\033[1;33m'
else
NOFORMAT='' RED='' GREEN='' ORANGE='' BLUE='' PURPLE='' CYAN='' YELLOW=''
fi
}
msg() {
echo >&2 -e "${1-}"
}
die() {
local msg=$1
local code=${2-1}
msg "$msg"
exit "$code"
}
parse_params() {
build=0
rootfs=''
kernel=''
initrd=''
memory=''
while :; do
case "${1-}" in
-h | --help) usage ;;
-v | --verbose) set -x ;;
--no-color) NO_COLOR=1 ;;
-b | --build) build=1 ;; # build disk
-k | --kernel) # kernel file
kernel="${2-}"
shift
;;
-i | --initrd) # initrd file
initrd="${2-}"
shift
;;
-m | --memory) # max memory
memory="${2-}"
shift
;;
-?*) die "Unknown option: $1" ;;
*) break ;;
esac
shift
done
args=("$@")
# check required params and arguments
[[ -z "${kernel-}" ]] && die "${RED}Missing required parameter:${NOFORMAT} kernel"
[[ -z "${initrd-}" ]] && die "${RED}Missing required parameter:${NOFORMAT} initrd"
return 0
}
setup_colors
parse_params "$@"
if ! [ -x "$(command -v qemu-system-x86_64)" ]; then
echo '${RED}Error:${NOFORMAT} unable to find qemu-system-x86_64, please install it first.' >&2
exit 1
fi
if ! [ -x "$(command -v qemu-img)" ]; then
echo '${RED}Error:${NOFORMAT} unable to find qemu-img, please install it first.' >&2
exit 1
fi
if [[ $build -eq 1 ]]
then
msg "${CYAN}Building disk... Please wait${NOFORMAT}"
qemu-img create -f qcow2 vm.qcow2 800M
qemu-system-x86_64 -m 256M -smp 1 -drive file=packervm/packer.qcow2,if=virtio,readonly=on -drive file=vm.qcow2,if=virtio -enable-kvm -fsdev local,id=rootfs_dev,path=rootfs,security_model=none -device virtio-9p-pci,fsdev=rootfs_dev,mount_tag=rootfs -display none
fi
qemumem=''
if ! [[ $memory -eq "" ]]
then
qemumem="-m ${memory}"
fi
qemu-system-x86_64 -net nic,model=virtio,netdev=user.0 -netdev user,id=user.0 -drive file=vm.qcow2,if=virtio -enable-kvm -kernel ${kernel} -initrd ${initrd} ${qemumem} -append 'root=/dev/vda1 rw quiet modules=ext4 console=ttyS0,9600 console=ttyS0 init=/bin/nimd'

7
scripts/rebuild.sh Executable file
View File

@ -0,0 +1,7 @@
nim -o:rootfs/bin/nimd -d:release --gc:orc --opt:size --passL:"-static" compile src/main.nim
nim -o:rootfs/bin/nimdown -d:release --gc:orc --opt:size --passL:"-static" compile src/programs/poweroff.nim
nim -o:rootfs/bin/nimhalt -d:release --gc:orc --opt:size --passL:"-static" compile src/programs/halt.nim
nim -o:rootfs/bin/nimreboot -d:release --gc:orc --opt:size --passL:"-static" compile src/programs/reboot.nim
mkdir -p rootfs/etc/nimd
cp nimd.conf rootfs/etc/nimd/nimd.conf
./boot.sh --kernel vmlinuz-linux --initrd initrd-linux.img --memory 1G --build

1
scripts/start.sh Executable file
View File

@ -0,0 +1 @@
./boot.sh --kernel vmlinuz-linux --initrd initrd-linux.img --memory 1G