diff options
| author | Marc Vertes <mvertes@free.fr> | 2021-03-22 08:50:26 +0100 |
|---|---|---|
| committer | Marc Vertes <mvertes@free.fr> | 2021-03-22 08:50:26 +0100 |
| commit | 262eb286cb7c8d7412160cacfb022247b1f09f00 (patch) | |
| tree | 95e3f61b74fd4e09d67b3d6188da3ee083f410a7 /bin/vm | |
| parent | a8e3586e9b691c2a25b5f2e50a2761d5fcad18f8 (diff) | |
update
Diffstat (limited to 'bin/vm')
| -rwxr-xr-x | bin/vm | 77 |
1 files changed, 60 insertions, 17 deletions
@@ -7,9 +7,12 @@ vm_version='vm-0.1' # TODO: # - DONE: fetch, build and install vftool in .vm/vftool # - DONE: setup a config file per VM -# - creation of hdd image -# - import iso, kernel, initrd from existing vm +# - DONE: creation of hdd image +# - DONE: delete a VM +# - DONE: import iso, kernel, initrd from existing vm +# - when no vm is specified, apply command to last one # - script install from CDROM iso: +# - patch alpine setup-disk # - setup system, including static IP address # - setup user account from host # - setup ssh from host @@ -22,9 +25,9 @@ export LC_ALL=C IFS=' add() { usage 'add [Options] name' 'Add a new VM' && return - while getopts :c:p:s: opt; do + while getopts :c:i:k:p:s: opt; do case $opt in - c|k|p|s) eval "$opt=$OPTARG";; + [cikps]) eval "$opt=$OPTARG";; *) Opth=1 add; exit ;; esac done @@ -32,6 +35,7 @@ add() { [ -d "$dir/$1" ] && die "vm $1 already exists in $dir/$1" init_alpine || die "could not init alpine iso" + [ "$1" = 'alpine-iso' ] && return mkdir -p "$dir/$1" && cd "$dir/$1" || die "add $1 failed" hdd=${p-$1.raw} [ -f "$hdd" ] || dd if=/dev/zero of=$hdd bs=1 count=0 seek=${s-32g} 2>/dev/null @@ -41,11 +45,11 @@ add() { iso=$(getconf "$c" iso) [ "$iso" ] && echo "iso=\"../$c/$iso\"" >> config cp "../$c/vmlinux" "../$c/initrd" . + echo "kernel=${k-vmlinux}" >> config + echo "initrd=initrd" >> config } } -getconf() { awk -F '=' -v k="$2" '$1 == k {print $2}' "$dir/$1/config"; } - console() { usage 'console name' 'Attach a console to a VM' && return [ "$1" ] || die 'console: name is missing' @@ -56,10 +60,39 @@ console() { del() { usage 'del name' 'Delete a VM' && return - echo 'not implemented yet' + case $1 in ''|*/*|.*) die "invalid VM name: $1" ;; esac + [ -d "$dir/$1" ] || die "$dir/$1 not found" + cd "$dir" && rm -r "$1" +} + +die() { [ "$1" ] && echo "$0: $@" >&2; exit 1; } + +edit() { + usage 'edit name' 'Edit a VM configuration' && return + [ -f "$dir/$1/config" ] || die "$dir/$1/config not found" + ${EDITOR-vi} "$dir/$1/config" +} + +exp() { + usage 'exp name' 'experiment on a VM' && return + cd "$dir/$1" || die "invalid VM: $1" + [ -f vftool.pid ] || die "vm $1 is not active" + + screen -X stuff 'root +' + screen -X stuff 'sed -i "s/die..Bootloader/;; #/" /sbin/setup-disk +' + screen -X stuff 'grep BOOTLOADER /sbin/setup-disk +' + } -die() { [ "$1" ] && echo "$@" >&2; exit 1; } +finalize() { + tty=$(vftool_tty) + echo "root\nuname -a" >> "$tty" +} + +getconf() { awk -F '=' -v k="$2" '$1 == k {print $2}' "$dir/$1/config"; } help() { usage 'help' 'Print this help text' && return @@ -90,6 +123,11 @@ vmlinux: $(iso) $(isoinfo) config: $(iso) @echo "iso=$(iso)" >config + @echo "initrd=initrd" >>config + @echo "kernel=vmlinux" >>config + @echo "cpu=1" >> config + @echo "ram=512" >> config + @echo "arg=\"console=hvc0\"" >> config $(iso): curl -LO $(iso_url) || { rm -f $@; false; } @@ -126,17 +164,19 @@ ls() { [ -d "$dir" ] && cd "$dir" || return for i in */; do i=${i%/} + [ "$i" = '*' ] && continue [ -f "$i/vftool.pid" ] && state=active || state=stopped printf "%-20s %s\n" "$i" "$state" done } start() { - usage 'start [-a][-c vm] name' 'Start a VM' && return - while getopts :ac: opt; do + usage 'start [-af][-c vm] name' 'Start a VM' && return + while getopts :ac:f opt; do case $opt in - (a) attach=1 ;; + (a) a=1 ;; (c) from=$OPTARG ;; + (f) f=1 ;; (*) Opth=1 start; exit;; esac done @@ -146,8 +186,9 @@ start() { init_vftool cd "$dir/$1" || die "start $1 failed" [ -f vftool.pid ] && die "Error: process $(cat vftool.pid) is active or $PWD/vftool.pid should be removed" - start_vm & - [ "$attach" ] && sleep 1 && vm console $1 + start_vm & sleep 2 + [ "$f" ] && finalize + [ "$a" ] && vm console $1 } start_vm() ( @@ -157,8 +198,8 @@ start_vm() ( trap 'rm -f vftool.pid' EXIT "$dir/vftool" \ - -k vmlinux \ - -i initrd \ + ${kernel+-k "$kernel"} \ + ${initrd+-i "$initrd"} \ ${hdd+-d "$hdd"} \ ${iso+-c "$iso"} \ ${cpu+-p "$cpu"} \ @@ -167,7 +208,7 @@ start_vm() ( >>vftool.log 2>&1 & sleep 1 echo "$!" >vftool.pid - screen -S "${PWD##*/}" -d -m "$(grep -om 1 '\/dev\/tty.*' vftool.log)" + screen -S "${PWD##*/}" -d -m "$(vftool_tty)" wait ) @@ -186,9 +227,11 @@ version() { echo "$vm_version" } +vftool_tty() { grep -om 1 '\/dev\/tty.*' 'vftool.log'; } + # Main starts here. dir="$HOME/.vm" -Cmdlist='add console del info help ls log start stop version' +Cmdlist='add console del edit exp info help ls log start stop version' [ "$1" ] && C=$1 && shift 1 || { help; exit 1; } for c in $Cmdlist; do case $c in |
