Last active
January 20, 2023 03:32
-
-
Save hedzr/ea2626fb290e5ca74687967d0d768cdb to your computer and use it in GitHub Desktop.
function `vm` - manage VMs from command-line - chs post at https://hedzr.com/devops/linux/manage-vms-from-command-line/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# `vm`: To run it, vmware_run.sh & vagrant_run.sh should be integrated | |
# into this script, or you have a zshrc import framework to make | |
# them work together. | |
# And, `bash.sh` (https://github.com/hedzr/bash.sh) should be | |
# imported for `strip_r`, `commander`, `tip`, `err`, and so on. | |
# But you can always cut them from original. | |
# | |
# - bash.sh - https://github.com/hedzr/bash.sh | |
# - vm.sh - https://gist.github.com/hedzr/ea2626fb290e5ca74687967d0d768cdb | |
# - vmware_run.sh - https://gist.github.com/hedzr/956cb892069b0353f915b395a9504ebf | |
# - vagrant_run.sh - https://gist.github.com/hedzr/a24592879ac90239be6c8b1746feebd4 | |
# | |
vm() { | |
# strip_r() { echo ${1%"$2"}; } | |
vm_entry() { commander $(strip_r $(fn_name) _entry) "$@"; } | |
vm_usage() { | |
cat <<-EOF | |
Usage: $0 $self <sub-command> [...] | |
Sub-commands: | |
ls, list lists all known vhosts | |
size [vm-short-name] prints the disk usage of a VM on host | |
run <vm-short-name> [up|halt|destroy...] | |
invoke vagrant_run function | |
run <vm-short-name> <child-vm-name> [up|halt|destroy...] | |
invoke vagrant_run function | |
vmware <vm-name> [start|up|stop|halt|suspend|reset|pause|unpause] | |
invoke vmware_run function | |
Examples: | |
$ vm size arm-cxx # prints arm-cxx.local VM size on host | |
$ vm size # prints disk size of all vhosts | |
$ vm vmware u20.local up | |
# run the vmware vm in ~/Downloads/VMs/vmware/u20.local.vmwarevm | |
$ vm run ub20a.local up | |
# run the virtualbox vm with Named Folder '~vhost.ub20a' | |
$ vm run ubuntu20test | |
# run the virtualbox vm with its name | |
EOF | |
} | |
vm_vmware() { tip "$@" && vmware_run "$@"; } | |
vm_call() { vagrant_run "$@"; } | |
vm_run() { vagrant_run "$@"; } | |
vm_list() { vm_ls; } | |
vm_ls() { | |
__vms_reg | |
which VBoxManage >/dev/null && { | |
headline "VMs in virtualbox (* running)" | |
echo | |
local line lines=$(VBoxManage list vms --sorted) | |
local unprocessed=1 running=$(echo $(VBoxManage list runningvms | awk '{print $1}')) | |
while read line; do | |
if [ "$running" != "" ]; then | |
if echo $line | grep -q "$running"; then | |
printf '* %s\n' "$line" && unprocessed=0 | |
fi | |
fi | |
if ((unprocessed)); then | |
printf ' %s\n' "$line" | |
fi | |
done <<<"$lines" | |
echo | |
} | |
headline "VMs in hash folder (for vagant vhosts)" | |
echo | |
hash -d | grep -E 'vhost\.(.*)=' | sed -re "s#${HOME//\//\\/}#~#" | pad 2 | |
cat <<-"EOF" | |
To manage the short names (hash folder names) of your | |
vagrant hosts, modify the $HOME/.vagrant.hosts with | |
any text editor. | |
EOF | |
which vmrun >/dev/null && { | |
headline "The Running VMware VMs" | |
echo | |
vmrun list | pad 2 | sed -re "s,$HOME,~," | |
} | |
} | |
vm_sizes() { vm_size "$@"; } | |
vm_size() { | |
local i name asan=0 | |
for i in "$@"; do | |
[ "$i" = "--all" ] && asan=1 || name="$i" | |
done | |
__vms_reg | |
[ "$name" = "" ] && { | |
dbg "\$# = $#" | |
function test_and_print_size() { | |
local dir="$1" && shift | |
if [ -d "$dir" ]; then | |
if test -n "$(find "$dir" -type d -mindepth 1 -maxdepth 1 -name '*' -print -quit)"; then | |
echo | |
headline "$@" | |
echo | |
du -sh "$dir"/* | sort -hr | pad 2 | sed -re "s,$HOME,~," | |
fi | |
fi | |
} | |
function test_and_print_size_exact() { | |
local dir=$1 && shift | |
[ -d "$dir" ] && { | |
echo | |
headline "$@" | |
du -sh "$dir" | sort -hr | pad 2 | sed -re "s,$HOME,~," | |
} | |
} | |
test_and_print_size ~/Downloads/VMs/virtualbox "The VirtualBox VMs" | |
test_and_print_size ~/Downloads/VMs/vmware "The VMware VMs" | |
test_and_print_size ~/.vagrant.d/boxes "The VirtualBox BOXes" | |
test_and_print_size ~/.local/share/containers "The Podman vmdisk" | |
test_and_print_size ~/.config/containers "The Podman VM configs" | |
test_and_print_size_exact ~/Library/Containers/com.docker.docker/Data/vms "The Docker Desktop vmdisk" | |
test_and_print_size ~/.android/avd "Android Emulators" | |
(($asan)) && test_and_print_size_exact ~/Library/Android/sdk "Android SDK" | |
(($asan)) && test_and_print_size_exact ~/Library/Android/sdk/ndk "Android NDK (inside SDK directory)" | |
return | |
} | |
hash -d | grep -qE 'vhost.'${name}'=' && [ -d ~vhost.$name ] && { | |
echo "Calculating the folder size of " ~vhost.$name " ..." | |
pushd ~vhost.$name >/dev/null | |
if [ -f Vagrantfile ]; then | |
du -sh "$(pwd)" "$HOME/Downloads/VMs/virtualbox/$(basename $(pwd))"* | sed -re "s,$HOME,~," | |
fi | |
popd >/dev/null | |
} || { | |
echo "Error: Unknown vagrant vhost name: $name" 1>&2 | |
echo 1>&2 | |
echo "You may create the hash link for a vagrant vhost:" 1>&2 | |
echo " $ hash -d vhost.ub20a=\"\$HOME/work/ops.work/ub20a.local\"" 1>&2 | |
} | |
} | |
vm_box_entry() { commander $(strip_r $(fn_name) _entry) "$@"; } | |
vm_box_usage() { | |
cat <<-"EOF" | |
Examples: | |
$ vm box size | |
EOF | |
} | |
vm_box_size() { | |
__vms_reg | |
du -sh "$HOME/.vagrant.d/boxes/"* | sort -rh | |
} | |
vm_entry "$@" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment