-
Notifications
You must be signed in to change notification settings - Fork 14
/
fast-vm-image
executable file
·798 lines (746 loc) · 29.4 KB
/
fast-vm-image
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
#!/bin/sh
DEBUG_LOG_CMD="logger -p debug -t fast-vm-image-dbg"
FASTVM_HELPER=/usr/libexec/fast-vm-helper.sh
FASTVM_USER_CONF_DIR="$HOME/.fast-vm"
FASTVM_SYSTEM_CONF_DIR="/etc/fast-vm"
CURL_OPTS=""
TASKSET_CPULIST="0"
LOG_LEVEL=7
DISPLAY_LEVEL=7
FVM=$(basename "$0")
## terminal colors
c_red=$(tput setaf 1)
c_yellow=$(tput setaf 3)
c_green=$(tput setaf 2)
c_cyan=$(tput setaf 6)
c_normal=$(tput sgr0)
## LOG priorities
P_DEBUG=7
P_INFO=6
P_WARNING=4
P_ERROR=3
#### start common functions
# function for message output to display&logs based on LOG/DISPLAY level
pmsg () {
priority="$1"
message="$2"
vmn=${3:-__}
if [ "$LOG_LEVEL" -ge "$priority" ]; then
printf "%s |%s| %s" "$USER" "$vmn" "$message"|logger -p "$priority" --id -t fast-vm
fi
if [ "$DISPLAY_LEVEL" -ge "$priority" ]; then
case "$priority" in
"$P_DEBUG")
# shellcheck disable=SC2059
printf "[${c_cyan}inf${c_normal}] %b" "$message" | sed -e "s/^/\[$vmn\]/"
;;
"$P_INFO")
# shellcheck disable=SC2059
printf "[${c_green}ok${c_normal}] %b" "$message" | sed -e "s/^/\[$vmn\]/"
;;
"$P_WARNING")
# shellcheck disable=SC2059
printf "[${c_yellow}wrn${c_normal}] %b" "$message" | sed -e "s/^/\[$vmn\]/"
;;
"$P_ERROR")
# shellcheck disable=SC2059
printf "[${c_red}err${c_normal}] %b" "$message" | sed -e "s/^/\[$vmn\]/"
;;
*)
printf "[unk] %s" "$message" | sed -e "s/^/\[$vmn\]/"
;;
esac
fi
}
# helper function for loading and checking configuration
check_empty () {
var_name="$1"
var_value="$2"
if [ -z "$var_value" ]; then
pmsg $P_ERROR "variable $var_name not declared in global configuration run configure-fast-vm again or fix manually\n"
exit 2
fi
}
check_file () {
file_path="$1"
file_size=0
file_local=0
error_prefix="$2"
quiet_flag="$3"
if [ -z "$file_path" ]; then pmsg $P_DEBUG "provided empty file path\n"; fi
if [ "$curl_ok" -eq 0 ] && [ "$(echo "$file_path" | awk '{ s=substr($0, 0, 4); print s; }' )" = "http" ]; then
curl_data=$(curl $CURL_OPTS --head "$file_path" 2>/dev/null)
curl_exit="$?"
curl_httpok=$(echo "$curl_data"|grep -c -E "(HTTP.*200 OK|HTTP/2 200)")
if [ "$curl_httpok" -eq "1" ]; then
file_size=$(echo "$curl_data"|grep -i "Content-Length:"|awk '{print $2}'|sed 's/[^0-9]*//g')
if [ -z "$file_size" ]; then
file_size=0
pmsg $P_DEBUG "Unable to detect remote file. Using '$file_size' instead.\n"
else
pmsg $P_DEBUG "Detected remote file with size '$file_size'.\n"
fi
else
if [ -z "$quiet_flag" ]; then
# print error only when "quiet" flag is not present
pmsg $P_ERROR "$error_prefix: error checking remote file on the server.\nCURL exited with code $curl_exit and we got following information from server\n$curl_data\n"
fi
return 2
fi
else
if [ -f "$file_path" ]; then
file_local=1
else
pmsg $P_ERROR "$error_prefix: local file '$file_path' not found\n"
return 2
fi
fi
}
download_file () {
file_url="$1"
tmp_file=$(mktemp)
pmsg $P_DEBUG "downloading $file_url\ninto $tmp_file\n" >&2
if curl $CURL_OPTS -o "$tmp_file" -s "$file_url"; then
echo "$tmp_file"
else
pmsg $P_ERROR "Download failed\n" >&2
exit 1
fi
}
config_file_operation () {
cmd="$1"
options="$2"
if [ "$(whoami)" = 'root' ]; then
$cmd "$FASTVM_SYSTEM_CONF_DIR/$options" 2>&1|$DEBUG_LOG_CMD
else
$cmd "$FASTVM_USER_CONF_DIR/$options" 2>&1|$DEBUG_LOG_CMD
fi
}
check_image_existance () {
if [ ! -b "/dev/$THINPOOL_VG/$VM_PREFIX$image_name" ]; then
pmsg $P_ERROR "Image '${image_name}' not found.\n '/dev/$THINPOOL_VG/$VM_PREFIX$image_name' is not a block device.\n"
exit 2
fi
}
check_appliance_presence () {
# export LIBGUESTFS_PATH to custom appliance if it looks to be present
# this doesn't check if appliance is usable (configure-fast-vm does that)
ap_path="/var/lib/fast-vm/appliance"
if [ -f "$ap_path/kernel" ] && [ -f "$ap_path/initrd" ] && [ -f "$ap_path/root" ]; then
export LIBGUESTFS_PATH="$ap_path"
pmsg $P_DEBUG "Using system appliance ($ap_path) with following capabilities:\n"
ls "$ap_path"/capability_* 2>/dev/null
fi
}
usage () {
part="$1"
case $part in
import)
echo "$FVM import ImageName <empty |PathToImage> PathToLibvirtXML [PathToHacksFile] [PathToDeleteHackFile]"
;;
import_custom)
echo "$FVM import_custom ImageSizeInGiB ImageName <empty |PathToImage> PathToLibvirtXML [PathToHacksFile] [PathToDeleteHackFile]"
;;
export)
echo "$FVM export ImageName [ <zst|xz|gz> ]"
;;
remove)
echo "$FVM remove ImageName"
;;
resize)
echo "$FVM resize ImageName NewImageSizeInGiB"
;;
import_profile)
echo "$FVM import_profile ProfileName ImageName PathToLibvirtXML [PathToHacksFile] [PathToDeleteHackFile]"
;;
remove_profile)
echo "$FVM remove_profile ProfileName"
;;
list)
echo "$FVM list [short]"
;;
list_profiles)
echo "$FVM list_profiles [short]"
;;
compact)
echo "$FVM compact ImageName"
;;
verify)
echo "$FVM verify ImageName [ PathToCheksumsDir ]"
;;
gen_checksums)
echo "$FVM gen_checksums ImageName"
;;
*)
echo "== fast-vm-image version 1.7 <ondrej-xa2iel8u@famera.cz> =="
echo "$FVM <action> <options>"
for cmd in import import_custom export remove resize import_profile remove_profile list list_profiles compact verify gen_checksums;
do
usage $cmd noexit
done
;;
esac
# exit only when second parameter is not specified, otherwise just return and continue
if [ -z "$2" ]; then exit 1; fi
}
#### end common functions
# first parameter is compulsory
if [ -z "$1" ]; then usage; fi
# locate and load fast-vm configuration
if [ -f "/etc/fast-vm.conf" ]; then
# load and verify configuration
# shellcheck source=fast-vm.conf.defaults
. /etc/fast-vm.conf
check_empty "VM_PREFIX" "$VM_PREFIX"
check_empty "LIBVIRT_NETWORK" "$LIBVIRT_NETWORK"
check_empty "THINPOOL_VG" "$THINPOOL_VG"
check_empty "THINPOOL_LV" "$THINPOOL_LV"
check_empty "SUBNET_NUMBER" "$SUBNET_NUMBER"
check_empty "FASTVM_GROUP" "$FASTVM_GROUP"
else
pmsg $P_ERROR "no global configuration file /etc/fast-vm.conf not found please run configure-fast-vm as root before using fast-vm\n"
exit 1
fi
# check if we are in $FASTVM_GROUP group or if we are root
if [ "$(id |grep -c "($FASTVM_GROUP)")" -eq 0 ] && [ "$(whoami)" != "root" ]; then
pmsg $P_ERROR "User running fast-vm must be member of group '$FASTVM_GROUP'. You can easily add user to this group using command below\n # usermod -a -G $FASTVM_GROUP $(whoami)\n"
exit 1
fi
## try to detect if the defined thin pool is available
double_dash_lv=$(echo "$THINPOOL_LV"|sed 's/-/--/g') # LVM uses double dash in the /dev/mapper
double_dash_vg=$(echo "$THINPOOL_VG"|sed 's/-/--/g') # also for VGs
if [ -b "/dev/mapper/${double_dash_vg}-${double_dash_lv}-tpool" ];then
THINPOOL_PATH="/dev/mapper/${double_dash_vg}-${double_dash_lv}-tpool"
fi
if [ -b "/dev/mapper/${double_dash_vg}-${double_dash_lv}_tdata" ] && [ -b "/dev/mapper/${double_dash_vg}-${double_dash_lv}_tmeta" ];then
THINPOOL_PATH="/dev/mapper/${double_dash_vg}-${double_dash_lv}"
fi
if [ -z "$THINPOOL_PATH" ]; then
pmsg $P_ERROR "thinpool $THINPOOL_VG/$THINPOOL_LV not found or not a thinpool LV, try running configure-fast-vm as root to check/correct\n"
exit 1
fi
# create local configuration directory if it doesn't exists
if [ ! -d "$FASTVM_USER_CONF_DIR" ]; then
mkdir "$FASTVM_USER_CONF_DIR" 2>&1 |$DEBUG_LOG_CMD
fi
if [ ! -d "$FASTVM_SYSTEM_CONF_DIR" ] && [ "$(whoami)" = "root" ]; then
mkdir "$FASTVM_SYSTEM_CONF_DIR" 2>&1 |$DEBUG_LOG_CMD
fi
## check if there is curl available so we can fetch images from http/https
command -v curl >/dev/null 2>&1
curl_ok="$?"
command -v pv >/dev/null 2>&1
pv_ok="$?"
#### main script
case "$1" in
import|import_custom)
unpacked_image_size="10"
if [ "$1" = "import_custom" ]; then
unpacked_image_size=$(echo "$2"|grep -E '^[0-9]+$')
if [ -z "$unpacked_image_size" ]; then pmsg $P_ERROR "invalid image size. Use only whole decimal numbers\n"; usage import_custom; fi
shift
fi
image_name="$2"
if [ -z "$image_name" ]; then pmsg $P_ERROR "missing image name\n"; usage import; fi
image_name_stripped=$(echo "$image_name"|tr -d -C '[:alnum:]\-.')
if [ "$image_name" != "$image_name_stripped" ]; then
pmsg $P_ERROR "ImageName contains restricted characters, allowed are only letters, numbers, dot(.) and dash(-)\n"
exit 1
fi
image_name_stripped2=$(echo "$image_name_stripped"|sed -e 's/-[0-9]\+$//')
if [ "$image_name_stripped" != "$image_name_stripped2" ]; then
pmsg $P_ERROR "ImageName cannot end with '-XX' where XX is number.\n"
exit 1
fi
image_path="$3"
# skip path check for "empty" image file
if [ "$image_path" != "empty" ]; then
check_file "$image_path" "image file"
if [ "$?" = 2 ]; then pmsg $P_ERROR "image file not found\n"; exit 2; fi
image_path="$file_path"
fi
tmp_files=""
image_size="$file_size"
image_local="$file_local"
image_extension="${image_path##*.}"
case "$image_extension" in
gz)
if ! command -v gunzip >/dev/null 2>&1; then
pmsg $P_ERROR "'gunzip' binary missing, install it to have support for GZ\n"
exit 1
fi
decompress_cmd="gunzip -c"
;;
xz)
if ! command -v xz >/dev/null 2>&1; then
pmsg $P_ERROR "'xz' binary missing, install it to have support for XZ\n"
exit 1
fi
decompress_cmd="xz -d -c"
;;
zst)
if ! command -v zstd >/dev/null 2>&1; then
pmsg $P_ERROR "'zstd' binary missing, install it to have support for ZST\n"
exit 1
fi
decompress_cmd="zstd -d --stdout"
;;
empty)
pmsg $P_DEBUG "we are creating empty image file\n"
;;
*)
pmsg $P_ERROR "unknown image file extension $image_extension, (supported: xz, gz)\n"
exit 1
;;
esac
image_xml_path="$4"
check_file "$image_xml_path" "XML VM definition"
if [ "$?" = 2 ]; then pmsg $P_ERROR "XML file $image_xml_path not found\n"; exit 2; fi
if [ "$file_local" = '0' ]; then
image_xml_path=$(download_file "$image_xml_path")
tmp_files="$tmp_files $image_xml_path"
fi
hack_file="$5"
if [ -n "$hack_file" ]; then
check_file "$hack_file" "hack file"
if [ "$?" = 2 ]; then pmsg $P_ERROR "hack file $hack_file not found\n"; exit 2; fi
if [ "$file_local" = '0' ]; then
hack_file=$(download_file "$hack_file")
tmp_files="$tmp_files $hack_file"
fi
fi
delete_hack_file="$6"
if [ -n "$delete_hack_file" ]; then
check_file "$delete_hack_file" "delete hack file"
if [ "$?" = 2 ]; then pmsg $P_ERROR "delete hack file $delete_hack_file not found\n"; exit 2; fi
if [ "$file_local" = '0' ]; then
delete_hack_file=$(download_file "$delete_hack_file")
tmp_files="$tmp_files $delete_hack_file"
fi
fi
# try to autodect the size of image from filename
autodetected_size=$(basename "$image_path"|sed -nr 's/^([0-9]+)g__.*$/\1/gp')
if [ "$1" = "import" ] && [ -n "$autodetected_size" ]; then
unpacked_image_size=$autodetected_size
pmsg $P_DEBUG "Size of image was determined from the filename to be ${autodetected_size}G.\n"
fi
if [ -b "/dev/$THINPOOL_VG/$VM_PREFIX$image_name" ]; then
pmsg $P_ERROR "base image for '$image_name' version already exists. To remove image use 'remove' parameter\n"
exit 1
else
pmsg $P_DEBUG "creating LV $VM_PREFIX$image_name ...\n"
if ! echo "lvcreate" "base" "$image_name" "$unpacked_image_size" | sudo -n $FASTVM_HELPER; then
pmsg $P_ERROR "Error creating thin LV for VM image\n"
exit 1
fi
fi
# change group of LV so we can use it for locking
echo "chgrp" "/dev/$THINPOOL_VG/$VM_PREFIX$image_name" | sudo -n $FASTVM_HELPER
(
# locked section - BEGIN
flock -x -n 9 || exit 4
pmsg $P_DEBUG "importing image $image_path into /dev/$THINPOOL_VG/$VM_PREFIX$image_name\n"
if [ "$image_path" = "empty" ]; then
config_file_operation "cp $image_xml_path" "config-${image_name}.xml"
pmsg $P_INFO "Empty image imported\n"
exit 0
fi
if [ "$image_local" = "1" ]; then
if [ "$pv_ok" -eq '0' ]; then
$decompress_cmd "$image_path"| pv -s "${unpacked_image_size}G" | dd of="/dev/$THINPOOL_VG/$VM_PREFIX$image_name" bs=1M conv=sparse
import_exit="$?"
else
pmsg $P_DEBUG "please wait while importing image (to show progress, install 'pv')\n"
$decompress_cmd "$image_path"| dd of="/dev/$THINPOOL_VG/$VM_PREFIX$image_name" bs=1M conv=sparse
import_exit="$?"
fi
else
# import from http(s)
if [ "$pv_ok" -eq '0' ]; then
stty_state=$(stty -g) # save terminal state in case we neeed to restore it due to 'pv' breaking terminal 'echo'
curl $CURL_OPTS -s "$image_path" | pv -N download -c -s "$image_size" | $decompress_cmd | pv -N unpack -c -s "${unpacked_image_size}G" | dd of="/dev/$THINPOOL_VG/$VM_PREFIX$image_name" bs=1M conv=sparse
import_exit="$?"
if $(stty -a|grep -q -E '\-echo[^a-z]'); then
pmsg $P_DEBUG "Missing 'echo' on termninal after 'pv', restoring terminal settings.\n"
# this should address issue #50 - https://github.com/OndrejHome/fast-vm/issues/50
stty "$stty_state"
fi
else
pmsg $P_DEBUG "please wait while importing image (to show image write progress, install 'pv')\n"
curl $CURL_OPTS "$image_path" | $decompress_cmd | dd of="/dev/$THINPOOL_VG/$VM_PREFIX$image_name" bs=1M conv=sparse
import_exit="$?"
fi
fi
if [ "$import_exit" != "0" ]; then
pmsg $P_ERROR "Error copying the image into thin LV\n"
exit 1
fi
# copy the XML configuration file
config_file_operation "cp $image_xml_path" "config-${image_name}.xml"
config_file_operation "chmod +r" "config-${image_name}.xml"
if [ -n "$hack_file" ] && [ -f "$hack_file" ]; then
config_file_operation "cp $hack_file" "hacks-${image_name}.sh"
config_file_operation "chmod +rx" "hacks-${image_name}.sh"
fi
if [ -n "$delete_hack_file" ] && [ -f "$delete_hack_file" ]; then
config_file_operation "cp $delete_hack_file" "delete-hacks-${image_name}.sh"
config_file_operation "chmod +rx" "delete-hacks-${image_name}.sh"
fi
## run image verification after importing image and hacks files, user can interrupt this to skip it
$0 verify "$image_name" "$(dirname "$image_path")"
pmsg $P_INFO "Image $image_name imported\n"
rm -f -- $tmp_files # cleanup temporary files
# locked section - END
) 9<"/dev/$THINPOOL_VG/$VM_PREFIX$image_name"
if [ "$?" != "0" ]; then
pmsg $P_ERROR "It is not possible to import image $image_name now as it is being used by fast-vm or other process at the moment\n"
exit 1
else
rm -f "/tmp/fast-vm.img.${image_name}.lock"
fi
;;
export)
image_name="$2"
if [ -z "$image_name" ]; then pmsg $P_ERROR "missing image name\n"; usage export; fi
check_image_existance "$image_name"
lvs_out=$(echo 'lvs'|sudo -n $FASTVM_HELPER)
image_size=$(echo "$lvs_out"|grep -E " ${VM_PREFIX}$image_name "|sed 's/\.[0-9]\+g//'|awk '{printf $2}')
# get image format or use default 'xz' if no format was specified
image_format="$3"
if [ -z "$image_format" ]; then image_format='xz'; fi
case "$image_format" in
gz)
if ! command -v gzip >/dev/null 2>&1; then
pmsg $P_ERROR "'gzip' binary missing, install it to have support for GZ\n"
exit 1
fi
compress_cmd="gzip -c"
;;
xz)
if ! command -v xz >/dev/null 2>&1; then
pmsg $P_ERROR "'xz' binary missing, install it to have support for XZ\n"
exit 1
fi
compress_cmd="xz -c -k --best --block-size=16777216 -T 0"
;;
zst)
if ! command -v zstd >/dev/null 2>&1; then
pmsg $P_ERROR "'zstd' binary missing, install it to have support for ZST\n"
exit 1
fi
compress_cmd="zstd -c -k -19 -T0 --stdout"
;;
*)
pmsg $P_ERROR "unknown image format $image_format (supported: zst, xz, gz)\n"
exit 1
;;
esac
# change group of LV so we can use it for locking
echo "chgrp" "/dev/$THINPOOL_VG/$VM_PREFIX$image_name" | sudo -n $FASTVM_HELPER
(
# locked section - BEGIN
flock -s -n 9 || exit 4
pmsg $P_DEBUG "compressing the image $image_name into $(pwd)/${image_size}g__${image_name}.img.${image_format}\n"
if [ "$pv_ok" -eq '0' ]; then
pv -N "Image reading" -s "${image_size}G" < "/dev/$THINPOOL_VG/$VM_PREFIX$image_name" | $compress_cmd > "$(pwd)/${image_size}g__${image_name}.img.${image_format}"
else
$compress_cmd < "/dev/$THINPOOL_VG/$VM_PREFIX$image_name" > "$(pwd)/${image_size}g__${image_name}.img.${image_format}"
fi
if [ "$?" -eq "0" ]; then
pmsg $P_INFO "Image compression finished\n"
else
pmsg $P_ERROR "There was error compressing the image, check output above\n"
fi
# locked section - END
) 9<"/dev/$THINPOOL_VG/$VM_PREFIX$image_name"
if [ "$?" != "0" ]; then
pmsg $P_ERROR "It is not possible to export image $image_name now as it is being used by fast-vm or other process at the moment\n"
exit 1
else
rm -f "/tmp/fast-vm.img.${image_name}.lock"
fi
;;
remove)
image_name="$2"
if [ -z "$image_name" ]; then pmsg $P_ERROR "missing image name\n"; usage remove; fi
check_image_existance "$image_name"
pmsg $P_DEBUG "stopping and undefining the base VM of $image_name\n"
virsh --connect qemu:///system destroy "$VM_PREFIX$image_name" 2>&1|$DEBUG_LOG_CMD
virsh --connect qemu:///system undefine "$VM_PREFIX$image_name" --nvram --managed-save 2>&1|$DEBUG_LOG_CMD
pmsg $P_DEBUG "removing LV $VM_PREFIX$image_name ...\n"
echo "lvremove" "/dev/$THINPOOL_VG/$VM_PREFIX$image_name" | sudo -n $FASTVM_HELPER
pmsg $P_DEBUG "removing XML and hack file(s) of $image_name...\n"
config_file_operation "rm" "config-${image_name}.xml"
config_file_operation "rm" "hacks-${image_name}.sh"
config_file_operation "rm" "delete-hacks-${image_name}.sh"
pmsg $P_INFO "image $image_name removed\n"
;;
import_profile)
profile_name="$2"
if [ -z "$profile_name" ]; then pmsg $P_ERROR "missing profile name\n"; usage import_profile; fi
profile_name_stripped=$(echo "$profile_name"|tr -d -C '[:alnum:]\-.')
if [ "$profile_name" != "$profile_name_stripped" ]; then
pmsg $P_ERROR "ProfileName contains restricted characters, allowed are only letters, numbers, dot(.) and dash(-)\n"
exit 1
fi
profile_name_stripped2=$(echo "$profile_name_stripped"|sed -e 's/-[0-9]\+$//')
if [ "$profile_name_stripped" != "$profile_name_stripped2" ]; then
pmsg $P_ERROR "ProfileName cannot end with '-XX' where XX is number.\n"
exit 1
fi
image_name="$3"
if [ -z "$image_name" ]; then pmsg $P_ERROR "missing image name\n"; usage import_profile; fi
check_image_existance "$image_name"
tmp_files=""
image_xml_path="$4"
check_file "$image_xml_path" "XML VM definition"
if [ "$?" = 2 ]; then pmsg $P_ERROR "XML file $image_xml_path not found\n"; exit 2; fi
if [ "$file_local" = '0' ]; then
image_xml_path=$(download_file "$image_xml_path")
tmp_files="$tmp_files $image_xml_path"
fi
hack_file="$5"
if [ -n "$hack_file" ]; then
check_file "$hack_file" "hack file"
if [ "$?" = 2 ]; then pmsg $P_ERROR "hack file $hack_file not found\n"; exit 2; fi
if [ "$file_local" = '0' ]; then
hack_file=$(download_file "$hack_file")
tmp_files="$tmp_files $hack_file"
fi
fi
delete_hack_file="$6"
if [ -n "$delete_hack_file" ]; then
check_file "$delete_hack_file" "delete hack file"
if [ "$?" = 2 ]; then pmsg $P_ERROR "delete hack file $delete_hack_file not found\n"; exit 2; fi
if [ "$file_local" = '0' ]; then
delete_hack_file=$(download_file "$delete_hack_file")
tmp_files="$tmp_files $delete_hack_file"
fi
fi
# create profile and copy files to right places
config_file_operation "mkdir" "${profile_name}"
tmp_base_image=$(mktemp)
tmp_files="$tmp_files $tmp_base_image"
echo "$image_name" > "$tmp_base_image"
config_file_operation "cp $tmp_base_image" "${profile_name}/base_image"
config_file_operation "chmod +r" "${profile_name}/base_image"
rm -f "$tmp_base_image"
config_file_operation "cp $image_xml_path" "${profile_name}/config.xml"
config_file_operation "chmod +r" "${profile_name}/config.xml"
if [ -n "$hack_file" ] && [ -f "$hack_file" ]; then
config_file_operation "cp $hack_file" "${profile_name}/hacks.sh"
config_file_operation "chmod +rx" "${profile_name}/hacks.sh"
fi
if [ -n "$delete_hack_file" ] && [ -f "$delete_hack_file" ]; then
config_file_operation "cp $delete_hack_file" "${profile_name}/delete-hacks.sh"
config_file_operation "chmod +rx" "${profile_name}/delete-hacks.sh"
fi
rm -f -- $tmp_files # cleanup temporary files
pmsg $P_INFO "Profile $profile_name based on Image $image_name imported\n"
;;
remove_profile)
profile_name="$2"
if [ -z "$profile_name" ]; then pmsg $P_ERROR "missing profile name\n"; usage remove_profile; fi
if [ ! -d "$FASTVM_USER_CONF_DIR/${profile_name}" ] && [ ! -d "$FASTVM_SYSTEM_CONF_DIR/${profile_name}" ]; then
pmsg $P_ERROR "profile '$profile_name' not found\n"
usage remove_profile
fi
config_file_operation "rm -rf" "$profile_name"
pmsg $P_INFO "Profile $profile_name removed\n"
;;
resize)
image_name="$2"
new_size=$(echo "$3"|grep -E '^[0-9]+$')
if [ -z "$image_name" ]; then pmsg $P_ERROR "missing image name\n"; usage resize; fi
check_image_existance "$image_name"
if [ -z "$new_size" ]; then pmsg $P_ERROR "invalid image size. Use only whole decimal numbers\n"; usage resize; fi
#FIXME due to handling of logging in fast-vm-helper we might not receive error code if something went wrong
pmsg $P_DEBUG "resizing image $image_name to ${new_size}G\n"
if echo "lvresize" "/dev/$THINPOOL_VG/$VM_PREFIX$image_name" "$new_size" | sudo -n $FASTVM_HELPER; then
pmsg $P_INFO "Only newly created VMs from this image will use new size.\n"
pmsg $P_INFO "Resize of image to ${new_size}G succeeded.\n"
else
pmsg $P_ERROR "Resize failed. Check logs for more details\n"
fi
;;
list)
short_output="$2" # if non-zero the output suitable for external processing
# lets get the dm-XX number of the thinpool
pool_dm=$(basename "$(readlink "$THINPOOL_PATH")")
# from dm-XX of thinpool we can list the LVs inside the thinpool
holders=$(ls "/sys/block/$pool_dm/holders")
holder_regexp=""
for dm in $holders
do
if [ -z "$holder_regexp" ]; then
holder_regexp="$dm"
else
holder_regexp="$holder_regexp|$dm"
fi
done
holder_regexp="($holder_regexp)"
# finaly parse the VM image versions from what we have
image_name_list=$(ls -l "/dev/$THINPOOL_VG/" |grep -E "$holder_regexp"|awk '{print $9}'|grep "$VM_PREFIX"|sed -e "s/$VM_PREFIX//; s/-[0-9]\+$//"|uniq)
if [ -n "$short_output" ] && [ "$short_output" = "short" ]; then
echo "$image_name_list"
else
printf '%-36s |%-21s |%-21s |\n' 'IMAGE' 'SYSTEM' 'USER'
printf '%-20s %13s |%-8s %-13s|%-8s %-13s|\n' 'Image name' 'Size( %used )' 'XML' 'Hack file for' 'XML' 'Hack file for'
lvs_out=$(echo 'lvs'|sudo -n $FASTVM_HELPER)
for image_name in $image_name_list; do
# check the existence of the XML file
if [ -f "$FASTVM_USER_CONF_DIR/config-${image_name}.xml" ]; then
user_xml_exists="ok"
else
user_xml_exists="missing"
fi
# check existence of hack file
user_hacks_exists="no hack files"
if [ -f "$FASTVM_USER_CONF_DIR/hacks-${image_name}.sh" ]; then
user_hacks_exists="create"
fi
if [ -f "$FASTVM_USER_CONF_DIR/delete-hacks-${image_name}.sh" ]; then
user_hacks_exists="${user_hacks_exists},delete"
fi
# check the existence of the XML file
if [ -f "$FASTVM_SYSTEM_CONF_DIR/config-${image_name}.xml" ]; then
system_xml_exists="ok"
else
system_xml_exists="missing"
fi
# check existence of hack file
system_hacks_exists="no hack files"
if [ -f "$FASTVM_SYSTEM_CONF_DIR/hacks-${image_name}.sh" ]; then
system_hacks_exists="create"
fi
if [ -f "$FASTVM_SYSTEM_CONF_DIR/delete-hacks-${image_name}.sh" ]; then
system_hacks_exists=",delete"
fi
size=$(echo "$lvs_out"|grep -E " ${VM_PREFIX}$image_name "|sed 's/\.00g//'|awk '{printf "%3sg(%6s%%)",$2,$3}')
printf "%-20s %13s |%-8s %-13s|%-8s %-13s|\n" "$image_name" "$size" "$system_xml_exists" "$system_hacks_exists" "$user_xml_exists" "$user_hacks_exists"
done
printf "\nNOTE: if image is missing XML it wouldn't be possible to create a VM from it!\n Image with missing hack file(s) can work if it's not needed.\n"
printf "\n USER XML and hack file(s) takes precendense before SYSTEM ones.\n"
fi
;;
list_profiles)
short_output="$2" # if non-zero the output suitable for external processing
profile_dir_list=$(find "$FASTVM_SYSTEM_CONF_DIR/" "$FASTVM_USER_CONF_DIR/" -mindepth 1 -maxdepth 1 -type d 2>/dev/null)
if [ -n "$short_output" ] && [ "$short_output" = "short" ]; then
for dir in $profile_dir_list;
do
# valid profile must contain at least base image name
if [ -f "$dir/base_image" ]; then
basename "$dir"
fi
done
else
printf "%-41s|%-41s\n" 'Names' 'Changes provided by profile'
printf "%-20s|%-20s|%-5s|%-7s|%-14s\n" 'Profile Name' 'Base Image Name' 'XML' 'hacks' 'delete-hacks'
for dir in $profile_dir_list;
do
profile_name=$(basename "$dir")
base_image_name=$(cat "$dir/base_image" 2>/dev/null)
# skip directories not containing base image name
if [ -z "$base_image_name" ]; then continue; fi
profile_xml='no'
profile_hacks='no'
profile_delete_hacks='no'
if [ -f "$dir/config.xml" ]; then profile_xml='yes'; fi
if [ -f "$dir/hacks.sh" ]; then profile_hacks='yes'; fi
if [ -f "$dir/delete-hacks.sh" ]; then profile_delete_hacks='yes'; fi
printf "%-20s|%-20s|%-5s|%-7s|%-14s\n" "$profile_name" "$base_image_name" "$profile_xml" "$profile_hacks" "$profile_delete_hacks"
done
fi
;;
compact)
image_name="$2"
if [ -z "$image_name" ]; then pmsg $P_ERROR "missing image name\n"; usage compact; fi
check_image_existance "$image_name"
check_appliance_presence
# change group of LV so we can clean it up
echo "chgrp" "/dev/$THINPOOL_VG/$VM_PREFIX$image_name" | sudo -n $FASTVM_HELPER
# set CPU afifnity when 'taskset' is present to make this work on systems with several CPU core types
TASKSET=""
if command -v taskset >/dev/null 2>&1; then
TASKSET="taskset -c $TASKSET_CPULIST"
fi
pmsg $P_INFO "Compacting disk of image $image_name ....\n"
pmsg $P_DEBUG "Running '$TASKSET virt-sparsify -q --in-place /dev/$THINPOOL_VG/$VM_PREFIX$image_name.\n"
pipefail=$(mktemp)
if { LIBGUESTFS_BACKEND=direct $TASKSET virt-sparsify -q --in-place "/dev/$THINPOOL_VG/$VM_PREFIX$image_name" 2>&1 || echo > $pipefail; } | $DEBUG_LOG_CMD && [ ! -s "$pipefail" ]; then
pmsg $P_INFO "Image disk compaction completed.\n"
else
pmsg $P_ERROR "virt-sparsify command failed, check logs for details.\n"
fi
rm -f "$pipefail"
;;
verify)
image_name="$2"
if [ -z "$image_name" ]; then pmsg $P_ERROR "missing image name\n"; usage verify; fi
check_image_existance "$image_name"
image_checksum_dir="$3"
if [ -z "$image_checksum_dir" ]; then image_checksum_dir='./'; fi
check_file "$image_checksum_dir/${image_name}.part_00.sha512.txt" "part_00 checksum" "quiet"
if [ "$?" = 2 ]; then pmsg $P_ERROR "part_00 checksum file not found\n"; exit 2; fi
lvs_out=$(echo 'lvs'|sudo -n $FASTVM_HELPER)
size=$(echo "$lvs_out"|grep -E " ${VM_PREFIX}$image_name "|sed 's/\.[0-9]\+g//'|awk '{printf $2}')
# change group of LV so we can clean it up
echo "chgrp" "/dev/$THINPOOL_VG/$VM_PREFIX$image_name" | sudo -n $FASTVM_HELPER
pmsg $P_INFO "Verifying image $image_name ($size) using checksums from $image_checksum_dir\n"
parts_ok=0
parts_fail=0
parts_missing=0
for part in $(seq -w 00 "$size");
do
check_file "$image_checksum_dir/${image_name}.part_${part}.sha512.txt" "part_${part} checksum"
if [ "$?" = 2 ]; then
parts_missing=$((parts_missing+1))
pmsg $P_WARNING "part_${part} checksum file missing - $image_checksum_dir/${image_name}.part_${part}.sha512.txt\n"
else
# read the checksum from file
if [ "$file_local" = '1' ]; then
part_checksum=$(head -1 "$image_checksum_dir/${image_name}.part_${part}.sha512.txt")
else
part_checksum=$(curl $CURL_OPTS -s "$image_checksum_dir/${image_name}.part_${part}.sha512.txt"| head -1)
fi
# compute checksum of same part of imported image
checksum=$(dd if="/dev/$THINPOOL_VG/${VM_PREFIX}$image_name" skip="$part" bs=1G count=1 2>/dev/null|sha512sum |awk '{print $1}')
if [ "$part_checksum" = "$checksum" ]; then
parts_ok=$((parts_ok+1))
pmsg $P_DEBUG "part_${part} checksum OK\n"
else
parts_fail=$((parts_fail+1))
pmsg $P_ERROR "part_${part} checksum failed\n"
fi
fi
done
if [ "$parts_fail" = '0' ] && [ "$parts_missing" = '0' ]; then
pmsg $P_INFO "CHECK SUMMARY: OK: $parts_ok FAIL: $parts_fail MISSING: $parts_missing\n"
else
pmsg $P_ERROR "CHECK SUMMARY: OK: $parts_ok FAIL: $parts_fail MISSING: $parts_missing\n"
exit 1
fi
;;
gen_checksums)
image_name="$2"
if [ -z "$image_name" ]; then pmsg $P_ERROR "missing image name\n"; usage gen_checksums; fi
check_image_existance "$image_name"
## get image size
lvs_out=$(echo 'lvs'|sudo -n $FASTVM_HELPER)
size=$(echo "$lvs_out"|grep -E " ${VM_PREFIX}$image_name "|sed 's/\.[0-9]\+g//'|awk '{printf $2}')
# change group of LV so we can clean it up
echo "chgrp" "/dev/$THINPOOL_VG/$VM_PREFIX$image_name" | sudo -n $FASTVM_HELPER
pmsg $P_INFO "Generating checksums for $image_name ($size)\n"
for part in $(seq -w 00 "$size");
do
checksum=$(dd if="/dev/$THINPOOL_VG/${VM_PREFIX}$image_name" skip="$part" bs=1G count=1 2>/dev/null|sha512sum |awk '{print $1}')
pmsg $P_DEBUG "${image_name}.part_${part}.sha512.txt: $(echo $checksum|awk '{print substr($0, 1, 7),"...",substr($0, length - 6, 7)}')\n"
echo "$checksum" > "${image_name}.part_${part}.sha512.txt"
done
;;
*)
pmsg $P_ERROR "unknown action\n"
usage
;;
esac