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
|
#!/usr/bin/env bash
#
# kvmx virtual machine manager
#
# Copyright (C) 2017 Silvio Rhatto - rhatto at riseup.net
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License,
# or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Basic parameters
VERSION="0.1.0"
BASENAME="`basename $0`"
DIRNAME="`dirname $0`"
ACTION="$1"
VM="$2"
GLOBAL_USER_CONFIG_FOLDER="$HOME/.config/kvmx"
GLOBAL_USER_CONFIG_FILE="$HOME/.config/kvmxconfig"
# Get the application base
function kvmx_app_base {
local dest
local base
# Determine if we are in a local or system-wide install.
if [ -h "$0" ]; then
dest="$(readlink $0)"
# Check again as the caller might be a symlink as well
if [ -h "$dest" ]; then
base="`dirname $dest`"
dest="$(dirname $(readlink $dest))"
else
base="`dirname $0`"
dest="`dirname $dest`"
fi
# Deal with relative or absolute links
if [ "`basename $dest`" == "$dest" ]; then
APP_BASE="$base"
else
APP_BASE="$dest"
fi
else
APP_BASE="`dirname $0`"
fi
echo $APP_BASE
}
# Build a SSH command
function __kvmx_ssh_command {
# See http://blog.djm.net.au/2013/11/chacha20-and-poly1305-in-openssh.html
SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=FATAL -o ProxyCommand=none -o Ciphers=chacha20-poly1305@openssh.com -i $1"
}
# Create a guest entry at the global user config folder
function __kvmx_create_config_entry {
if [ -z "$FOLDER" ]; then
return 1
fi
( cd $GLOBAL_USER_CONFIG_FOLDER && ln -s $FOLDER/kvmxfile $VM )
}
# Initialize
function __kvmx_initialize {
if [ "$ACTION" == "app_base" ]; then
return
fi
# Load basic functions
export APP_BASE="`$DIRNAME/kvmx app_base`"
source $APP_BASE/lib/kvmx/functions || exit 1
# Alias to be used in config files
KVMX_BASE="$APP_BASE"
# Stop processing here for some actions
if [ "$ACTION" == "init" ] || [ "$ACTION" == "list" ]; then
return
fi
if [ -z "$VM" ]; then
VM="$(basename `pwd`)"
fi
# Default parameters
PORT="$(($RANDOM + 1024))"
SSH="$(($PORT + 22))"
GUEST_DISPLAY="$(((RANDOM % 10) + 1))"
# Initalize
mkdir -p $GLOBAL_USER_CONFIG_FOLDER
# Load user config
if [ -e "$GLOBAL_USER_CONFIG_FILE" ]; then
source $GLOBAL_USER_CONFIG_FILE
fi
# Load and check guest config
if [ "$ACTION" != "ls" ] && [ "$ACTION" != "edit" ] && [ "$ACTION" != "usage" ]; then
if [ ! -e "$GLOBAL_USER_CONFIG_FOLDER/$VM" ]; then
if [ -e "kvmxfile" ]; then
# Existing kvmxfile but not registered at the global user config
FOLDER="$(pwd)"
__kvmx_create_config_entry
else
echo "$BASENAME: config not found: $GLOBAL_USER_CONFIG_FOLDER/$VM"
exit 1
fi
else
source $GLOBAL_USER_CONFIG_FOLDER/$VM
fi
if [ -z "$image" ]; then
if [ -z "$image_base" ]; then
image_base="$HOME/.local/share/kvmx"
fi
image="$image_base/$VM/box.img"
fi
# Box and folder config
KVMXFILE="`readlink $GLOBAL_USER_CONFIG_FOLDER/$VM`"
KVMX_PROJECT_FOLDER="`dirname $KVMXFILE`"
STORAGE="`dirname $image`"
STATE_DIR="$STORAGE/state/$VM"
LOG_DIR="$STORAGE/log"
PIDFILE="$STATE_DIR/pid"
PORTFILE="$STATE_DIR/port"
SSHFILE="$STATE_DIR/ssh"
DISPLAYFILE="$STATE_DIR/display"
SPICEFILE="$STATE_DIR/spice"
LOGFILE="$LOG_DIR/qemu"
SPICELOG="$LOG_DIR/spice"
XPRALOG="$LOG_DIR/xpra"
if [ -e "$STORAGE/ssh/$VM.key" ]; then
mkdir -p "$STORAGE/ssh"
SSHKEY="$STORAGE/ssh/$VM.key"
else
SSHKEY="$APP_BASE/share/ssh/insecure_private_key"
fi
if [ ! -z "$user" ]; then
SSH_LOGIN="$user"
else
SSH_LOGIN="user"
fi
__kvmx_ssh_command $SSHKEY
mkdir -p $STATE_DIR $LOG_DIR
if [ ! -e "$image" ] && [ "$ACTION" != "up" ] && [ "$ACTION" != "purge" ] && [ "$ACTION" != "destroy" ]; then
echo "$BASENAME: file not found: $image"
exit 1
fi
fi
}
# Run spice client
function kvmx_spice {
if ! kvmx_running; then
echo "$BASENAME: guest $VM is not running"
exit 1
fi
# Ensure we have the right port configuration: we can also be
# running directly from command line.
PORT="`cat $PORTFILE`"
if [ -z "$PORT" ]; then
echo "$BASENAME: cannot get spice port for $VM."
exit 1
fi
# https://lists.freedesktop.org/archives/spice-devel/2013-September/014643.html
SPICE_NOGRAB=1 spicec --host localhost --port $PORT &> $SPICELOG &
#spicy -h localhost -p $PORT
#remote-viewer spice://localhost:$PORT
SPICEPID="$!"
echo "$SPICEPID" > $SPICEFILE
# Give time to connect
sleep 5
# Fix window titles
if which /usr/bin/xdotool &> /dev/null; then
xdotool search --name "SPICEc:0" set_window --name $VM
fi
}
# Bring virtual machine up
function kvmx_up {
if kvmx_suspended; then
$DIRNAME/$BASENAME resume $VM
if [ "$run_spice_client" == "1" ]; then
$DIRNAME/$BASENAME spice $VM
fi
exit
elif kvmx_running; then
echo "$BASENAME: guest $VM is already running"
exit 1
fi
if [ ! -z "$shared_folder" ]; then
# Get absolute path of shared folder relative to project path
shared_folder="`cd $KVMX_PROJECT_FOLDER && cd $shared_folder &> /dev/null && pwd`"
# Requires samba package installed in the host; see http://unix.stackexchange.com/a/183609
#local shared="-net user,smb=$shared_folder"
# See http://wiki.qemu-project.org/Documentation/9psetup
local shared="-fsdev local,id=shared,path=$shared_folder,security_model=none -device virtio-9p-pci,fsdev=shared,mount_tag=shared"
fi
if [ ! -z "$port_mapping" ]; then
local hostfwd=",$port_mapping"
fi
# Check if image exists, create otherwise
if [ ! -e "$image" ]; then
if [ ! -z "$basebox" ]; then
if [ -e "$GLOBAL_USER_CONFIG_FOLDER/$basebox" ]; then
baseimage="`kvmx list_image $basebox`"
basekey="`dirname $baseimage`/ssh/`basename $baseimage .img`.key"
if [ ! -e "$baseimage" ]; then
echo "$BASENAME: could not find basebox $baseimage. Please create it first."
exit 1
fi
echo "Copying base image $baseimage to $image..."
cp $baseimage $image
if [ -e "$basekey" ]; then
imagekey="`dirname $image`/ssh/`basename $image .img`.key"
mkdir "`dirname $image`/ssh"
cp $basekey $imagekey
cp $basekey.pub $imagekey.pub
# Re-evaluate this if there's a custom SSH key.
__kvmx_ssh_command $basekey
fi
local wait="y"
fi
else
local wait="y"
kvmx-create $GLOBAL_USER_CONFIG_FOLDER/$VM
fi
if [ "$wait" == "y" ]; then
echo "Waiting before starting the new guest..."
sleep 5
fi
fi
if [ -z "$graphics" ]; then
graphics="-vga qxl"
fi
# Run virtual machine
# See https://en.wikipedia.org/wiki/Nohup#Overcoming_hanging
nohup kvm -m 2048 -name $VM -drive file=$image,if=virtio $graphics $shared \
-spice port=$PORT,addr=127.0.0.1,disable-ticketing,streaming-video=off,jpeg-wan-compression=never,playback-compression=off,zlib-glz-wan-compression=never,image-compression=off \
-device virtio-serial-pci \
-device virtserialport,chardev=spicechannel0,name=com.redhat.spice.0 \
-chardev spicevmc,id=spicechannel0,name=vdagent \
-smp 2 -soundhw ac97 -cpu host -balloon virtio \
-net nic,model=virtio \
-net user,hostfwd=tcp:127.0.0.1:$SSH-:22$hostfwd &> $LOGFILE < /dev/null &
PID="$!"
# Save state
echo $PID > $PIDFILE
echo $PORT > $PORTFILE
echo $SSH > $SSHFILE
echo $GUEST_DISPLAY > $DISPLAYFILE
if [ "$run_spice_client" == "1" ]; then
kvmx_spice
fi
let ssh_attempts="0"
echo -n "Waiting for machine to boot..."
while true; do
echo true | $SSH_COMMAND -o ConnectTimeout=2 -o NumberOfPasswordPrompts=0 -p $SSH $SSH_LOGIN@127.0.0.1 &> /dev/null && break
echo -n "."
let ssh_attempts++
if [ "$ssh_attempts" == "20" ]; then
echo "$BASENAME: timeout or access denied when trying to SSH into $VM."
echo "$BASENAME: please check if the image is in a good state and if it accepts passwordless ssh connections"
kvmx_stop
exit 1
fi
sleep 2
done
echo " done."
#sleep 5
#echo ""
# Somehow it is starting before DBUS and then crashing, so we try to start again
echo "Ensure spice-vdagent is running..."
echo "sudo /usr/sbin/service spice-vdagent start" | kvmx_ssh
kvmx_hostname
if [ ! -z "$shared_folder" ] && [ ! -z "$shared_folder_mountpoint" ]; then
echo "Mounting $shared_folder on $shared_folder_mountpoint on guest..."
echo "sudo mkdir -p $shared_folder_mountpoint" | kvmx_ssh
echo "sudo mount -t 9p -o trans=virtio shared $shared_folder_mountpoint -oversion=9p2000.L,posixacl,cache=loose" | kvmx_ssh
#echo "sudo mount //10.0.2.4/qemu $shared_folder_mountpint" | kvmx_ssh
fi
kvmx_status
}
# Set hostname
function kvmx_hostname {
if ! kvmx_running; then
echo "$BASENAME: guest $VM is not running"
exit 1
fi
echo "Setting hostname..."
$SSH_COMMAND -o ConnectTimeout=2 -p $SSH $SSH_LOGIN@127.0.0.1 <<EOF
##### BEGIN REMOTE SCRIPT #####
OLD_HOST="\$(hostname)"
# Set hosts entry
if ! grep -q "^127.0.0.1 $hostname.$domain $hostname$" /etc/hosts; then
echo "127.0.0.1 $hostname.$domain $hostname" | sudo tee -a /etc/hosts > /dev/null
fi
echo "$hostname.$domain" | sudo tee /etc/hostname > /dev/null
sudo hostname $hostname.$domain
# Remove old hostname from hosts file
if [ "\$OLD_HOST" != "$hostname.$domain" ]; then
if grep -q \$OLD_HOST /etc/hosts; then
sudo sed -i -e "/\$OLD_HOST/d" /etc/hosts
fi
fi
##### END REMOTE SCRIPT #######
EOF
}
# Display usage
function kvmx_usage {
echo "$BASENAME $VERSION - virtual machine manager"
echo ""
echo "usage: $BASENAME <action> [options]"
echo ""
echo "available actions:"
echo ""
grep "^function kvmx_" $0 | cut -d ' ' -f 2 | sed -e 's/kvmx_//' | sort | xargs -L 6 | column -t -c 6 | sed -e 's/^/\t/'
echo ""
echo "examples:"
echo ""
echo -e "\t$BASENAME list"
echo -e "\t$BASENAME init <machine> [folder]"
echo -e "\t$BASENAME clone <orig-guest> <dest-folder>"
echo ""
local list="`kvmx_list`"
if [ ! -z "$list" ]; then
echo "available virtual machines:"
echo ""
echo "$list" | sed -e 's/^/\t/'
echo ""
fi
exit 1
}
# Log into the guest using SSH
function kvmx_ssh {
if ! kvmx_running; then
echo "$BASENAME: guest $VM is not running"
exit 1
fi
if [ "$ssh_support" != "y" ]; then
echo "$BASENAME: SSH support for $VM is disabled"
exit 1
fi
# Shift params according to how the program was called:
# either "kvmx ssh" or "kvmx ssh guest".
if [ "$ACTION" == "ssh" ]; then
if [ ! -z "$2" ]; then
shift 2
else
shift 1
fi
fi
SSH="`cat $SSHFILE`"
$SSH_COMMAND -p $SSH $SSH_LOGIN@127.0.0.1 $*
}
# Suspend the virtual machine
function kvmx_suspend {
if ! kvmx_running; then
echo "$BASENAME: guest $VM is not running"
exit 1
fi
PID="`cat $PIDFILE`"
kill -STOP $PID
}
# Check if a guest is running
function kvmx_running {
if [ ! -e "$PIDFILE" ]; then
return 1
fi
PID="`cat $PIDFILE`"
if [ -z "$PID" ]; then
return 1
fi
ps $PID &> /dev/null
return $?
}
# Check if a guest is running
function kvmx_suspended {
if ! kvmx_running; then
return 1
else
if ps -p $PID -o stat --no-headers | grep -q 'Tl'; then
return 0
else
return 1
fi
fi
}
# Resume the guest
function kvmx_resume {
if ! kvmx_running; then
echo "$BASENAME: guest $VM is not running"
exit 1
fi
PID="`cat $PIDFILE`"
kill -CONT $PID
}
# Poweroff the guest
function kvmx_poweroff {
if ! kvmx_running; then
echo "$BASENAME: guest $VM is not running"
exit 1
fi
echo /usr/bin/sudo poweroff | kvmx_ssh &> /dev/null
sleep 3
kvmx_status
}
# Alias for poweroff
function kvmx_down {
kvmx_poweroff
}
# Hibernate
function kvmx_hibernate {
if ! kvmx_running; then
echo "$BASENAME: guest $VM is not running"
exit 1
fi
echo "which s2disk &> /dev/null && /usr/bin/sudo s2disk" | kvmx_ssh &> /dev/null
echo "Checking if hibernation was successful..."
sleep 3
if kvmx_running; then
echo "Unable to hibernate guest: please check guest configuration"
exit 1
else
kvmx_status
fi
}
# Reboot the guest
function kvmx_reboot {
if ! kvmx_running; then
echo "$BASENAME: guest $VM is not running"
exit 1
fi
echo /usr/bin/sudo reboot | kvmx_ssh &> /dev/null
sleep 3
kvmx_status
}
# Rsync files to the guest
function kvmx_rsync {
if ! kvmx_running; then
echo "$BASENAME: guest $VM is not running"
exit 1
fi
ORIG="$3"
DEST="$4"
SSH="`cat $SSHFILE`"
rsync -av -e "$SSH_COMMAND -o Port=$SSH" --rsync-path "sudo rsync" $ORIG/ $SSH_LOGIN@127.0.0.1:$DEST/
}
# List guests
function kvmx_list {
if [ -e "$GLOBAL_USER_CONFIG_FOLDER" ]; then
ls -1 $GLOBAL_USER_CONFIG_FOLDER | xargs -L 6 | column -t -c 6
fi
}
# Alias to list command
function kvmx_ls {
kvmx_list
}
# Upgrade guest
function kvmx_upgrade {
echo "sudo apt-get update && sudo apt-get dist-upgrade -y && sudo apt-get autoremove -y" | kvmx_ssh
}
# Initializes a new guest
function kvmx_init {
FOLDER="$3"
if [ -z "$FOLDER" ]; then
if [ -z "$VM" ]; then
VM="$(basename `pwd`)"
FOLDER="$(dirname `pwd`)/$VM"
else
FOLDER="$(pwd)/$VM"
fi
fi
if [ -e "$GLOBAL_USER_CONFIG_FOLDER/$VM" ]; then
echo "$BASENAME: guest $VM already exists"
exit 1
fi
if [ ! -d "$FOLDER" ]; then
mkdir -p $FOLDER
fi
# Ensure we have an absolute folder name
FOLDER="`cd $FOLDER &> /dev/null && pwd`"
# Copy config from template
if [ ! -e "$FOLDER/kvmxfile" ]; then
cp $APP_BASE/kvmxfile $FOLDER/
sed -i -e "s|hostname=\"machine\"|hostname=\"$VM\"|g" $FOLDER/kvmxfile
fi
# Create config entry
__kvmx_create_config_entry
}
# Clone a guest
function kvmx_clone {
if kvmx_running; then
echo "$BASENAME: orig $VM is running, cannot clone."
exit 1
fi
FOLDER="$3"
DEST="`basename $FOLDER`"
if [ -z "$FOLDER" ]; then
kvmx_usage
fi
# Check if dest machine exists
if [ -e "$GLOBAL_USER_CONFIG_FOLDER/$DEST" ]; then
echo "$BASENAME: destination guest $DEST already exists."
exit 1
fi
if [ -d "$FOLDER" ]; then
echo "$BASENAME: destination $FOLDER already exists."
exit 1
fi
# Ensure we have an absolute folder name
mkdir -p $FOLDER
FOLDER="`cd $FOLDER &> /dev/null && pwd`"
rmdir $FOLDER
# Copy image and configuration
cp -r `dirname $image` $FOLDER/
( cd $GLOBAL_USER_CONFIG_FOLDER && ln -s $FOLDER/kvmxfile $DEST )
# Update config file
new_image="$FOLDER/`basename $image`"
sed -i -e "s|image=\"$image\"|image=\"$new_image\"|g" $GLOBAL_USER_CONFIG_FOLDER/$DEST
sed -i -e "s|hostname=\"$VM\"|hostname=\"$DEST\"|g" $GLOBAL_USER_CONFIG_FOLDER/$DEST
}
# Edit guest config
function kvmx_edit {
if [ -z "$EDITOR" ]; then
EDITOR="vi"
fi
if [ -e "$GLOBAL_USER_CONFIG_FOLDER/$VM" ]; then
$EDITOR $GLOBAL_USER_CONFIG_FOLDER/$VM
else
echo "$BASENAME: $GLOBAL_USER_CONFIG_FOLDER/$VM: file not found."
fi
}
# Stop a guest
function kvmx_stop {
if kvmx_running; then
PID="`cat $PIDFILE`"
kill $PID
fi
}
# Kill a guest
function kvmx_kill {
if kvmx_running; then
PID="`cat $PIDFILE`"
kill -9 $PID
fi
}
# Destroy a guest
function kvmx_destroy {
kvmx_stop
rm -f $image
rm -rf $STATE_DIR
echo "$BASENAME: removed image and state files, but not the whole `dirname $image` folder."
}
# Shred a guest
function kvmx_shred {
kvmx_stop
if which shred &> /dev/null; then
shred $image
rm -f $image
else
echo "$BASENAME: error shreding $image: shred program not available."
exit 1
fi
}
# Wipe a guest
function kvmx_wipe {
kvmx_stop
if which wipe &> /dev/null; then
wipe -f $image
rm -f $image
else
echo "$BASENAME: error wipeing $image: wipe program not available."
exit 1
fi
}
# Purge a guest and all its configuration
function kvmx_purge {
kvmx_destroy
rm -f $GLOBAL_USER_CONFIG_FOLDER/$VM
echo "$BASENAME: removed $GLOBAL_USER_CONFIG_FOLDER/$VM config."
}
# Provision a machine
function kvmx_provision {
if ! kvmx_running; then
echo "$BASENAME: guest $VM is not running"
exit 1
fi
if [ -z "$provision_command" ]; then
echo "$BASENAME: error: parameter provision_command is not configured for $VM."
exit 1
fi
if [ ! -z "$provision_rsync" ]; then
SSH="`cat $SSHFILE`"
ORIG="`echo $provision_rsync | cut -d ' ' -f 1`"
DEST="`echo $provision_rsync | cut -d ' ' -f 2`"
echo "Syncing provision files into the guest..."
echo "sudo mkdir -p `dirname $DEST`" | kvmx_ssh
rsync -av -e "$SSH_COMMAND -o Port=$SSH" --rsync-path "sudo rsync" $ORIG/ $SSH_LOGIN@127.0.0.1:$DEST/
fi
echo "Running provision command inside the guest..."
echo "$provision_command $hostname $domain $mirror" | kvmx_ssh
}
# Print guest image file name
function kvmx_list_image {
echo $image
}
# Print guest status
function kvmx_status {
if kvmx_running; then
echo "$BASENAME: $VM guest is running"
PID="`cat $PIDFILE`"
ps $PID
else
echo "$BASENAME: $VM guest is stopped"
fi
}
# Print guest log
function kvmx_log {
local logs=""
if [ -s "$LOGFILE" ]; then
logs="$logs $LOGFILE"
fi
if [ -s "$SPICELOG" ]; then
logs="$logs $SPICELOG"
fi
if [ -s "$XPRALOG" ]; then
logs="$logs $XPRALOG"
fi
tail -F $logs
}
# Rotate SSH keys
function kvmx_rotate_sshkeys {
# Generate new keypair
mkdir -p "$STORAGE/ssh"
SSHKEY="$STORAGE/ssh/$VM.key"
__kvmx_ssh_keygen $SSHKEY.new "$user@`basename $image .img`"
# Replace pubkey on server
echo "touch ~/.ssh/authorized_keys.new && chmod 600 ~/.ssh/authorized_keys.new" | kvmx_ssh
cat $SSHKEY.new.pub | kvmx_ssh "tee ~/.ssh/authorized_keys.new &> /dev/null"
echo "mv ~/.ssh/authorized_keys.new ~/.ssh/authorized_keys" | kvmx_ssh
# Replace keypair locally
mv $SSHKEY.new $SSHKEY
mv $SSHKEY.new.pub $SSHKEY.pub
}
# Xpra integration
function kvmx_xpra {
if ! which xpra &> /dev/null; then
echo "$BASENAME: please install xpra package"
exit 1
fi
local action="$3"
shift 3
SSH="`cat $SSHFILE`"
if [ -z "$action" ]; then
action="start"
fi
if [ "$action" == "start" ] || [ "$action" == "attach" ]; then
nohup xpra $action --ssh="$SSH_COMMAND -p $SSH" ssh:$SSH_LOGIN@127.0.0.1 $* &> $XPRALOG < /dev/null &
else
xpra $action --ssh="$SSH_COMMAND -p $SSH" ssh:$SSH_LOGIN@127.0.0.1 $*
fi
}
# Alias for up command
function kvmx_start {
kvmx_up $*
}
# Alias for up command
function kvmx_run {
kvmx_up $*
}
# Connect to the guest using VNC
function kvmx_vnc {
if ! kvmx_running; then
echo "$BASENAME: guest $VM is not running"
exit 1
fi
if [ -z "$vnc_client" ]; then
vnc_client="virt-viewer"
fi
if which $vnc_client &> /dev/null; then
if [ "$vnclient_client" == "virt-viewer" ]; then
$vnc_client vnc://127.0.0.1:$GUEST_DISPLAY
else
GUEST_DISPLAY="`cat $DISPLAYFILE`"
$vnc_client :$GUEST_DISPLAY
fi
else
echo "$BASENAME: no vnc_client configured"
exit 1
fi
}
# Dispatch
if type kvmx_$ACTION 2> /dev/null | grep -q 'function'; then
__kvmx_initialize
kvmx_$ACTION $*
else
kvmx_usage
fi
|