forked from vpenso/libvirt-shell-functions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm_functions.sh
1023 lines (946 loc) · 30.7 KB
/
vm_functions.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
#---------------------------------------------------------------
#
# Shell functions for working with virtual machines.
#
# This 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 (at your option) 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/>.
#
#----------------------------------------------------------------
# Author: Victor Penso
# Copyright 2011-2012
_version=1.4
#----------------------------------------------------------------
# enable line numbers for debug output
if [ "$_DEBUG" = "true" ]
then
# no double quotes here!
export PS4='(${BASH_SOURCE}:${LINENO}):${FUNCNAME[0]}-[${SHLVL},${BASH_SUBSHELL},$?] '
fi
# print log output only in debugging mode
function _log() {
if [ "$_DEBUG" = "true" ]; then
echo 1>&2 "debug: $@"
fi
}
# alert the user about a problem
function _error() {
echo 1>&2 "ERROR: $@"
}
# ----------------------------------------------------------------------
##
## Default for environments variables
##
# Users can overwrite these variables to adjust the functions
# to their environment!
# Where the virtual machines get stored
KVM_VM_INSTANCES=${KVM_VM_INSTANCES:-"/srv/vms/instances"}
# Golden images used to clone virtual machines from
KVM_GOLDEN_IMAGES=${KVM_IMAGES:-"/srv/vms/images"}
# Remote location for golden images
if [ -z "$KVM_REMOTE_IMAGES" ]; then
echo "Please set '\$KVM_REMOTE_IMAGES' to use remote VM images!"
fi
# Network configuration to libvirt
VIRSH_NET_CONFIG=${VIRSH_NET_CONFIG:-"/srv/vms/_config/libvirt_nat_bridge.xml"}
# Network domain
VIRSH_NET_CONFIG_DOMAIN=".devops.test"
export KVM_VM_INSTANCES KVM_GOLDEN_IMAGES
if [ ! -d $KVM_GOLDEN_IMAGES ]
then
mkdir $KVM_GOLDEN_IMAGES
_log "Create image directory: $KVM_GOLDEN_IMAGES"
fi
if [ ! -d $KVM_VM_INSTANCES ]
then
mkdir --mode 777 $KVM_VM_INSTANCES
_log "Create instance directory: $KVM_VM_INSTANCES"
fi
# Unique directory for various purposes
SPOOL=$HOME/.vm_functions
mkdir --parents $SPOOL
# ----------------------------------------------------------------------
##
## Prepare the host-internal network
##
# appends the domain name if it is missing
function __vm_fqdn() {
local _instance=$1
# if no parameter is appended return nothing
if [ ! -z $_instance ]; then
# check if the domain name is part of the instance name
if ! `echo $_instance | grep -q "$VIRSH_NET_CONFIG_DOMAIN"`; then
# append the domain name if missing
echo $_instance$VIRSH_NET_CONFIG_DOMAIN
_log "[__vm_fqdn] Appending domain name to '$_instance'."
else
echo $_instance
fi
fi
}
function __vm_ip() {
local _help=\
"Prints the IP-address of a virtual machine instance,
by reading the network configuration in:
\$VIRSH_NET_CONFIG
Returns the IP-address associated with a given hostname,
unless no parameter is applied. Then the IP-address of
the virtual machine in the current working directory is
returned if possible. In case no IP-address can be found
the function returns nothing."
if [ "$1" = "help" ]; then
echo $_help
else
local _instance=
if $(__vm_container_directory); then
_instance=$(__vm_name)
else
_instance=$(__vm_fqdn $1)
fi
if [ ! -z $_instance ]; then
_log "[__vm_ip] Looking for IP-address of '$_instance'."
xmlstarlet sel -t -m "//network/ip/dhcp" \
-v "host[@name='$_instance']/@ip" $VIRSH_NET_CONFIG
fi
fi
}
function __vm_mac() {
local _help=\
"Prints the MAC-address of a virtual machine instance,
by reading the network configuration in:
\$VIRSH_NET_CONFIG
Returns the MAC-address associated with a given hostname,
unless no parameter is applied. Then the MAC-address of
the virtual machine in the current working directory is
returned if possible. In case no MAC-address can be found
the function returns nothing."
if [ "$1" = "help" ]; then
echo $_help
else
local instance=
if $(__vm_container_directory); then
_instance=$(__vm_name)
else
_instance=$(__vm_fqdn $1)
fi
if [ ! -z $_instance ]; then
_log "[__vm_mac] Locking for MAC-address of '$_instance'."
xmlstarlet sel -t -m "//network/ip/dhcp" \
-v "host[@name='$_instance']/@mac" $VIRSH_NET_CONFIG
fi
fi
}
function __vm_network() {
local _help=\
"Manage the host-internal (NATed) network used by all
virtual machine instance.
vm network status|start|stop|lookup
The command 'lookup' list all FQDNs and their IPs known
to the internal DNS server."
local _command=$1
local _bn='nat_bridge'
case "$_command" in
status) virsh net-info $_bn ;;
start)
# define a persistent host-internal network
virsh net-define $VIRSH_NET_CONFIG > /dev/null 2>&1
_log "[__vm_network] Defining network from '$VIRSH_NET_CONFIG'."
virsh net-start $_bn > /dev/null 2>&1
_log "[__vm_network] Starting network '$_bn'."
# make it boot-persistent, also
virsh net-autostart $_bn > /dev/null 2>&1
_log "[__vm_network] Making network '$_bn' boot persistent."
echo "Network '$_bn' started"
;;
stop)
virsh net-destroy $_bn > /dev/null 2>&1
_log "[__vm_network] Shutdown network '$_bn'."
virsh net-undefine $_bn > /dev/null 2>&1
_log "[__vm_network] Un-defining network '$_bn'."
echo "Network '$_bn' stopped"
;;
lookup)
xmlstarlet sel -t -m "//network/ip/dhcp/host" \
-v "@ip" -o " " -v "@name" -n $VIRSH_NET_CONFIG
;;
*) echo $_help ;;
esac
}
# ----------------------------------------------------------------------
##
## Access the virtual machine meta data
##
function __vm_name() {
if [[ -e $PWD/libvirt_instance.xml ]]; then
xmlstarlet sel -t -v "/domain/name" $PWD/libvirt_instance.xml
fi
}
function __vm_id() {
#virsh list --all | awk '{gsub(/^ +| +$/,"")}1' | grep $(__vm_name) | cut -d' ' -f2
virsh list --all | awk '/'$(__vm_name)'/ {gsub(/^[ ]*/,""); print $1}'
}
# ----------------------------------------------------------------------
##
## Accessing a virtual machine instance
##
function __vm_ssh() {
if [ ! $# -eq 0 ]
then
_log "[__vm_ssh] Executing command '$@'."
fi
ssh -qt -F $PWD/ssh_config instance $@;
}
function __vm_login() {
local _instance=$1
# if the user explicitly defines a virtual machine
# instance to login
if [ ! -z $_instance ]; then
# make sure to use the FQDN of the defined instance
_instance=$(__vm_fqdn $_instance)
_log "[__vm_login] Changing to work-directory of $_instance"
# change to the container directory of the instance
cd $KVM_VM_INSTANCES/$_instance
fi
# otherwise it is assumed that $PWD is an instance
# container directory
__vm_ssh
}
function __vm_put() {
local _help=\
"Usage: vm put <local_path> <instance_path>
Upload a file into a running virtual machine instance.
First parameter is the path to a local file, and the
second is the path inside the virtual machine file-system."
# do we have exactly two arguments?
if [ $# -ne 2 ]; then
_error "Not enough arguments!"
echo $_help
else
# copy the file
local _source=$1
local _destination=$2
_log "[__vm_put] Uploading file '$_source' to '$(__vm_name):$_destination'."
scp -q -r -F $PWD/ssh_config $_source instance:$_destination
fi
}
function __vm_get() {
local _help=\
"Usage: vm get <instance_path> <local_path>
Download a file from the virtual machine instance.
First parameter is the path inside the virtual
machine file-system, second the path to the local
file."
if [ $# -ne 2 ]; then
_error "Broken arguments!"
echo $_help
else
local _source=$1
local _destination=$2
_log "[__vm_get] Downloading file '$(__vm_name):$_source' to '$_destination'."
scp -q -r -F $PWD/ssh_config instance:$_source $_destination
fi
}
function __vm_sync() {
local _help=\
"Usage: vm sync <local_dir> <instance_dir>
Recursive upload a directory to the virtual machine
instance. First parameter is the path to the local
directory, and second parameter is the path in the
virtual machine instance file-system. After the first
upload a differential sync is done."
if [ $# -ne 2 ]; then
_error "Broken arguments!"
echo $_help
else
local _source=$1
local _destination=$2
_log "[__vm_sync] Syncing '$_source' to '$(__vm_name):$_destination'."
rsync --exclude '.git' --exclude '.gitignore' --omit-dir-times \
--recursive --copy-links --copy-dirlinks --verbose \
-e "ssh -q -F $PWD/ssh_config" $_source instance:$_destination 2>/dev/null
fi
}
# use SSHFS to mount the virtual machine instance root-directory
function __vm_fs() {
local _help=\
"Usage: vm fs mount|umount
Mount/unmount the virtual machine instance file-system.
The mount-point 'mnt/' will be connected with root
privileges."
# check for the name of the SFTP-service
local _command=$1
local _sftp_path=$(vm exec "sudo grep Subsystem /etc/ssh/sshd_config | cut -d ' ' -f 3 | tr -d '\n'" )
# make sure to have a mount-point
mkdir $PWD/mnt
case "$_command" in
mount)
_log "Mounting virtual machine file-system."
sshfs -F $PWD/ssh_config -o sftp_server="/usr/bin/sudo $_sftp_path" instance:/ $PWD/mnt/
;;
umount)
_log "Unmounting virtual machine file-system."
fusermount -u $PWD/mnt/
;;
*)
_error "The command '$_command' is not supported!"
echo $_help
;;
esac
}
# ----------------------------------------------------------------------
##
## Managing the virsh configuration
##
# Adjust the virtual machine configuration files:
#
# * libvirtd_instance.xml
# * ssh_config
#
# to the current directory.
function __vm_reloc() {
local _template=$1
# change the SSH client configuration file
input=" IdentityFile $PWD/keys/id_rsa";
sed "s|^ Iden.*|$input|g" ssh_config > $SPOOL/dump.txt;
mv $SPOOL/dump.txt ssh_config;
_log "[__vm_reloc] Configuring SSH key location in '$PWD/ssh_config'"
# change the libvirt configuration file
local _hostname=${PWD##*/}
# Extract the disk image source path
local _path=$(grep -o '/.*/disk.img' libvirt_instance.xml | head -1)
# Remove the node specific path suffix
_path=${_path%/*/*}
# Replace the node names and the paths to the disk images
sed -e "s|$_path|$KVM_VM_INSTANCES|" \
-e "s|$_template|$_hostname|" libvirt_instance.xml > $SPOOL/dump.txt ;
mv $SPOOL/dump.txt libvirt_instance.xml;
_log "[__vm_reloc] Configuring '$PWD/libvirt_instance.xml'"
}
# Change the virtual machine MAC- and IP-address, e.g.:
#
# __vm_network_config 02:FF:0A:0A:06:04 10.1.1.4
#
function __vm_network_config() {
local _dump=/tmp/dump.xml
# change the IP-address in the SSH configuration
_log "[__vm_network_config] Configuring IP-address in '$PWD/ssh_config'."
input=" HostName $2"
sed "s|^ HostNam.*|$input|g" ssh_config > $_dump;
mv $_dump ssh_config;
# change mac-address in the libvirt configuration file
_log "[__vm_network_config] Configuring MAC-address in '$PWD/libvirt_instance.xml'"
xmlstarlet ed \
-u "/domain/devices/interface/mac[@address]/@address" -v "$1" \
libvirt_instance.xml > $_dump;
mv $_dump libvirt_instance.xml;
}
function __vm_hostname() {
local _fqdn=$1
local _name=`echo $_fqdn | cut -d. -f1`
local _ip=$2
__vm_ssh "sudo sh -c 'echo $_name > /etc/hostname' " > /dev/null 2>&1
__vm_ssh "sed '2 c $_ip $_fqdn $_name' /etc/hosts > /tmp/dump.txt" > /dev/null 2>&1
__vm_ssh "sudo sh -c 'mv /tmp/dump.txt /etc/hosts'" > /dev/null 2>&1
__vm_ssh "sudo sh -c '/etc/init.d/hostname.sh'" > /dev/null 2>&1
_log "[__vm_hostname] Configuring instance name $_name using SSH login."
}
# --------------------------------------------------------------
##
## Access the network configuration for a particular
## virtual machine.
##
# Port-forwarding of a host port to a virtual machine instance port.
function __vm_port_forward() {
local _ip_space="10.1.1"
case "$1" in
list)
echo "NAT rules:"
sudo iptables -L -n -t nat | grep $_ip_space
echo "Forwarding:"
sudo iptables -L FORWARD -n | grep $_ip_space
;;
add)
local _name=`echo $2 | cut -d: -f1`
vm cd $SPOOL
local _ip=$(__vm_ip $_name)
local _port=`echo $2 | cut -d: -f2`
sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport $3 -j DNAT --to $_ip:$_port
sudo iptables -I FORWARD 1 -p tcp -d $_ip --dport $_port -j ACCEPT
cd - >/dev/null
;;
drop)
local _name=`echo $2 | cut -d: -f1`
vm cd $SPOOL
local _ip=$(__vm_ip $_name)
local _port=`echo $2 | cut -d: -f2`
sudo iptables -D PREROUTING -t nat -i eth0 -p tcp --dport $3 -j DNAT --to $_ip:$_port
sudo iptables -D FORWARD -p tcp -d $_ip --dport $_port -j ACCEPT
cd - >/dev/null
;;
*)
echo "vm forward <list|add|drop> [FQDN:PORT] [PORT]"
;;
esac
}
# -----------------------------------------------------------
##
## Virtual machine image management
##
function __vm_image() {
local _command=$1
local _sub_command=$2
local _help=\
"Usage: vm image <command> [<subcommand>] [<args>]
Available commands are:
info
Show the properties of the disk image.
snapshot <subcommand> [<args>]
Manage snapshots of the disk image."
case "$_command" in
info) kvm-img info $PWD/disk.img ;;
snapshot)
case "$_sub_command" in
list) virsh snapshot-list $(__vm_id) ;;
create) virsh snapshot-create $(__vm_id) ;;
restore) virsh snapshot-revert $(__vm_id) $3 ;;
delete) virsh snapshot-delete $(__vm_id) $3 ;;
*) _error "list|create|restore|delete are available commands!" ;;
esac
;;
*)
_error "The command '$_command' is not supported!"
echo $_help
;;
esac
}
function __vm_template_remote_set() {
if [ -z "$KVM_REMOTE_IMAGES" ]
then
echo "Please define \$KVM_REMOTE_IMAGES to continue."
return 1
fi
return 0
}
function vmtemplate() {
local _suffix=".kvm.tgz"
case "$1" in
list)
find $KVM_GOLDEN_IMAGES -maxdepth 1 -mindepth 1 -type d -printf "%P\n" | sort
;;
remote)
# Make sure the template remote location is set.
__vm_template_remote_set
if [ $? -eq 0 ]
then
case "$2" in
list)
local _name=`echo $KVM_REMOTE_IMAGES | cut -d: -f1`
local _path=`echo $KVM_REMOTE_IMAGES | cut -d: -f2`
ssh $_name "find $_path -name '*$_suffix' -printf '%P\n' | sort"
;;
upload)
cd $KVM_GOLDEN_IMAGES
echo -n "Compressing template image $3..."
tar -czf $3$_suffix $3
echo "done"
scp $3$_suffix $KVM_REMOTE_IMAGES
cd -
;;
download)
# Don't overwrite exiting templates
local _ans=
cd $KVM_GOLDEN_IMAGES
if [[ -f $3 ]]; then
echo -n "Overwrite? (y/n): "
read _ans
else
_ans="y"
fi
if [[ "$_ans" == "y" ]]; then
# Download the image
scp $KVM_REMOTE_IMAGES/$3 .
echo -n "Decompressing image..."
tar -xzf $3
echo "done"
fi
cd -
;;
*)
echo "Error: list|upload|download are available parameters!"
echo "Remote KVM virtual machine images are stored in:"
echo " KVM_REMOTE_IMAGES=$KVM_REMOTE_IMAGES"
;;
esac
fi
;;
*)
echo "Error: list|remote are available parameters!"
;;
esac
}
#-------------------------------------------------------------
##
## Virtual Machine Instance Management
##
function __vm_instance_running() {
local _instance=$1
virsh list | grep $_instance > /dev/null 2>&1
}
# Return true if the current working directory is
# virtual machine container.
function __vm_container_directory() {
test ! -z $(__vm_name)
}
function __vm_instance_start() {
local _config=$PWD/libvirt_instance.xml
local _instance=$(__vm_name)
_log "[__vm_instance_start] Defining virtual machine from '$_config'."
virsh define $_config > /dev/null 2>&1
_log "[__vm_instance_start] Stating instance '$_instance'."
virsh start $_instance > /dev/null # display error messages
if [ $? -eq 0 ]
then echo "Boot instance $_instance"
fi
}
function __vm_instance_stop() {
_log "[__vm_instance_stop] Sending shutdown signal to VM ID '$(__vm_id)'"
virsh shutdown $(__vm_id) > /dev/null
echo "Shutdown instance $(__vm_name)"
}
# kills the instance without graceful shutdown and
# removes it from the libvirt configuration
function __vm_instance_remove {
local _instance=$(__vm_name)
if $(__vm_instance_running $_instance) ; then
_log "[__vm_instance_remove] Killing instance '$_instance'."
virsh destroy $_instance > /dev/null 2>&1
sleep 1 # wait for the instance to be killed
fi
_log "[__vm_instance_remove] Un-define instance: '$_instance'."
virsh undefine $_instance > /dev/null 2>&1
echo "Instance $_instance removed"
}
function __vm_clone() {
# Print a help message if parameters are missing
local _help=\
"Usage: vm clone|shadow <template> <hostname>
Creates a virtual machine instance using a template.
Clone will make a copy of the disk image, where as
shadow only stores a differential image file. First
parameter is the template to use, and the second the
hostname to be applied."
if [ $# -ne 2 ]; then
echo $_help
# Otherwise clone the instance
else
# make sure to be outside a virtual machine container
cd $SPOOL
# virtual machine image to use for cloning
local _template=$KVM_GOLDEN_IMAGES/$1
# make sure to have the FQDN of the instance
local _instance=$(__vm_fqdn $2)
# target instance container directory
local _target=$KVM_VM_INSTANCES/$_instance
# ask the user to remove an existing instance
if [ -d $_target ]; then
echo -n "Remove existing instance '$_instance' (y/n)?: "
read ans
if [ "$ans" = "y" ]; then
cd $_target
__vm_instance_remove
cd - > /dev/null
_log "[__vm_clone] Deleting instance container directory: $_target"
rm -rf $_target
fi
fi
# don't overwrite
if [ ! -d $_target ]; then
_log "[__vm_clone] Template used for instance: $_template"
if [ "$_clone_shadow" = "true" ]; then
mkdir $_target
qemu-img create -b $_template/disk.img -f qcow2 $_target/disk.img > /dev/null
cp -R -n $_template/* $_target
_log "[__vm_clone] Deploying template shadow to '$_target'."
else
# clone the virtual machine template
cp -R $_template $_target
_log "[__vm_clone] Deploying template clone to '$_target'."
fi
# prepare the configuration files
local _ip=$(__vm_ip $_instance)
local _mac=$(__vm_mac $_instance)
_log "[__vm_clone] Instance network $_instance at $_ip ($_mac)."
cd $_target
__vm_reloc $1
__vm_network_config $_mac $_ip
__vm_instance_start
echo -n 'Booting.'
while :; do # try to connect
ping -c 1 $_ip >/dev/null 2>&1
# set the hostname
if [ $? = 0 ]
then # if network interface up
break
fi
sleep 1
echo -n '.'
done
echo "done"
# wait for the SSH service to come up
netcat $_ip 22 -w 30 -q 0 < /dev/null > /dev/null 2>&1
sleep 2
_log "[__vm_clone] SSH service at port 22 online."
__vm_hostname $_instance $_ip
else
_error"'$_target' exists!"
fi
fi
}
# ------------------------------------------------------------------
##
## Using the Chef configuration management with virtual machines
##
# Path to the users Chef cookbooks
CHEF_COOKBOOKS_DEFAULT="$HOME/chef/cookbooks:$HOME/chef/site-cookbooks"
CHEF_COOKBOOKS=${CHEF_COOKBOOKS:-$CHEF_COOKBOOKS_DEFAULT}
# This is the template for the chef-solo configuration file,
# shipped to the virtual machines before execution.
CHEF_SOLO_CONFIG=$(cat <<EOF
log_level :info
log_location STDOUT
verbose_logging nil
cookbook_path ["/var/chef/cookbooks"]
data_bag_path "/var/chef/data-bags"
role_path "/var/chef/roles"
cache_type "BasicFile"
cache_options({ :path => "/tmp/chef/cache/checksums", :skip_expires => true })
EOF
)
# This is the JSON specification use with chef-solo to
# configure the virtual machine. Users need to adjust this
# file!
CHEF_ATTRIBUTES=$(cat <<EOF
{
"run_list": [
"recipe[empty]"
]
}
EOF
)
# Link a list of cookbooks from the Chef users cookbook
# directories to the virtual machine containers cookbooks/
# directory.
function __vm_chef_cookbook() {
# add multiple cookbooks at once by argument list
for cookbook in $@
do
# Continue only if cookbook link doesn't exist
if [ -h $PWD/cookbooks/$cookbook ]
then
echo "Cookbook '$cookbook' already linked"
continue
fi
local found=false
# support multiple cookbook directories in a colon separated list
for cookbook_path in $(echo $CHEF_COOKBOOKS | tr ":" " ")
do
# if the source cookbook is found
if [ -d $cookbook_path/$cookbook ]
then
if [ ! -h $PWD/cookbooks/$cookbook ]
then
# link to the source cookbook
ln -s $cookbook_path/$cookbook $PWD/cookbooks/$cookbook
found=""
echo "Cookbook '$cookbook' added"
continue
fi
fi
done
if [ "$found" ]
then
echo "Cookbook '$cookbook' not found!"
fi
done
}
function __vm_chef_role() {
local role=$1
local name=$(basename $role)
if [ -h $PWD/roles/$name ]
then
echo "Role '$name' already linked"
else
if [[ -e $role ]]
then
ln -s $role $PWD/roles/$name
echo "Role '$name' added."
else
echo "ERROR: Role '$name' isn't existing!"
fi
fi
}
function __vm_chef() {
local _help=\
"Usage: vm config <command> [<sub-command>] [<args>]
Use Chef configuration management to provision a
virtual machine instance.
Commands:
add cookbook <name> [<name> ...]
Define a list of cookbooks used to configure this
virtual machine instance. They will be synced to
the virtual machine each time the configuration
process is executed.
add role <path>
Adds a role to apply to this virtual machine
instance. Roles will be synced each time the
configuration process is executed.
add data-bag <path>
Add data-bags to be synced to the virtual machines
each time the configuration process is executed.
solo [run-list]
Execute Chef in solo-mode inside the virtual machine
instance using defined cookbooks and roles, as
well as the node description in 'chef_attributes.json'.
client <server_hostname>
Connect a virtual machine instance to an existing
Chef server using the validation certificate and the
configuration in the directory passed as argument."
case "$1" in
help) echo $_help ;;
add)
if ! [[ -d $PWD/cookbooks && -d $PWD/roles && -d $PWD/data-bags ]]; then
mkdir -p $PWD/cookbooks
mkdir -p $PWD/roles
mkdir -p $PWD/data-bags
fi
case "$2" in
cookbook) shift; shift; __vm_chef_cookbook $@ ;;
role) shift; shift; __vm_chef_role $@ ;;
data-bag)
if [[ -e $3 ]]
then
local _bag_name=$(basename $3)
ln -v -s $3 $PWD/data-bags/$_bag_name
else
_error "'$3' isn't existing!"
fi
;;
*)
_error "cookbook|role|data-bag are available parameters"
;;
esac
;;
solo)
touch $PWD/chef.log # create the log file
# Without at least one cookbook we cannot run!
if [[ ! -d $PWD/cookbooks ]]
then
_error "No Chef cookbooks defined yet."
echo "Use: vm config add cookbook <name> [<name>...]"
return
else
cookbooks_found=$(find $PWD/cookbooks -maxdepth 1 -type l 2> /dev/null)
if [ ! "$cookbooks_found" ]
then
_error "No Chef cookbooks found in cookbooks/"
return
fi
fi
# Write a simple Chef attributes file
if [[ -n $2 ]]
then
echo "{ \"run_list\": [ \"$2\" ] }" > $PWD/chef_attributes.json
echo "INFO: Creating Chef attributes file ./chef_attributes.json"
fi
# Make sure the user edits the Chef attributes
if [[ ! -e $PWD/chef_attributes.json ]]; then
echo "$CHEF_ATTRIBUTES" > $PWD/chef_attributes.json
echo "INFO: No attributes definition to run chef-solo!"
echo "Add cookbooks,roles and attributes to the file ./chef_attributes.json"
return
fi
__vm_ssh "sudo mkdir -p -m 777 /var/chef/cookbooks"
__vm_ssh 'sudo chmod 777 /var/chef'
__vm_sync $PWD/cookbooks /var/chef >> $PWD/chef.log
# Sync the roles if they exist
if [[ -d $PWD/roles ]]; then
__vm_ssh "[ ! -d /var/chef/roles ] && sudo mkdir -m 777 /var/chef/roles"
__vm_sync $PWD/roles /var/chef/ >> $PWD/chef.log
fi
# Sync data-bags if existing
if [[ -d $PWD/data-bags ]]; then
__vm_ssh "[ ! -d /var/chef/data-bags ] && sudo mkdir -m 777 /var/chef/data-bags"
__vm_sync $PWD/data-bags /var/chef >> $PWD/chef.log
fi
# Write the chef configuration file if not existing and upload it!
if [[ ! -e $PWD/chef_config.rb ]]; then
echo "$CHEF_SOLO_CONFIG" > $PWD/chef_config.rb
fi
__vm_put chef_config.rb /var/chef/config.rb
__vm_put chef_attributes.json /var/chef/attributes.json
shift 2> /dev/null
__vm_ssh "cd /var/chef; sudo chef-solo -c config.rb -j attributes.json $@"
;;
client)
local _server=$2
if [ -z $_server ]
then
_error "No Chef-Server instance defined!"
else
local _config=$KVM_VM_INSTANCES/$(__vm_fqdn $_server)
_log "[__vm_chef] Reading Chef-Client configuration from '$_config'."
__vm_ssh 'sudo /etc/init.d/chef-client stop > /dev/null 2>&1'
__vm_ssh 'sudo rm /etc/chef/client.pem > /dev/null 2>&1'
__vm_put $_config/chef_client.rb /tmp
__vm_ssh 'sudo mv /tmp/chef_client.rb /etc/chef/client.rb'
__vm_put $_config/chef/validation.pem /tmp
__vm_ssh 'sudo mv /tmp/validation.pem /etc/chef/'
sleep 1
__vm_ssh 'sudo /etc/init.d/chef-client start'
echo "Instance connected to Chef-Server '$_server'."
fi
;;
*) _error "Unknown command '$1'!"; echo $_help ;;
esac
}
# Use Chef Knife configuration in the local directory to
# connect to the server.
function vknife() { knife $@ -c $PWD/chef/knife.rb; }
# --------------------------------------------------------------
##
## Interface all commands
##
HELP="Usage: vm <command> [<sub-command>] [<args>]
Shell-function to manage clusters of local virtual
machines. (Version $_version)
Commands:
network status|start|stop|lookup
Manage the host-internal (NATed) network used
to connect the virtual machine instance.
status
Display the current state of instances.
login [<hostname>]
Opens a terminal to the defined instance or to
the instance of the current working directory.
cd <hostname>
Change to the working directory of instance.
template list
Show local template images.
template remote list|upload|download [<image>]
Show remote template images. Upload or download
at template from the remote storage by defining
the template name.
clone|shadow <template> <hostname>
Use a template for a new instance.
forward <instance>:<port> <port>
Enable port-forwarding.
Commands used in the instance working directory:
start
Switch the instance on.
stop
Shutdown the instance down.
kill
Kill the instance.
remove
Shutdown and remove VM from system.
exec <command>
Run a command inside the instance.
sudo <command>
Run a command inside the instance with Sudo.
put <local_file> <instance_file>
Upload a file to the instance.
get <instance_file> <local_file>
Download a file from the instance.
sync <local_dir> <instance_dir>
Rsync a directory to the instance.
fs mount|umount
Mount the VM root-directory.
config solo|client
Provision the instance using Chef either in solo
mode, or configure the client connection to an
Chef-server.
config add cookbook|role|data-bag [<cookbook>|<path>]
Add a cookbook to the provision facility, by defining
a cookbook name as target, or the path to a role
file as target.
image info|snapshot
Manage/snapshot VM disk images.
Overwrite environment variables:
KVM_VM_INSTANCES
Directory storing virtual machine instances.
KVM_GOLDEN_IMAGES
Directory holding virtual machine templates.
KVM_REMOTE_IMAGES
Where to download virtual machine templates.
VIRSH_NET_CONFIG
DNS/DHCP network configuration file.
CHEF_COOKBOOKS
Paths to your Chef cookbooks (colon separated).
Further information in the README.
"
function vm() {
local _command=$1
if [ -z $_command ]; then
_command='status'
fi
case "$_command" in
status)
virsh list --all | awk '{gsub(/^ +| +$/,"")}1' |\
tail -n +3 | cut -d ' ' -f2-
;;
network) shift; __vm_network "$@";;
cd) shift; cd $KVM_VM_INSTANCES/$(__vm_fqdn $1) ;;
login) shift; __vm_login "$@" ;;
template) shift; vmtemplate $@ ;;
clone) shift; __vm_clone $@ ;;
shadow) shift; _clone_shadow=true __vm_clone $@ ;;
forward) shift; __vm_port_forward $@ ;;
help) echo "$HELP" ;;
*)
# the following commands can only be executed inside
# a virtual machine container directory
if $(__vm_container_directory); then
case "$_command" in
start) __vm_instance_start ;;
stop) __vm_instance_stop ;;
kill) virsh destroy $(__vm_id) ;;
remove) __vm_instance_remove ;;
image) shift; __vm_image $@ ;;
*)
# the following commands can only be executed when
# the virtual machine is running
if $(__vm_instance_running $(__vm_name)) ; then
case "$_command" in