|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +socket="/tmp/virtiofs.sock" |
| 4 | + |
| 5 | +rootfs="" |
| 6 | +kernel="$rootfs/boot/vmlinuz" |
| 7 | +initram="$rootfs/boot/initrd.img" |
| 8 | + |
| 9 | +user="user" |
| 10 | +pass="pass" |
| 11 | +name="cloud" |
| 12 | +init="/sbin/init" |
| 13 | + |
| 14 | +fspid=0 |
| 15 | + |
| 16 | +fail() { |
| 17 | + echo "$1" >&2 |
| 18 | + exit 1 |
| 19 | +} |
| 20 | + |
| 21 | +usage() { |
| 22 | + echo "" |
| 23 | + echo "Usage: $0 [OPTIONS]" |
| 24 | + echo " -r, --rootfs Path to the root filesystem image (required)" |
| 25 | + echo " -u, --user Username for the system (optional)" |
| 26 | + echo " -p, --pass Password for the user (optional)" |
| 27 | + echo " -n, --name Hostname for the system (optional)" |
| 28 | + echo "" |
| 29 | + exit 1 |
| 30 | +} |
| 31 | + |
| 32 | +handle_options() { |
| 33 | + while [[ "$#" -gt 0 ]]; do |
| 34 | + case $1 in |
| 35 | + --rootfs) |
| 36 | + rootfs="$2" |
| 37 | + kernel="$rootfs/boot/vmlinuz" |
| 38 | + initram="$rootfs/boot/initrd.img" |
| 39 | + |
| 40 | + if [[ -z "$rootfs" ]]; then |
| 41 | + echo "Error: -r (rootfs) flag is required." |
| 42 | + usage |
| 43 | + fi |
| 44 | + shift |
| 45 | + ;; |
| 46 | + --user) |
| 47 | + user="$2" |
| 48 | + shift |
| 49 | + ;; |
| 50 | + --pass) |
| 51 | + pass="$2" |
| 52 | + shift |
| 53 | + ;; |
| 54 | + --name) |
| 55 | + name="$2" |
| 56 | + shift |
| 57 | + ;; |
| 58 | + --init) |
| 59 | + init="$2" |
| 60 | + shift |
| 61 | + ;; |
| 62 | + *) |
| 63 | + usage |
| 64 | + ;; |
| 65 | + esac |
| 66 | + shift |
| 67 | + done |
| 68 | + |
| 69 | +} |
| 70 | + |
| 71 | +validate() { |
| 72 | + which virtiofsd &>/dev/null || fail "virtiofsd not found in PATH" |
| 73 | + which cloud-hypervisor &>/dev/null || fail "cloud-hypervior not found in path" |
| 74 | + |
| 75 | + if [ ! -f "${kernel}" ]; then |
| 76 | + fail "kernel file not found" |
| 77 | + fi |
| 78 | + |
| 79 | + if [ ! -f "${initram}" ]; then |
| 80 | + fail "kernel file not found" |
| 81 | + fi |
| 82 | +} |
| 83 | + |
| 84 | +cidata="/tmp/cidata.img" |
| 85 | +create_cidata() { |
| 86 | + rm -f "${cidata}" |
| 87 | + mkdosfs -n CIDATA -C "${cidata}" 8192 |
| 88 | + |
| 89 | + cat >user-data <<EOF |
| 90 | +#cloud-config |
| 91 | +users: |
| 92 | + - name: $user |
| 93 | + plain_text_passwd: $pass |
| 94 | + shell: /bin/bash |
| 95 | + lock_passwd: false |
| 96 | + sudo: ALL=(ALL) NOPASSWD:ALL |
| 97 | +EOF |
| 98 | + mcopy -oi "${cidata}" -s user-data :: |
| 99 | + rm -f user-data |
| 100 | + |
| 101 | + cat >meta-data <<EOF |
| 102 | +instance-id: $name |
| 103 | +local-hostname: $name |
| 104 | +EOF |
| 105 | + mcopy -oi "${cidata}" -s meta-data :: |
| 106 | + rm -f meta-data |
| 107 | +} |
| 108 | + |
| 109 | +start_fs() { |
| 110 | + sudo virtiofsd \ |
| 111 | + --socket-path="${socket}" \ |
| 112 | + --shared-dir="${rootfs}" \ |
| 113 | + --cache=never \ |
| 114 | + & |
| 115 | + |
| 116 | + fspid=$! |
| 117 | +} |
| 118 | + |
| 119 | +cleanup() { |
| 120 | + kill "$fspid" &>/dev/null || true |
| 121 | +} |
| 122 | +trap cleanup EXIT |
| 123 | + |
| 124 | +run_hypervisor() { |
| 125 | + sudo cloud-hypervisor \ |
| 126 | + --cpus boot=1,max=1 \ |
| 127 | + --memory size=1024M,shared=on \ |
| 128 | + --kernel "${kernel}" \ |
| 129 | + --initramfs "${initram}" \ |
| 130 | + --fs tag=vroot,socket="${socket}" \ |
| 131 | + --disk path="${cidata}" \ |
| 132 | + --cmdline "rw console=ttyS0 reboot=k panic=1 root=vroot rootfstype=virtiofs rootdelay=30 init=${init}" \ |
| 133 | + --serial tty \ |
| 134 | + --console off |
| 135 | +} |
| 136 | + |
| 137 | +handle_options "$@" |
| 138 | +validate |
| 139 | +create_cidata |
| 140 | +start_fs |
| 141 | +run_hypervisor |
0 commit comments