-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvbox.sh
1006 lines (810 loc) · 20.3 KB
/
vbox.sh
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
set -e
export PATH=$PATH:/Library/Frameworks/Python.framework/Versions/Current/bin
_script="$0"
_script_home="$(dirname "$_script")"
isLinux() {
uname -a | grep -i "Linux" >/dev/null
}
_SUDO_VIR_=""
if isLinux; then
_SUDO_VIR_=sudo
fi
#installOCR
setup() {
_installOCR="$1"
if isLinux; then
sudo apt-get update
sudo apt-get install -y zstd libvirt-daemon-system virt-manager qemu-kvm qemu-system-arm libosinfo-bin axel expect screen sshpass
if [ "$_installOCR" ]; then
sudo apt-get install -y tesseract-ocr python3-pil tesseract-ocr-eng tesseract-ocr-script-latn python3-opencv python3-pip
if ! pip3 install --break-system-packages pytesseract opencv-python vncdotool; then
#ubuntu 22.04
pip3 install pytesseract opencv-python vncdotool
fi
fi
else
brew install tesseract libvirt qemu virt-manager axel
brew services start libvirt
virsh net-define --file /usr/local/etc/libvirt/qemu/networks/default.xml
virsh net-autostart default
virsh net-start default
pip3 install pytesseract opencv-python
echo "Reloading sshd services in the Host"
sudo sh <<EOF
echo "" >>/etc/ssh/sshd_config
echo "StrictModes no" >>/etc/ssh/sshd_config
EOF
sudo launchctl unload /System/Library/LaunchDaemons/ssh.plist
sudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist
fi
mkdir -p "$HOME/.ssh"
chmod 700 "$HOME/.ssh"
sudo chmod o+rx $HOME
}
#link and localfile
download() {
_link="$1"
_file="$2"
echo "Downloading $_link"
axel -n 8 -o "$_file" -q "$_link"
echo "Download finished"
}
#isolink osname ostype sshport
createVM() {
_isolink="$1"
_osname="$2"
_ostype="$3"
_sshport="$4"
if [ -z "$_osname" ]; then
echo "Usage: createVM isolink osname ostype sshport"
echo "Usage: createVM 'https://xxxxx.com/xxx.iso' netbsd NetBSD_64 2225"
return 1
fi
_vdi="$_osname.qcow2"
_iso="$_osname.iso"
if [[ "$_isolink" == *"img" ]]; then
_iso="$_osname.img"
fi
if [ ! -e "$_iso" ]; then
download "$_isolink" $_iso
if echo "$_isolink" | grep 'bz2$'; then
mv "$_iso" "$_iso.bz2"
bzip2 -dc "$_iso.bz2" >"$_iso"
fi
fi
qemu-img create -f qcow2 -o preallocation=off $_vdi 200G
if [ "$VM_ARCH" = "aarch64" ]; then
if [[ "$_iso" == *"img" ]]; then
$_SUDO_VIR_ virt-install \
--name $_osname \
--memory 6144 \
--vcpus ${VM_CPU:-2} \
--arch aarch64 \
--disk $_iso \
--disk path=$_vdi,format=qcow2,bus=${VM_DISK:-virtio} \
--os-variant=$_ostype \
--network network=default,model=e1000 \
--graphics vnc,listen=0.0.0.0 \
--noautoconsole --import --machine virt --noacpi --boot loader=/usr/share/AAVMF/AAVMF_CODE.fd
else
$_SUDO_VIR_ virt-install \
--name $_osname \
--memory 6144 \
--vcpus 2 \
--arch aarch64 \
--disk path=$_vdi,format=qcow2,bus=${VM_DISK:-virtio} \
--cdrom $_iso \
--os-variant=$_ostype \
--network network=default,model=e1000 \
--graphics vnc,listen=0.0.0.0 \
--noautoconsole --import --machine virt --noacpi --boot loader=/usr/share/AAVMF/AAVMF_CODE.fd
fi
else
$_SUDO_VIR_ virt-install \
--name $_osname \
--memory 6144 \
--vcpus ${VM_CPU:-2} \
--arch ${VM_ARCH:-x86_64} \
--disk path=$_vdi,format=qcow2,bus=${VM_DISK:-virtio} \
--cdrom $_iso \
--os-variant=$_ostype \
--network network=default,model=e1000 \
--graphics vnc,listen=0.0.0.0 \
--noautoconsole --import
fi
}
#osname ostype sshport
createVMFromVHD() {
_osname="$1"
_ostype="$2"
_sshport="$3"
if [ -z "$_osname" ]; then
echo "Usage: createVMFromVHD osname ostype sshport"
echo "Usage: createVMFromVHD freebsd freebsd13.1 2222"
return 1
fi
_vhd="$_osname.qcow2"
sudo qemu-img resize $_vhd +200G
if [ "$VM_ARCH" = "aarch64" ]; then
$_SUDO_VIR_ virt-install \
--name $_osname \
--memory 6144 \
--vcpus ${VM_CPU:-2} \
--arch ${VM_ARCH} \
--disk $_vhd,format=qcow2,bus=${VM_DISK:-virtio} \
--os-variant=$_ostype \
--network network=default,model=e1000 \
--graphics vnc,listen=0.0.0.0 \
--noautoconsole --import --machine virt --noacpi --boot loader=/usr/share/AAVMF/AAVMF_CODE.fd
else
$_SUDO_VIR_ virt-install \
--name $_osname \
--memory 6144 \
--vcpus ${VM_CPU:-2} \
--arch ${VM_ARCH:-x86_64} \
--disk $_vhd,format=qcow2,bus=${VM_DISK:-virtio} \
--os-variant=$_ostype \
--network network=default,model=e1000 \
--graphics vnc,listen=0.0.0.0 \
--noautoconsole --import
fi
$_SUDO_VIR_ virsh shutdown $_osname
$_SUDO_VIR_ virsh destroy $_osname
}
disableSecureBoot() {
_osname="$1"
$_SUDO_VIR_ virsh dumpxml "$_osname" >"$_osname".xml
$_SUDO_VIR_ sed -i "s/firmware='efi'//" "$_osname".xml
$_SUDO_VIR_ sed -i "/enrolled-keys/d" "$_osname".xml
$_SUDO_VIR_ sed -i "/feature enabled='yes' name='secure-boot'/d" "$_osname".xml
$_SUDO_VIR_ sed -i 's/AAVMF_CODE.ms.fd/AAVMF_CODE.fd/' "$_osname".xml
$_SUDO_VIR_ sed -i 's/AAVMF_VARS.ms.fd/AAVMF_VARS.fd/' "$_osname".xml
$_SUDO_VIR_ virsh undefine "$_osname" --nvram
$_SUDO_VIR_ virsh define "$_osname".xml
}
#ova
importVM() {
_osname="$1"
_ostype="$2"
_ova="$3"
_iso="$4"
_mem="${5:-6144}"
_cpu="${6:-2}"
if [ -z "$_ova" ]; then
echo "Usage: importVM xxxx.ova"
return 1
fi
if [ "$VM_ARCH" = "aarch64" ]; then
$_SUDO_VIR_ virt-install \
--name $_osname \
--memory 6144 \
--vcpus ${VM_CPU:-2} \
--arch aarch64 \
--disk $_ova,format=qcow2,bus=${VM_DISK:-virtio} \
--os-variant=$_ostype \
--network network=default,model=e1000 \
--graphics vnc,listen=0.0.0.0 \
--noautoconsole --import --check all=off --machine virt --noacpi --boot loader=/usr/share/AAVMF/AAVMF_CODE.fd
else
$_SUDO_VIR_ virt-install \
--name $_osname \
--memory $_mem \
--vcpus $_cpu \
--arch ${VM_ARCH:-x86_64} \
--disk $_ova,format=qcow2,bus=${VM_DISK:-virtio} \
--os-variant=$_ostype \
--network network=default,model=e1000 \
--graphics vnc,listen=0.0.0.0 \
--noautoconsole --import --check all=off
fi
$_SUDO_VIR_ virsh shutdown $_osname
$_SUDO_VIR_ virsh destroy $_osname
if [ "$VM_ARCH" = "aarch64" ]; then
disableSecureBoot $_osname
fi
}
isVMReady() {
_osname="$1"
[ -e "$HOME/$_osname.rebooted" ]
}
waitForVMReady() {
_osname="$1"
while ! isVMReady $_osname ; do
echo "VM is booting, just wait"
sleep 2
$_SUDO_VIR_ virsh send-key $_osname KEY_ENTER
done
echo "VM is ready!"
cat "$HOME/$_osname.rebooted"
}
#osname
startVM() {
_osname="$1"
if [ -z "$_osname" ]; then
echo "Usage: startVM netbsd"
return 1
fi
rm -f $HOME/$_osname.rebooted
$_SUDO_VIR_ virsh start $_osname
}
openConsole() {
_osname="$1"
CONSOLE_NAME="$_osname-$VM_RELEASE-console"
CONSOLE_FILE="$_script_home/$_osname-$VM_RELEASE-console.log"
rm -f "$CONSOLE_FILE"
screen -dmLS "$CONSOLE_NAME" -Logfile "$CONSOLE_FILE" -L $_SUDO_VIR_ virsh console "$_osname"
}
closeConsole() {
_osname="$1"
CONSOLE_NAME="$_osname-$VM_RELEASE-console"
if screen -list | grep -q "$CONSOLE_NAME"; then
screen -S "$CONSOLE_NAME" -X quit
fi
}
#osname optsfile
processOpts() {
_osname="$1"
_optsfile="$2"
if [ -z "$_optsfile" ]; then
echo "Usage: processOpts netbsd netbsd.9.2.opts.txt"
return 1
fi
while read -r line; do
if [ -z "$(echo "$line" | tr -d '# ' )" ]; then
continue
fi
if echo "$line" | grep "^#" >/dev/null ; then
continue
fi
echo "====> $line"
_text="$(echo "$line" | cut -d '|' -f 1 | xargs)"
_keys="$(echo "$line" | cut -d '|' -f 2 )"
_timeout="$(echo "$line" | cut -d '|' -f 3 )"
echo "========> Text: $_text"
echo "========> Keys: $_keys"
echo "========> Timeout: $_keys"
if waitForText "$_osname" "$_text" "$_timeout"; then
echo "Input keys: $_keys"
input "$_osname" "$_keys"
else
echo "Timeout for waiting for text: $_text"
fi
sleep 1
done <"$_optsfile"
}
#osname
clearVM() {
_osname="$1"
if [ -z "$_osname" ]; then
echo "Usage: clearVM netbsd"
return 1
fi
if $_SUDO_VIR_ virsh list | grep $_osname; then
$_SUDO_VIR_ virsh shutdown $_osname
$_SUDO_VIR_ virsh destroy $_osname
$_SUDO_VIR_ virsh undefine $_osname --remove-all-storage
fi
rm -f ~/.ssh/known_hosts
}
#osname
shutdownVM() {
_osname="$1"
if [ -z "$_osname" ]; then
echo "Usage: shutdownVM netbsd"
return 1
fi
rm -f $HOME/$_osname.rebooted
$_SUDO_VIR_ virsh shutdown $_osname
sleep 2
}
#force shutdown
#osname
destroyVM() {
_osname="$1"
if [ -z "$_osname" ]; then
echo "Usage: destroyVM netbsd"
return 1
fi
rm -f $HOME/$_osname.rebooted
$_SUDO_VIR_ virsh destroy $_osname
sleep 2
}
#osname
isRunning() {
_osname="$1"
if $_SUDO_VIR_ virsh list --all | grep $_osname | grep -i running; then
return 0
fi
return 1
}
detachIMG() {
_osname="$1"
_imgfile="$_script_home/$_osname.img"
if [ -z "$_osname" ]; then
echo "Usage: detachISO openbsd"
return 1
fi
echo "<disk type='file' device='disk'>
<driver name='qemu' type='raw'/>
<source file='$_imgfile' index='2'/>
<backingStore/>
<target dev='vda' bus='virtio'/>
<alias name='virtio-disk0'/>
<address type='pci' domain='0x0000' bus='0x04' slot='0x00' function='0x0'/>
</disk>" >remove-cdrom.xml
$_SUDO_VIR_ virsh detach-device "$_osname" --file remove-cdrom.xml --persistent
}
detachISO() {
_osname="$1"
if [ -z "$_osname" ]; then
echo "Usage: detachISO netbsd"
return 1
fi
if [ "$VM_ARCH" = "aarch64" ]; then
echo "<disk type='file' device='cdrom'>
<target dev='sda' bus='scsi'/>
</disk>" >remove-cdrom.xml
else
echo "<disk type='file' device='cdrom'>
<target dev='hdc' bus='ide'/>
</disk>" >remove-cdrom.xml
fi
$_SUDO_VIR_ virsh detach-device "$_osname" --file remove-cdrom.xml --persistent
}
attachISO() {
_osname="$1"
_iso="$2"
if [ -z "$_iso" ]; then
echo "Usage: attachISO netbsd netbsd.iso"
return 1
fi
echo "<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<target dev='hdc' bus='ide'/>
<readonly/>
<address type='drive' controller='0' bus='1' target='0' unit='0'/>
</disk>" >cdrom.xml
$_SUDO_VIR_ virsh attach-device "$_osname" --file cdrom.xml --persistent
$_SUDO_VIR_ virsh change-media "$_osname" hdc --insert --source "$_iso"
$_SUDO_VIR_ virsh dumpxml "$_osname" >dump.xml
sed -i "/<boot dev='hd'/i <boot dev='cdrom'/>" dump.xml
$_SUDO_VIR_ virsh define dump.xml
}
#img
_ocr() {
_ocr_img="$1"
if [ "$VM_OCR" == "py" ]; then
_ocrpy "$_ocr_img"
else
_ocrt "$_ocr_img"
fi
}
#img
_ocrt() {
_ocr_img="$1"
tesseract -l eng $_ocr_img - 2>/dev/null
}
#img
_ocrpy() {
_ocr_img="$1"
#pytesseract $_ocr_img
python3 -c "
import cv2,pytesseract,numpy,sys;
img = cv2.imread(sys.argv[1]);
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY);
gray, img_bin = cv2.threshold(gray,128,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU);
gray = cv2.bitwise_not(img_bin);
kernel = numpy.ones((2, 1), numpy.uint8);
img = cv2.erode(gray, kernel, iterations=1);
img = cv2.dilate(img, kernel, iterations=1);
out_below = pytesseract.image_to_string(img);
print(out_below);
" "$_ocr_img"
}
#osname [img]
screenText() {
_osname="$1"
_img="$2"
if [ -z "$_osname" ]; then
echo "Usage: screenText netbsd"
return 1
fi
CONSOLE_FILE="$_script_home/$_osname-$VM_RELEASE-console.log"
if [ -z "$VM_USE_CONSOLE_BUILD" ]; then
_png="${_img}"
if [ -z "$_img" ]; then
_png="$(mktemp).png"
echo "using _png=$_png"
fi
while ! vncdotool capture $_png >/dev/null 2>&1; do
#echo "screenText error, lets just wait"
sleep 3
done
sudo chmod 666 $_png
fi
if [ -z "$_img" ]; then
if [ -z "$VM_USE_CONSOLE_BUILD" ]; then
_ocr $_png
rm -rf $_png
else
tail -50 "$CONSOLE_FILE"
fi
else
if [ -z "$VM_USE_CONSOLE_BUILD" ]; then
_ocr $_png >screen.txt
else
tail -50 "$CONSOLE_FILE" >screen.txt
fi
echo "<!DOCTYPE html>
<html>
<head>
<title>$_osname $VM_RELEASE</title>
<meta http-equiv='refresh' content='1'>
</head>
<body onclick='stop()' style='background-color:grey;'>
<img src='screen.png' alt='Screen'>
<br>
<pre>
" >index.html
cat screen.txt >>index.html
echo '</pre></body></html>' >>index.html
fi
}
#osname text [timeout secs] [hook]
waitForText() {
_osname="$1"
_text="$2"
_sec="$3"
_hook="$4"
if [ -z "$_text" ]; then
echo "Usage: waitForText netbsd text"
return 1
fi
echo "Waiting for text: $_text"
_t=0
while [ -z "$_sec" ] || [ $_t -lt $_sec ]; do
sleep 3
screenText $_osname >_screenText.txt
echo ""
echo "==========screen Text============"
cat _screenText.txt
echo ""
echo "==========screen Text end============"
if grep -- "$_text" _screenText.txt; then
echo "====> OK, found: $_text"
return 0
elif [ "$DEBUG" ]; then
echo "Not found for text: $_text"
fi
_t=$((_t + 1))
$_hook
done
echo "Timeout for text: $_text"
return 0
}
#osname needOCR
startWeb() {
_osname="$1"
_needOCR="$2"
if [ -z "$_osname" ]; then
echo "Usage: startWeb netbsd"
return 1
fi
python3 -m http.server >/dev/null 2>&1 &
if ! [ -e "index.html" ]; then
echo "<!DOCTYPE html>
<html>
<head>
<title>$_osname</title>
<meta http-equiv='refresh' content='1'>
</head>
<body style='background-color:grey;'>
<h1>Please just wait....<h1>
</body>
</html>" >index.html
fi
if [ "$_needOCR" ]; then
(while true; do screenText "$_osname" "screen.png"; sleep 3; done)&
else
(while true; do $_SUDO_VIR_ virsh "$_osname" "screen.ppm"; convert "screen.ppm" "screen.png"; sleep 3; done)&
fi
}
exportOVA() {
_osname="$1"
_ova="$2"
if [ -z "$_ova" ]; then
echo "Usage: exportOVA netbsd netbsd.9.2.qcow2"
return 1
fi
_sor="$($_SUDO_VIR_ virsh domblklist $_osname | grep -E -o '/.*qcow2')"
echo "$_sor"
sudo zstd -c "$_sor" | split -b 2000M -d -a 1 - "$_ova.zst."
ls -lah
sudo mv "$_ova.zst.0" "$_ova.zst"
sudo chmod +r ${_ova}.zst*
}
#osname [_idfile]
addSSHHost() {
_osname="$1"
_idfile="$2"
if [ ! -e ~/.ssh/id_rsa ] ; then
ssh-keygen -f ~/.ssh/id_rsa -q -N ""
fi
_ip="$(getVMIP $_osname)"
echo "
Include config.d/*
StrictHostKeyChecking=accept-new
SendEnv CI GITHUB_*
" >>~/.ssh/config
mkdir -p ~/.ssh/config.d
echo "
Host $_osname
User root
HostName $_ip
" >~/.ssh/config.d/"$_osname.conf"
if [ "$_idfile" ]; then
echo " IdentityFile=$_idfile
" >>~/.ssh/config.d/"$_osname.conf"
fi
mkdir -p ~/.local/bin
echo "#!/usr/bin/env sh
ssh $_osname sh<\$1
">~/.local/bin/$_osname
chmod +x ~/.local/bin/$_osname
}
#pbk
addSSHAuthorizedKeys() {
_pbk="$1"
if [ -z "$_pbk" ]; then
echo "Usage: addSSHAuthorizedKeys id_rsa.pub"
return 1
fi
cat "$_pbk" >> $HOME/.ssh/authorized_keys
chmod 600 $HOME/.ssh/authorized_keys
}
#osname protocol hostPort vmPort
addNAT() {
_osname="$1"
_proto="$2"
_hostPort="$3"
_vmPort="$4"
if [ -z "$_vmPort" ]; then
echo "Usage: addNAT osname protocol hostPort vmPort"
return 1
fi
echo "addNAT not implemented"
}
#osname memsize
setMemory() {
_osname="$1"
_memsize="$2"
if [ -z "$_memsize" ]; then
echo "Usage: setMemory osname 2048"
return 1
fi
echo "setMemory not implemented"
}
#osname cpuCount
setCPU() {
_osname="$1"
_cpuCount="$2"
if [ -z "$_cpuCount" ]; then
echo "Usage: setCPU osname 3"
return 1
fi
echo "setCPU not implemented"
}
#osname
getVMIP() {
_osname="$1"
if [ -e "$HOME/$_osname.rebooted" ]; then
line=$(head -1 "$HOME/$_osname.rebooted")
if [ "$line" ]; then
printf -- "%s" "read ip from rebooted: $line" >&2
echo "$line"
return
fi
fi
if ! $_SUDO_VIR_ virsh net-dhcp-leases default | sort | grep "ipv4" | tail -1 | grep "$_osname" | grep -o -E '192.168.[0-9]*.[0-9]*'; then
$_SUDO_VIR_ virsh net-dhcp-leases default | sort | grep "ipv4" | tail -1 | grep -o -E '192.168.[0-9]*.[0-9]*'
fi
}
# input the file as shell script to execute
inputFile() {
_osname="$1"
_file="$2"
if [ -z "$_file" ]; then
echo "Usage: inputFile netbsd file.txt"
return 1
fi
if [ "$VM_USE_CONSOLE_BUILD" ]; then
cat "$_file" | nc -q 0 -l 64342 &
string "nc 192.168.122.1 64342 | sh"
input "$_osname" "enter"
else
vncdotool --force-caps --delay=100 typefile "$_file"
fi
}
#upload a local file into the remote VM
#osname local remote
uploadFile() {
_osname="$1"
_local="$2" #local file in the host machine.
_remote="$3" #remote file in the VM
if [ -z "$_osname" ]; then
echo "Usage: uploadFile openbsd local remote"
return 1
fi
export VM_OS_NAME=$_osname
if [ "$VM_USE_CONSOLE_BUILD" ]; then
cat "$_local" | nc -q 0 -l 64343 &
string "nc 192.168.122.1 64343 >$_remote"
input "$_osname" "enter"
else
string "cat - >$_remote"
input "$_osname" "enter"
inputFile "$_osname" "$_local"
ctrlD
fi
}
#keys splitted by ;
#eg: enter
#eg: down; enter
#eg: down; up; tab; enter
input() {
_osname="$1"
if [ -z "$_osname" ]; then
echo "Usage: input netbsd enter"
return 1
fi
shift
(
export VM_OS_NAME=$_osname
eval "$*"
)
}
string() {
_osname="$VM_OS_NAME"
if [ -z "$_osname" ]; then
_osname=$1
shift
fi
if [ -z "$_osname" ]; then
echo "Usage: string netbsd"
return 1
fi
if [ "$VM_USE_CONSOLE_BUILD" ]; then
CONSOLE_NAME="$_osname-$VM_RELEASE-console"
screen -S "$CONSOLE_NAME" -p 0 -X stuff "$1"
else
vncdotool --force-caps type "$1"
fi
}
#osname
space() {
_osname="${1:-$VM_OS_NAME}"
if [ -z "$_osname" ]; then
echo "Usage: enter netbsd"
return 1
fi
if [ "$VM_USE_CONSOLE_BUILD" ]; then
CONSOLE_NAME="$_osname-$VM_RELEASE-console"
screen -S "$CONSOLE_NAME" -p 0 -X stuff " "
else
vncdotool type ' '
fi
}
#osname
enter() {
_osname="${1:-$VM_OS_NAME}"
if [ -z "$_osname" ]; then
echo "Usage: enter netbsd"
return 1
fi
if [ "$VM_USE_CONSOLE_BUILD" ]; then
CONSOLE_NAME="$_osname-$VM_RELEASE-console"
screen -S "$CONSOLE_NAME" -p 0 -X stuff "\r"
else
vncdotool key enter
fi
}
#osname
tab() {
_osname="${1:-$VM_OS_NAME}"
if [ -z "$_osname" ]; then
echo "Usage: tab netbsd"
return 1
fi
if [ "$VM_USE_CONSOLE_BUILD" ]; then
CONSOLE_NAME="$_osname-$VM_RELEASE-console"
screen -S "$CONSOLE_NAME" -p 0 -X stuff $'\t'
else
vncdotool key tab
fi
}
#osname
f2() {
_osname="${1:-$VM_OS_NAME}"
if [ -z "$_osname" ]; then
echo "Usage: f2 netbsd"
return 1
fi
if [ "$VM_USE_CONSOLE_BUILD" ]; then
CONSOLE_NAME="$_osname-$VM_RELEASE-console"
screen -S "$CONSOLE_NAME" -p 0 -X stuff $'\e[12~'
else
vncdotool key f2
fi
}
#osname
f7() {
_osname="${1:-$VM_OS_NAME}"
if [ -z "$_osname" ]; then
echo "Usage: f7 netbsd"
return 1
fi
if [ "$VM_USE_CONSOLE_BUILD" ]; then
CONSOLE_NAME="$_osname-$VM_RELEASE-console"
screen -S "$CONSOLE_NAME" -p 0 -X stuff $'\e[18~'
else
vncdotool key f7
fi
}
#osname
f8() {
_osname="${1:-$VM_OS_NAME}"
if [ -z "$_osname" ]; then
echo "Usage: f8 netbsd"
return 1
fi
if [ "$VM_USE_CONSOLE_BUILD" ]; then
CONSOLE_NAME="$_osname-$VM_RELEASE-console"
screen -S "$CONSOLE_NAME" -p 0 -X stuff $'\e[19~'
else
vncdotool key f8
fi
}
#osname
down() {
_osname="${1:-$VM_OS_NAME}"
if [ -z "$_osname" ]; then
echo "Usage: down netbsd"
return 1
fi
if [ "$VM_USE_CONSOLE_BUILD" ]; then
CONSOLE_NAME="$_osname-$VM_RELEASE-console"
screen -S "$CONSOLE_NAME" -p 0 -X stuff $'\e[B'
else
vncdotool key down
fi
}
#osname
up() {
_osname="${1:-$VM_OS_NAME}"
if [ -z "$_osname" ]; then
echo "Usage: up netbsd"
return 1
fi
if [ "$VM_USE_CONSOLE_BUILD" ]; then
CONSOLE_NAME="$_osname-$VM_RELEASE-console"
screen -S "$CONSOLE_NAME" -p 0 -X stuff $'\e[A'
else
vncdotool key up
fi
}
#osname
ctrlD() {
_osname="${1:-$VM_OS_NAME}"
if [ -z "$_osname" ]; then
echo "Usage: up netbsd"
return 1
fi
if [ "$VM_USE_CONSOLE_BUILD" ]; then
CONSOLE_NAME="$_osname-$VM_RELEASE-console"
screen -S "$CONSOLE_NAME" -p 0 -X stuff $'\x04'
else
vncdotool key ctrl-d
fi