11#! /bin/bash
22
3- set -euo pipefail
4-
5- # constants
3+ # Constants
64readonly SOCKET=" /tmp/virtiofs.sock"
75readonly OVERLAYFS=" /tmp/merged"
86
9- # defaults
7+ # Defaults
108cidata=" /tmp/cidata.img"
11- user=" user"
12- pass=" pass"
13- name=" cloud"
149cmdline=" rw console=ttyS0 reboot=k panic=1 root=vroot rootfstype=virtiofs rootdelay=30"
1510
16- # globals
11+ # Globals
12+ declare -A options=(
13+ [--debug]=" false"
14+ [--cidata]=" "
15+ [--kernel]=" "
16+ [--initramfs]=" "
17+ [--init]=" "
18+ [--user]=" user"
19+ [--pass]=" pass"
20+ [--name]=" cloud"
21+ )
1722init=" "
1823kernel=" "
1924initramfs=" "
2025fspid=0
2126flpid=0
2227
28+ # Functions
2329fail () {
2430 echo " Error: $* " >&2
2531 exit 1
@@ -42,8 +48,11 @@ handle_options() {
4248 ;;
4349 esac
4450 done
45- }
4651
52+ if [[ " ${options[--debug]} " == true ]]; then
53+ set -x
54+ fi
55+ }
4756
4857validate () {
4958 for tool in virtiofsd cloud-hypervisor rfs1; do
@@ -60,42 +69,33 @@ validate() {
6069}
6170
6271create_cidata () {
63- sudo chroot " ${OVERLAYFS} " cloud-init clean
64-
6572 if [[ -f " ${options[--cidata]} " ]]; then
6673 cidata=" ${options[--cidata]} "
6774 return
6875 fi
6976
70- [ ! -z " ${options[--user]} " ] && user=" ${options[--user]} "
71- [ ! -z " ${options[--pass]} " ] && pass=" ${options[--pass]} "
72- [ ! -z " ${options[--name]} " ] && name=" ${options[--name]} "
73-
74- echo " Creating cidata"
77+ local user=" ${options[--user]} "
78+ local pass=" ${options[--pass]} "
79+ local name=" ${options[--name]} "
80+
81+ echo ' #cloud-config' > user-data
82+ echo ' users:' >> user-data
83+ echo " - name: $user " >> user-data
84+ echo " plain_text_passwd: $pass " >> user-data
85+ echo " shell: /bin/bash" >> user-data
86+ echo " lock_passwd: false" >> user-data
87+ echo " sudo: ALL=(ALL) NOPASSWD:ALL" >> user-data
88+
89+ echo " instance-id: $name " > meta-data
90+ echo " local-hostname: $name " >> meta-data
7591
7692 rm -f " ${cidata} "
7793 mkdosfs -n CIDATA -C " ${cidata} " 8192
78-
79- cat > user-data << EOF
80- #cloud-config
81- users:
82- - name: $user
83- plain_text_passwd: $pass
84- shell: /bin/bash
85- lock_passwd: false
86- sudo: ALL=(ALL) NOPASSWD:ALL
87- EOF
8894 mcopy -oi " ${cidata} " -s user-data ::
89- rm -f user-data
90-
91- cat > meta-data << EOF
92- instance-id: $name
93- local-hostname: $name
94- EOF
9595 mcopy -oi " ${cidata} " -s meta-data ::
96- rm -f meta-data
97- }
9896
97+ rm -f user-data meta-data
98+ }
9999
100100cleanup () {
101101 local pids=( " $flpid " " $fspid " )
@@ -130,11 +130,11 @@ prepare_rootfs() {
130130 echo " ${image_path} is flist, mounting"
131131
132132 rootfs=/tmp/lower
133- sudo mkdir -p " $rootfs " && sudo chmod 777 " $rootfs "
133+ sudo mkdir -p " $rootfs " && sudo chmod 777 " $rootfs "
134134
135135 rfs1 --meta " $image_path " " $rootfs " &
136136 flpid=$!
137- else
137+ else
138138 rootfs=" ${image_path} "
139139 fi
140140
@@ -166,45 +166,26 @@ prepare_boot() {
166166 cmdline=" $cmdline init=$init "
167167 fi
168168
169- if [ ! -f " $kernel " ] | [ ! -f " $initramfs " ]; then
169+ if [ ! -f " $kernel " ] || [ ! -f " $initramfs " ]; then
170170 fail " kernel or initramfs not found"
171- # in case no kernel or initramfs, it is a container image
172- # chroot and install ci and add symlink to host vmlinuz/initrd and update initramfs
173171 fi
174172}
175173
176- # main
177-
178- # move defaults here
179- declare -A options=(
180- [--debug]=" false"
181- [--cidata]=" "
182- [--kernel]=" "
183- [--initramfs]=" "
184- [--init]=" "
185- [--user]=" "
186- [--pass]=" "
187- [--name]=" "
188- )
189-
174+ # Main
190175handle_options " $@ "
191176
192- if [[ " ${options[--debug]} " == true ]]; then
193- set -x
194- fi
195-
196- echo " validating requirements"
177+ echo " Validating requirements"
197178validate
198179
199- echo " preparing rootfs"
180+ echo " Preparing rootfs"
200181prepare_rootfs
201182
202- echo " preparing boot"
183+ echo " Preparing boot"
203184prepare_boot
204185
205- echo " creating cidata"
186+ echo " Creating cidata"
206187create_cidata
207188trap cleanup EXIT
208189
209- echo " booting "
210- boot
190+ echo " Booting "
191+ boot
0 commit comments