diff --git a/.gitignore b/.gitignore index bb3dc13..3c841b7 100644 --- a/.gitignore +++ b/.gitignore @@ -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.* diff --git a/scripts/boot.sh b/scripts/boot.sh new file mode 100755 index 0000000..6c9064c --- /dev/null +++ b/scripts/boot.sh @@ -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' diff --git a/scripts/rebuild.sh b/scripts/rebuild.sh new file mode 100755 index 0000000..61b6012 --- /dev/null +++ b/scripts/rebuild.sh @@ -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 diff --git a/scripts/start.sh b/scripts/start.sh new file mode 100755 index 0000000..5372785 --- /dev/null +++ b/scripts/start.sh @@ -0,0 +1 @@ +./boot.sh --kernel vmlinuz-linux --initrd initrd-linux.img --memory 1G