11#! /bin/bash
2- set -x
32socket=" /tmp/virtiofs.sock"
43
54rootfs=" "
@@ -30,73 +29,40 @@ usage() {
3029 echo " --name Hostname for the system (optional)"
3130 echo " --init Entrypoint for the system (optional)"
3231 echo " "
33- exit 1
3432}
3533
34+ declare -A options
3635handle_options () {
3736
3837 while [[ " $# " -gt 0 ]]; do
3938 case $1 in
40- --rootfs)
41- rootfs=" $2 "
42- kernel=" $rootfs /boot/vmlinuz"
43- initram=" $rootfs /boot/initrd.img"
44- shift
45- ;;
46- --flist)
47- flist=" $2 "
48- shift
49- ;;
50- --cidata)
51- cidata=" $2 "
52- shift
53- ;;
54- --init)
55- if [ ! -z " $2 " ]; then
56- cmdline=" $cmdline init=$2 "
57- fi
58- shift
59- ;;
60-
61- --user)
62- user=" $2 "
63- shift
64- ;;
65- --pass)
66- pass=" $2 "
67- shift
68- ;;
69- --name)
70- name=" $2 "
71- shift
39+ --image|--cidata|--kernel|--initramfs|--init|--user|--pass|--name)
40+ options[$1 ]=" $2 "
41+ shift 2
7242 ;;
73-
7443 * )
7544 usage
45+ exit 1
7646 ;;
7747 esac
78- shift
7948 done
80-
8149}
8250
8351validate () {
84- # if no rootfs or metadata, fail
85- # check other binaries
86- which virtiofsd & > /dev/null || fail " virtiofsd not found in PATH"
87- which cloud-hypervisor & > /dev/null || fail " cloud-hypervior not found in path"
88-
89- if [ ! -f " ${kernel} " ]; then
90- fail " kernel file not found"
91- fi
52+ for tool in virtiofsd cloud-hypervisor mkdosfs rfs1; do
53+ which " $tool " & > /dev/null || fail " '$tool ' not found in PATH"
54+ done
9255
93- if [ ! -f " ${initram} " ]; then
94- fail " kernel file not found"
95- fi
56+ [ -z " ${options[--image]} " ] && fail " rootfs image not provided"
9657}
9758
9859cidata=" /tmp/cidata.img"
9960create_cidata () {
61+ [[ -f " ${options[--cidata]} " ]] && return
62+
63+ echo " Creating cidata"
64+ # todo: cloud-init clean
65+
10066 rm -f " ${cidata} "
10167 mkdosfs -n CIDATA -C " ${cidata} " 8192
10268
12086 rm -f meta-data
12187}
12288
123- start_fs () {
124- sudo virtiofsd \
125- --socket-path=" ${socket} " \
126- --shared-dir=" ${overlayfs} " \
127- --cache=never \
128- &
129-
130- fspid=$!
131- }
13289
13390cleanup () {
134- sudo umount " $overlayfs "
135- kill " $flpid " & > /dev/null
136- kill " $fspid " & > /dev/null || true
91+ local pids=( " $flpid " " $fspid " )
92+
93+ # unmount and remove overlayfs
94+ sudo umount " $overlayfs " 2> /dev/null || true
95+ sudo rm -rf /tmp/upper /tmp/workdir " $overlayfs " 2> /dev/null || true
96+
97+ # kill any running processes
98+ for pid in " ${pids[@]} " ; do
99+ kill " $pid " 2> /dev/null || true
100+ done
137101}
138102trap cleanup EXIT
139103
@@ -150,53 +114,66 @@ run_hypervisor() {
150114 --console off
151115}
152116
153- create_rwlayer () {
117+ prepare_rootfs () {
118+ local image_path=" ${options[--image]} "
119+
154120 sudo mkdir -p /tmp/upper /tmp/workdir " $overlayfs "
155121
122+ if [[ ${image_path} == * .flist ]]; then
123+ echo " ${image_path} is flist, mounting"
124+
125+ rootfs=/tmp/lower
126+ sudo mkdir -p " $rootfs " && sudo chmod 777 " $rootfs "
127+
128+ rfs1 --meta " $image_path " " $rootfs " &
129+ flpid=$!
130+ else
131+ rootfs=" ${image_path} "
132+ fi
133+
134+ echo " Mounting overlay"
156135 sudo mount \
157136 -t overlay \
158137 -o lowerdir=" $rootfs " ,upperdir=/tmp/upper,workdir=/tmp/workdir \
159138 none \
160139 " $overlayfs "
161- }
162-
163- mount_flist () {
164- wget " $flist " -O /tmp/flist.flist
165140
166- rootfs=/home/omar/tmp
167- mkdir -p " $rootfs "
168- rfs1 --meta /tmp/flist.flist " $rootfs " &
169-
170- flpid=$!
141+ echo " Starting virtiofs"
142+ sudo virtiofsd \
143+ --socket-path=" ${socket} " \
144+ --shared-dir=" ${overlayfs} " \
145+ --cache=never \
146+ &
147+ fspid=$!
171148}
172149
173- # decompress_kernel () {
174- # wget -O /tmp/extract-vmlinux https://raw.githubusercontent.com/torvalds/linux/master/scripts/extract-vmlinux
175- # chmod +x /tmp/extract-vmlinux
176-
177- # }
150+ prepare_boot () {
151+ kernel= " ${options[--kernel]} "
152+ if [ -z " $kernel " ] ; then
153+ kernel= " ${overlayfs} /boot/vmlinuz "
154+ fi
178155
179- prepare_cloud_image () {
180- # chroot and install ci
181- # symlink to host vmlinuz/initrd
182- # load virtiofs and update ramfs
183- }
156+ initramfs=" ${options[--initramfs]} "
157+ if [ -z " $initramfs " ]; then
158+ initramfs=" ${overlayfs} /boot/initrd.img"
159+ fi
160+
161+ init=" ${options[--init]} "
162+ if [ ! -z " $init " ]; then
163+ cmdline=" $cmdline init=$init "
164+ fi
184165
185- boot () {
186- start_fs
187- run_hypervisor
166+ if [ ! -f " $kernel " ] | [ ! -f " $initramfs " ]; then
167+ fail " kernel or initramfs not found"
168+ # in case no kernel or initramfs, it is a container image
169+ # chroot and install ci and add symlink to host vmlinuz/initrd and update initramfs
170+ fi
188171}
189172
190173handle_options " $@ "
191-
192174validate
193-
194- # if no cidata
175+ prepare_rootfs
176+ prepare_boot
195177create_cidata
196178
197- # if is metadata
198- mount_flist
199-
200- create_rwlayer
201-
202- boot
179+ # run_hypervisor
0 commit comments