summaryrefslogtreecommitdiff
path: root/bin/vm
blob: 5a8ea8deec86f8f7efdf07e30cbe847d23173875 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/sh

# vm is a tool to manage virtual machines.

vm_version='vm-0.1'
unset CDPATH
export LC_ALL=C IFS=' 	
'

add() {
	usage 'add name' 'Add a new VM' && return
	[ -d "$idir/$1" ] && die "vm $1 already exists in $idir/$1"
	init_alpine || die "could not init alpine iso"
	mkdir -p "$idir/$1" && cd "$idir/$1" || die "add $1 failed"
	pwd
}

del() {
	usage 'del name' 'Delete a VM' && return
	echo 'not implemented yet'
}

die() { echo "$0: fatal: $@" >&2; exit 1; }

help() {
	usage 'help' 'Print this help text' && return
        echo "$vm_version
Manage virtual machines
Usage: vm command [options] [args]"
        Opth=1; for c in $Cmdlist; do $c; done
}

info() {
	usage 'info name' 'Print informations on a VM' && return
	echo 'not implemented yet'
}

init() {
	:
}

init_alpine() {
	mkdir -p "$sdir/alpine"  && cd "$sdir/alpine" || die "init alpine failed"
	[ -f Makefile ] || cat << \EOT > Makefile
# Do no edit. This file is generated  by vm.
# Install a run a native AlpineLinux VM in Apple silicon MacOS

# Check https://alpinelinux.org/downloads for possible upgrades
iso_url = https://dl-cdn.alpinelinux.org/alpine/v3.13/releases/aarch64/alpine-virt-3.13.2-aarch64.iso
iso = $(notdir $(iso_url))
isoinfo ?= /opt/homebrew/bin/isoinfo

all: initrd vmlinux

runiso: initrd vmlinux $(iso)
	vftool -k vmlinux -i initrd -d hdd.img -c $(iso)

initrd: $(iso) $(isoinfo)
	isoinfo -i $(iso) -J -x /boot/initramfs-virt > $@

vmlinux: $(iso) $(isoinfo)
	isoinfo -i $(iso) -J -x /boot/vmlinuz-virt | gunzip > $@

$(iso):
	curl -LO $(iso_url) || { rm -f $@; false; }

$(isoinfo):
	brew install cdrtools
EOT

	make -s all
}

ls() {
	usage 'ls' 'list VMs' && return
	[ -d "$idir" ] && cd "$idir" || return
	for i in *; do
		echo "$i"
	done
}

runiso() {
	usage 'runiso [isoname]' 'run a cdrom iso' && return
	init_alpine
	make runiso
}

start() {
	usage 'start name' 'Start a VM' && return
	echo 'not implemented yet'
}

stop() {
	usage 'stop name' 'Stop a VM' && return
	echo 'not implemented yet'
}

usage() { [ "$Opth" ] && printf " %-34s %s\n" "$1" "$2"; }

version() {
	usage 'version' 'Print version' && return
	echo "$vm_version"
}

# Main starts here.
pdir="$HOME/.vm"
sdir="$pdir/iso"
edir="$pdir/engine"
idir="$pdir/img"

engines="vftool"	# Todo: add qemu (when supported on m1)
isos="alpine"		# Todo: add freebsd, openbsd, archlinux, debian, ubuntu, macOS, win10, etc...

Cmdlist='add del info help ls runiso start stop version'
[ "$1" ] && C=$1 && shift 1 || { help; exit 1; }
for c in $Cmdlist
do
	case $c in
	($C)  cmd=$c; break;;
	($C*) [ "$cmd" ] && die "ambiguous command $C" || cmd=$c;;
	esac
done
[ "$cmd" ] || { help; exit 1; } && $cmd "$@"