A shell script to start an OpenBSD VM and log the console input/output to an unique log file
The name of the logs start with the VM name followed by DDMM_HHMMSS.log
Code:
18259 Aug 1 04:15 vmlogs/alix_new0731_221336.log
11549 Aug 9 04:39 vmlogs/alix_new0809_043239.log
This name is created with:
Code:
LOGFILE="${LOGDIR}/${VMNAME}$(date -j "+%m%d_%H%M%S").log"
For customizing this format, see
date(1) and
strftime(3)
Some alternatives shown on the shell command line:
Code:
$ date -j "+%m%d_%H:%M:%S"
0809_05:37:05
$ date -j "+%m%d_%H_%M_%S"
0809_05_37_56
$ date -j "+%b%d_%H_%M_%S"
Aug09_05_43_59
The following snippet from the script shows how you could start the script:
Code:
# if variable VMNAME is not defined you will be prompted to select a VM
# USAGE options:
# 1) $ env VMNAME=alix_new ./dovm.sh
# 2) $ export VMNAME=alix_new ; ./dovm.sh
# 3) Define the shell variable VMNAME in the script
# 4) Just run './dovm.sh' and select a stopped VM from a menu
# 5) If you place it in your $HOMEDIR/bin directory: $ dovm.sh
Using the interactive method 4:
Code:
$ dovm.sh
ID PID VCPUS MAXMEM CURMEM TTY OWNER STATE NAME
1 - 1 1.0G - - adriaan:wheel stopped df-us
2 - 1 1.0G - - adriaan:wheel stopped siralas-clone
3 - 1 1.0G - - adriaan:wheel stopped hetzner
4 - 1 500M - - adriaan:wheel stopped alix_new
Select 'ID' number of VM to start : 3
Script started on Tue Aug 9 05:56:44 2022
Connected to /dev/ttypb (speed 115200)
Using drive 0, partition 3.
Loading......
probing: pc0 com0 mem[638K 1022 a20=on]
disk: hd0+
>> OpenBSD/amd64 BOOT 3.53
com0: 115200 baud
switching console to com0
>> OpenBSD/amd64 BOOT 3.53
[snip]
root@df-us[~]shutdown -hp now
Shutdown NOW!
shutdown: [pid 33853]
*** FINAL System shutdown message from root@df-us.utp.xnet ***
System going down IMMEDIATELY
root@df-us[~]
System shutdown time has arrived
root@df-us[~]syncing disks... done
vmmci0: powerdown
The operating system has halted.
Please press any key to reboot.
Instead of rebooting we press '~." ( tilde followed by a dot) to exit the console.
Code:
[EOT]
Script done, output file is /home/adriaan/vmlogs/hetzner0809_055644.log
You will then see a hint to use
col(1) to sanitize 'download progress meter' lines:
Code:
------------------------------------------------------------------------------
HINT: You can use col(1) to filter out backspaces on messy lines like this:
------------------------------------------------------------------------------
[snip]
^MGet/Verify SHA256.sig 0% | | 0
--:-- ETA^MGet/Verify SHA256.sig 100% |**************************|
2144 00:00 Signature Verified ^MGet/Verify bsd 0%
| | 0 --:-- ETA^MGet/Verify bsd
8% |** | 1792 KB 00:11 ETA^MGet/Verify
bsd 17% |****
[snip]
For example:
$ col -b <df-us0724_023205.log | less
[snip]
Get/Verify SHA256.sig 100% |**************************| 2144 00:00 ETA
Signature Verified
Get/Verify bsd 100% |**************************| 22339 KB 00:11 ETA
Get/Verify bsd.rd 100% |**************************| 4498 KB 00:02 ETA
[snip]
Neat huh?
You can disable this hint by setting the variable SHOW_COL_HINT to 0:
Code:
#SHOW_COL_HINT=0
SHOW_COL_HINT=1