-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathparallel_guest_migration_base.pm
1797 lines (1595 loc) · 82.8 KB
/
parallel_guest_migration_base.pm
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
# PARALLEL GUEST MIGRATION BASE MODULE
#
# Copyright 2023 SUSE LLC
# SPDX-License-Identifier: FSFAP
#
# Summary: Base module for parallel guest migration test run.
#
# Global structures:
# %guest_matrix to record information about guest under test.
# %guest_network_matrix to specify guest network information.
# %guest_migration_matrix to store all guest migration tests.
# %test_result to record test reults for all guests and tests.
#
# Run-time settings:
# LOCAL_IPADDR, LOCAL_FQDN, PEER_IPADDR and PEER_FQDN store
# ip address and FQDN of source and destination hosts.
# GUEST_UNDER_TEST stores guest to be tested.
# GUEST_UNDER_TEST_MACADDR, GUEST_UNDER_TEST_IPADDR,
# GUEST_UNDER_TEST_NETTYPE, GUEST_UNDER_TEST_NETNAME,
# GUEST_UNDER_TEST_NETMODE and GUEST_UNDER_TEST_STATICIP are
# derived from %guest_matrix for source and destination hosts
# to be on the same picture about about during test runs.
# TEST_RUN_PROGRESS stores running subroutine name as progress.
# TEST_RUN_RESULT stores the final test run result.
#
# Member subroutines to support guest migraton test:
# Check hosts health and do logs cleanup in the first place
# Initialize above global structures to facilitate test run
# Check hosts meet requirements for guest migration test
# Check guets meet requirements for guest migration test
# Restore and create environment for guest to start
# Check and update information about guest during test run
# Check final test results and create junit logs
# Synchronization between peers in pass/fail situations
#
# Maintainer: Wayne Chen <[email protected]>, qe-virt <[email protected]>
package parallel_guest_migration_base;
use base "opensusebasetest";
use strict;
use warnings;
use POSIX 'strftime';
use DateTime;
use testapi;
use Data::Dumper;
use upload_system_log;
use XML::LibXML;
use Tie::IxHash;
use lockapi;
use mmapi;
use Carp;
use Utils::Systemd;
use List::MoreUtils qw(firstidx);
use List::Util 'first';
use version_utils qw(is_opensuse is_sle is_alp is_microos get_os_release);
use virt_utils qw(collect_host_and_guest_logs cleanup_host_and_guest_logs enable_debug_logging);
use virt_autotest::utils qw(is_kvm_host is_xen_host check_host_health check_guest_health is_fv_guest is_pv_guest add_guest_to_hosts parse_subnet_address_ipv4 check_port_state setup_common_ssh_config is_monolithic_libvirtd restart_libvirtd check_libvirtd restart_modular_libvirt_daemons);
use virt_autotest::domain_management_utils qw(construct_uri create_guest remove_guest shutdown_guest show_guest check_guest_state);
use utils qw(zypper_call systemctl script_retry define_secret_variable);
use virt_autotest::common;
tie our %guest_matrix, 'Tie::IxHash', ();
tie our %guest_network_matrix, 'Tie::IxHash', ();
tie our %test_result, 'Tie::IxHash', ();
%guest_network_matrix = (
nat => {
device => 'virbrX',
ipaddr => '192.168.X.1',
netmask => '255.255.255.0',
masklen => '24',
startaddr => '192.168.X.2',
endaddr => '192.168.X.254'
},
route => {
device => 'virbrX',
ipaddr => '192.168.X.1',
netmask => '255.255.255.0',
masklen => '24',
startaddr => '192.168.X.2',
endaddr => '192.168.X.254'
},
default => {
device => 'virbr0',
ipaddr => '',
netmask => '',
masklen => '',
startaddr => '',
endaddr => ''
},
host => {
device => 'br0',
ipaddr => '',
netmask => '',
masklen => '',
startaddr => '',
endaddr => ''
},
bridge => {
device => 'brX',
ipaddr => '192.168.X.1',
netmask => '255.255.255.0',
masklen => '24',
startaddr => '192.168.X.2',
endaddr => '192.168.X.254'
}
);
tie our %guest_migration_matrix_kvm, 'Tie::IxHash', (
virsh_live_native => 'virsh --connect=srcuri --debug=0 migrate --verbose --live --unsafe guest dsturi',
virsh_live_native_p2p => 'virsh --connect=srcuri --debug=0 migrate --verbose --live --p2p --persistent --change-protection --unsafe --compressed --abort-on-error --undefinesource guest dsturi',
virsh_live_tunnel_p2p => 'virsh --connect=srcuri --debug=0 migrate --verbose --live --p2p --tunnelled --persistent --change-protection --unsafe --compressed --abort-on-error --undefinesource guest dsturi',
virsh_live_native_p2p_auto_postcopy => 'virsh --connect=srcuri --debug=0 migrate --verbose --live --p2p --persistent --change-protection --unsafe --compressed --abort-on-error --postcopy --postcopy-after-precopy --undefinesource guest dsturi',
virsh_live_native_p2p_manual_postcopy => 'virsh --connect=srcuri --debug=0 migrate --verbose --live --p2p --persistent --change-protection --unsafe --compressed --abort-on-error --postcopy --undefinesource guest dsturi#virsh --connect=srcuri --debug=0 migrate-postcopy guest',
virsh_offline_native_p2p => 'virsh --connect=srcuri --debug=0 migrate --verbose --offline --p2p --persistent --unsafe --undefinesource guest dsturi');
tie our %guest_migration_matrix_xen, 'Tie::IxHash', (
xl_online => 'xl -vvv migrate guest dstip',
virsh_online => 'virsh --connect=srcuri --debug=0 migrate --verbose --undefinesource guest dsturi',
virsh_live => 'virsh --connect=srcuri --debug=0 migrate --verbose --live --undefinesource guest dsturi');
tie our %guest_migration_matrix, 'Tie::IxHash', (kvm => \%guest_migration_matrix_kvm, xen => \%guest_migration_matrix_xen);
=head2 run
Main subroutine to execute test.
=cut
sub run {
my ($self) = @_;
$self->set_test_run_progress;
$self->pre_run_test;
$self->{"start_run"} = time();
$self->run_test;
$self->{"stop_run"} = time();
$self->post_run_test;
}
=head2 pre_run_test
Run before test run starts. Check host healthy state and do logs cleanup.
=cut
sub pre_run_test {
my $self = shift;
$self->set_test_run_progress;
check_host_health;
cleanup_host_and_guest_logs;
}
=head2 run_test
Actual test is executed in this subroutine which needs to be overloaded in test
module which uses this module as base.
=cut
sub run_test {
my $self = shift;
$self->set_test_run_progress;
croak("Please overload this subroutine in children modules to run desired tests");
}
=head2 post_run_test
Run after test run finishes. Judge overall test run result and die if test fails.
=cut
sub post_run_test {
my $self = shift;
$self->set_test_run_progress;
print "Final Test Results Are:\n", Dumper(\%test_result);
foreach my $_guest (keys %test_result) {
foreach my $_test (keys %{$test_result{$_guest}}) {
if ($test_result{$_guest}{$_test}{status} eq 'FAILED') {
set_var('TEST_RUN_RESULT', 'FAILED');
bmwqemu::save_vars();
bmwqemu::load_vars();
croak("Test run failed because certain test case did not pass");
}
}
}
$self->create_junit_log;
set_var('TEST_RUN_RESULT', 'PASSED');
bmwqemu::save_vars();
bmwqemu::load_vars();
}
=head2 get_parallel_role
Get role (parent or children) of job based on whether PARALLEL_WITH is given.
=cut
sub get_parallel_role {
my $self = shift;
return get_var('PARALLEL_WITH', '') ? 'children' : 'parent';
}
=head2 create_barrier
Create barriers to be used for synchronization between peers.
=cut
sub create_barrier {
my ($self, %args) = @_;
$args{signal} //= '';
croak("Signal to be created must be given") if (!$args{signal});
foreach my $_signal (split(/ /, $args{signal})) {
barrier_create($_signal, 2);
record_info("$_signal(x2) barrier created");
}
}
=head2 set_test_run_progress
Any subroutine calls this set_test_run_progress will set TEST_RUN_PROGRESS to
the name of FILE::SUBROUTINE. If argument token is not empty, it will be added
to the end of the name of FILE::SUBROUTINE.
=cut
sub set_test_run_progress {
my ($self, %args) = @_;
$args{token} //= '';
my $_test_run_progress = (caller(1))[3];
$_test_run_progress .= "_$args{token}" if ($args{token});
set_var('TEST_RUN_PROGRESS', $_test_run_progress);
bmwqemu::save_vars();
bmwqemu::load_vars();
}
=head2 get_test_run_progress
Return TEST_RUN_PROGRESS of peer or self job depends on whether argument peer is
set (1) or not (0);
=cut
sub get_test_run_progress {
my ($self, %args) = @_;
$args{peer} //= 1;
return get_var('TEST_RUN_PROGRESS', '') if ($args{peer} == 0);
my $_role = $self->get_parallel_role;
my ($_peer_info, $_peer_vars) = $self->get_peer_info(role => $_role);
return $_peer_vars->{'TEST_RUN_PROGRESS'} if (defined $_peer_vars->{'TEST_RUN_PROGRESS'});
return '';
}
=head2 get_test_run_result
Return TEST_RUN_RESULT of peer or self job depends on whether argument peer is
set (1) or not (0);
=cut
sub get_test_run_result {
my ($self, %args) = @_;
$args{peer} //= 1;
return get_var('TEST_RUN_RESULT', '') if ($args{peer} == 0);
my $_role = $self->get_parallel_role;
my ($_peer_info, $_peer_vars) = $self->get_peer_info(role => $_role);
return $_peer_vars->{'TEST_RUN_RESULT'} if (defined $_peer_vars->{'TEST_RUN_RESULT'});
return '';
}
=head2 do_local_initialization
Initialization information about local host and save it into variables.
=cut
sub do_local_initialization {
my $self = shift;
$self->set_test_run_progress;
record_info("Local initialization");
my $_localip = '';
my $_localfqdn = '';
set_var('LOCAL_IPADDR', $_localip);
set_var('LOCAL_FQDN', $_localfqdn);
$_localip = script_output("hostname -i | cut -d' ' -f 2", type_command => 1) if (script_retry("hostname -i", option => '--kill-after=1 --signal=9', delay => 1, retry => 60) == 0);
(($_localip eq '' or $_localip eq '127.0.0.1' or $_localip eq '::1 127.0.0.1') and (is_sle('15+') or !is_sle)) ? set_var('LOCAL_IPADDR', (split(/ /, script_output("hostname -I", type_command => 1)))[0]) : set_var('LOCAL_IPADDR', $_localip);
$_localfqdn = script_output("hostname -f", type_command => 1) if (script_retry("hostname -f", option => '--kill-after=1 --signal=9', delay => 1, retry => 60) == 0);
(($_localfqdn eq '' or $_localfqdn eq 'localhost') and (is_sle('15+') or !is_sle)) ? set_var('LOCAL_FQDN', (split(/ /, script_output("hostname -A", type_command => 1)))[0]) : set_var('LOCAL_FQDN', $_localfqdn);
save_screenshot;
set_var('TEST_RUN_RESULT', '');
bmwqemu::save_vars();
bmwqemu::load_vars();
}
=head2 do_peer_initialization
Initialization information about peer, save it into variables and setup passwordless
ssh connection to it.
=cut
sub do_peer_initialization {
my $self = shift;
$self->set_test_run_progress;
record_info("Peer initialization");
my $_role = $self->get_parallel_role;
my ($_peer_info, $_peer_vars) = $self->get_peer_info(role => $_role);
set_var('PEER_IPADDR', $_peer_vars->{'LOCAL_IPADDR'});
set_var('PEER_FQDN', $_peer_vars->{'LOCAL_IPADDR'});
bmwqemu::save_vars();
bmwqemu::load_vars();
assert_script_run("sed -i -r \'/^PreferredAuthentications.*\$/d\' /root/.ssh/config") if (script_run("ls /root/.ssh/config") == 0);
$self->config_ssh_pubkey_auth(addr => get_required_var('PEER_IPADDR'), overwrite => 0);
}
=head2 get_peer_info
Get peer job info and variables.
=cut
sub get_peer_info {
my $self = shift;
my $_peer = '';
my $_peerid = '';
my $_role = $self->get_parallel_role;
if ($_role eq 'parent') {
$_peer = get_children();
$_peerid = (keys %$_peer)[0];
}
elsif ($_role eq 'children') {
$_peer = get_parents();
$_peerid = $_peer->[0];
}
my $_peerinfo = get_job_info($_peerid);
my $_peervars = get_job_autoinst_vars($_peerid);
print "Peer Job Info:", Dumper($_peerinfo);
print "Peer Job Vars:", Dumper($_peervars);
return ($_peerinfo, $_peervars);
}
=head2 config_ssh_pubkey_auth
Configure SSH Public Key Authentication to host or guest. Main arguments are
address to which ssh connects, whether overwrite (1) or not (0) existing keys,
either host (1) or guest (0) on which operation will be done and whether die
(1) or not (0) if any failures happen.
=cut
sub config_ssh_pubkey_auth {
my ($self, %args) = @_;
$args{addr} //= '';
$args{overwrite} //= 0;
$args{host} //= 1;
$args{die} //= 0;
croak("The address of ssh connnection must be given") if (!$args{addr});
assert_script_run("clear && ssh-keygen -b 2048 -t rsa -q -N \"\" -f ~/.ssh/id_rsa <<< y") if ($args{overwrite} == 1 or script_run("ls ~/.ssh/id_rsa") != 0);
my $_ret = 0;
foreach my $_addr (split(/ /, $args{addr})) {
record_info("Config $_addr SSH PubKey auth");
next if (script_run("timeout --kill-after=1 --signal=9 15 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root\@$_addr ls") == 0);
enter_cmd("clear", wait_still_screen => 3);
enter_cmd("timeout --kill-after=1 --signal=9 30 ssh-copy-id -f -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa.pub root\@$_addr", wait_still_screen => 3);
if ($args{host} == 1) {
susedistribution::handle_password_prompt();
}
else {
check_screen("password-prompt", 60);
enter_cmd(get_var('_SECRET_GUEST_PASSWORD', ''), wait_screen_change => 50, max_interval => 1);
wait_still_screen(35);
}
my $_temp = 1;
$_temp = script_run("timeout --kill-after=1 --signal=9 15 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root\@$_addr ls");
$_ret |= $_temp;
record_info("Machine $_addr SSH PubKeyAuth failed", "Can not establish ssh connection to machine $_addr using Public Key Authentication", result => 'fail') if ($_temp != 0);
}
croak("SSH public key authentication setup failed for certain system") if ($_ret != 0 and $args{die} == 1);
return $_ret;
}
=head2 check_host_architecture
Check source and destination hosts have the same architecture, otherwise test run
can not proceed.
=cut
sub check_host_architecture {
my ($self, %args) = @_;
record_info("Check host architecture");
my $_localip = get_var('LOCAL_IPADDR');
my $_localarch = script_output("uname -i", type_command => 1);
my $_peerip = get_var('PEER_IPADDR');
my $_peerarch = script_output("ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root\@$_peerip uname -i", type_command => 1);
save_screenshot;
croak("Architecture $_localarch on $_localip does not match $_peerarch on $_peerip") if ($_localarch ne $_peerarch);
}
=head2 check_host_os
Check source and destination hosts operating system version. Guest migration can
not be done from the newer to the older. Main argument is host role (src or dst).
=cut
sub check_host_os {
my ($self, %args) = @_;
$args{role} //= 'src';
record_info("Check host os");
my $_ret = 0;
my $_localip = get_var('LOCAL_IPADDR');
my $_peerip = get_var('PEER_IPADDR');
if (is_sle) {
my ($_localosver, $_localossp,) = get_os_release;
my ($_peerosver, $_peerossp,) = get_os_release("ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root\@$_peerip");
save_screenshot;
unless (($_peerosver > $_localosver) or ($_peerosver == $_localosver and $_peerossp >= $_localossp)) {
$_ret = 1;
if ($args{role} eq 'src') {
croak("Destination os $_peerosver-sp$_peerossp falls behind source os $_localosver-sp$_localossp");
}
elsif ($args{role} eq 'dst') {
record_info("Source os $_peerosver-sp$_peerossp falls behind destination os $_localosver-sp$_localossp");
}
}
}
return $_ret;
}
=head2 check_host_virtualization
Check virtualization modules and services are ready, otherwise test run can not
proceed.
=cut
sub check_host_virtualization {
my $self = shift;
record_info("Check host virtualization");
if (is_kvm_host) {
assert_script_run("lsmod | grep kvm");
}
elsif (is_xen_host) {
assert_script_run("lsmod | grep xen");
}
# Note: TBD for modular libvirt. See poo#129086 for detail.
if (!is_alp and is_monolithic_libvirtd) {
if (script_run("systemctl is-active libvirtd") != 0) {
systemctl("stop libvirtd", ignore_failure => 1);
systemctl("start libvirtd");
systemctl("is-active libvirtd");
}
else {
systemctl("restart libvirtd");
}
} else {
restart_modular_libvirt_daemons;
}
check_libvirtd;
save_screenshot;
}
=head2 check_host_package
Install necessary packages to facilitate test run down the road. Main argument
is packages to be installed.
=cut
sub check_host_package {
my ($self, %args) = @_;
$args{package} //= '';
record_info("Check host package");
zypper_call("--gpg-auto-import-keys ref");
is_kvm_host ? zypper_call("in -t pattern kvm_tools") : zypper_call("in -t pattern xen_tools") if (!is_alp and !is_microos);
zypper_call("in iputils nmap libguestfs* libguestfs0 guestfs-tools virt-install libvirt-client");
zypper_call("in $args{package}") if ($args{package});
}
=head2 check_host_uid
Check source and destination hosts have the same user id for user qemu. If any
discrepancy, set the same group id on both side by using the value provided in
_user.
=cut
sub check_host_uid {
my $self = shift;
my %_user = (qemu => 996);
my $_local = get_var('LOCAL_IPADDR');
my $_peer = get_var('PEER_IPADDR');
my $_localuid = '';
my $_peeruid = '';
foreach my $_single_user (keys %_user) {
record_info("Check $_single_user uid on host");
$_localuid = script_output("id -u $_single_user", type_command => 1);
$_peeruid = script_output("ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root\@$_peer id -u $_single_user", type_command => 1);
if ($_localuid != $_peeruid) {
my $_ret = script_run("usermod -u $_user{$_single_user} $_single_user");
croak("$_single_user UID modification failed on $_local") if ($_ret != 0 and $_ret != 12);
$_ret = script_run("ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root\@$_peer usermod -u $_user{$_single_user} $_single_user");
croak("$_single_user UID modification failed on $_peer") if ($_ret != 0 and $_ret != 12);
}
save_screenshot;
}
}
=head2 check_host_gid
Check source and destination hosts have the same group id for groups qemu, kvm and
libvirt. If any discrepancy, set the same group id on both side by using the value
provided in _group.
=cut
sub check_host_gid {
my $self = shift;
my %_group = (qemu => 999,
kvm => 998,
libvirt => 997
);
my $_local = get_var('LOCAL_IPADDR');
my $_peer = get_var('PEER_IPADDR');
my $_localgid = '';
my $_peergid = '';
foreach my $_single_group (keys %_group) {
record_info("Check $_single_group gid on host");
$_localgid = script_output("grep ^$_single_group /etc/group|cut -d \":\" -f 3", type_command => 1);
$_peergid = script_output("ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root\@$_peer grep ^$_single_group /etc/group|cut -d \":\" -f 3", type_command => 1);
save_screenshot;
if ($_localgid != $_peergid) {
my $_ret = script_run("groupmod -g $_group{$_single_group} $_single_group");
save_screenshot;
croak("$_single_group GID modification failed on $_local") if ($_ret != 0);
$_ret = script_run("ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root\@$_peer groupmod -g $_group{$_single_group} $_single_group");
save_screenshot;
croak("$_single_group GID modification failed on $_peer") if ($_ret != 0);
}
}
}
=head2 config_host_shared_storage
Configure shared nfs storage on server and mount it on client. Main arguments
are type of shared storage, path of exported shared storage, server or client
, original source path on server and mount path on server and client.
=cut
sub config_host_shared_storage {
my ($self, %args) = @_;
$args{type} //= 'nfs';
$args{exppath} //= '/home/virt';
$args{role} //= 'server';
$args{srcpath} //= '/var/lib/libvirt/images';
$args{mntpath} //= '/var/lib/libvirt/images';
record_info("Configure host shared storage");
if ($args{type} eq 'nfs') {
my $_temppath = get_var('EXTERNAL_NFS_SHARE', '');
if ($_temppath) {
script_run("umount $args{mntpath} || umount -f -l $args{mntpath}");
assert_script_run("mount -t nfs $_temppath $args{mntpath}");
$args{role} eq 'server' ? assert_script_run("rm -f -r $args{mntpath}/nfsok; touch $args{mntpath}/nfsok") : assert_script_run("cd ~ && ls -lah $args{mntpath}/nfsok");
}
elsif ($args{role} eq 'server') {
if (script_run("ls /etc/nfs.conf") == 0) {
my $_cpu_num = 0;
$_cpu_num = script_output("grep -c ^processor /proc/cpuinfo", type_command => 1, proceed_on_failure => 1);
$_cpu_num = 1 if ($_cpu_num == 0 or $_cpu_num eq '');
my $_nfs_threads = 32 * $_cpu_num;
assert_script_run("sed -i -r \'s/^.*threads=.*\$/ threads=$_nfs_threads/g\' /etc/nfs.conf");
}
$_temppath = $args{exppath};
$_temppath =~ s/\//\\\//g;
# Create export path directory if it doesn't exist
if (script_run("test -d $_temppath") != 0) {
assert_script_run("mkdir -p $_temppath");
}
# Move guest images from the default image path to the export path
# Guest installation puts images to /var/lib/libvirt/images by default,
# move them to nfs export directory /home/virt, then mount /home/virt to /var/lib/libvirt/images.
assert_script_run("mv $args{srcpath}/* $args{exppath}/");
assert_script_run("sed -i \'/^.*$_temppath.*\$/d\' /etc/exports");
assert_script_run("echo \"$args{exppath} *(rw,sync,no_root_squash,no_subtree_check)\" >> /etc/exports");
assert_script_run("exportfs -a");
systemctl('restart nfs-server.service');
systemctl('status nfs-server.service');
assert_script_run("rm -f -r $args{exppath}/nfsok; touch $args{exppath}/nfsok");
script_run("umount $args{mntpath} || umount -f -l $args{mntpath}");
assert_script_run("mount -t nfs localhost:$args{exppath} $args{mntpath}");
save_screenshot;
}
elsif ($args{role} eq 'client') {
my $_nfsserver = get_var('PEER_IPADDR');
script_run("umount $args{mntpath} || umount -f -l $args{mntpath}");
assert_script_run("mount -t nfs $_nfsserver:$args{exppath} $args{mntpath}");
assert_script_run("cd ~ && ls -lah $args{mntpath}/nfsok");
save_screenshot;
}
}
}
=head2 config_host_security
Get rid of limitations come from security services and rules which may have impact
on connectivity between host and guest.
=cut
sub config_host_security {
my $self = shift;
record_info("Config host security");
my @_security_service = ('SuSEFirewall2', 'firewalld', 'apparmor');
foreach my $_ss (@_security_service) {
if (script_run("systemctl is-enabled $_ss") == 0) {
systemctl("stop $_ss");
systemctl("disable $_ss");
save_screenshot;
}
}
if (script_run("cat /etc/selinux/config | grep -i ^SELINUX=enforcing\$") == 0) {
assert_script_run("sed -i -r \'s/^SELINUX=enforcing\$/SELINUX=permissive/g\' /etc/selinux/config");
}
script_run("iptables -P INPUT ACCEPT;
iptables -P FORWARD ACCEPT;
iptables -P OUTPUT ACCEPT;
iptables -t nat -F;
iptables -F;
sysctl -w net.ipv4.ip_forward=1;
sysctl -w net.ipv4.conf.all.forwarding=1"
);
save_screenshot;
setup_common_ssh_config;
}
=head2 guest_under_test
Obtain all guests to be tested, either from test suite level setting GUEST_LIST
or those already on host. Destination host retrieves such information from source.
At last, initialize guest_matrix to empty.
=cut
sub guest_under_test {
my ($self, %args) = @_;
$args{role} //= '';
$args{driver} //= '';
$args{transport} //= 'ssh';
$args{user} //= '';
$args{host} //= 'localhost';
$args{port} //= '';
$args{path} //= 'system';
$args{extra} //= '';
$self->set_test_run_progress;
croak("Role used to differentiate migration source from destination must be given") if (!$args{role});
my $_uri = "--connect=" . virt_autotest::domain_management_utils::construct_uri(driver => $args{driver}, transport => $args{transport}, user => $args{user}, host => $args{host}, port => $args{port}, path => $args{path}, extra => $args{extra});
if ($args{role} eq 'src') {
my $_guest_under_test = get_var('GUEST_LIST', '');
if (!$_guest_under_test) {
$_guest_under_test = join(" ", split(/\n/, script_output("virsh $_uri list --all --name | grep -v Domain-0", type_command => 1)));
}
else {
$_guest_under_test = join(" ", split(/,/, $_guest_under_test));
}
set_var('GUEST_UNDER_TEST', $_guest_under_test);
bmwqemu::save_vars();
bmwqemu::load_vars();
}
elsif ($args{role} eq 'dst') {
my ($_peer_info, $_peer_vars) = $self->get_peer_info(role => $self->get_parallel_role);
set_var('GUEST_UNDER_TEST', $_peer_vars->{'GUEST_UNDER_TEST'});
bmwqemu::save_vars();
bmwqemu::load_vars();
}
foreach my $_guest (split(/ /, get_required_var('GUEST_UNDER_TEST'))) {
tie my %_single_guest_matrix, 'Tie::IxHash', (macaddr => '', ipaddr => '', nettype => '', netname => '', netmode => '', staticip => 'no');
$guest_matrix{$_guest} = \%_single_guest_matrix;
}
print "Guest Matrix After Initialization:\n", Dumper(\%guest_matrix);
return get_required_var('GUEST_UNDER_TEST');
}
=head2 initialize_test_result
Initialize hash structure test_result, which contains all tests specified by
test suite level setting GUEST_MIGRATION_TEST, to 'FAILED' and TEST_RUN_RESULT
to empty. The detailed test commands are stored in guest_migration_matrix.
=cut
sub initialize_test_result {
my $self = shift;
$self->set_test_run_progress;
my @_guest_migration_test = split(/,/, get_var('GUEST_MIGRATION_TEST', ''));
# Remove 'virsh_live_native_p2p_manual_postcopy' if present and record a soft failure
@_guest_migration_test = $self->filter_migration_tests(migration_tests => \@_guest_migration_test) if (is_sle('=15-sp6'));
my $_full_test_matrix = is_kvm_host ? $parallel_guest_migration_base::guest_migration_matrix{kvm} : $parallel_guest_migration_base::guest_migration_matrix{xen};
@_guest_migration_test = keys(%$_full_test_matrix) if (scalar @_guest_migration_test == 0);
my $_localip = get_required_var('LOCAL_IPADDR');
my $_peerip = get_required_var('PEER_IPADDR');
my $_localuri = virt_autotest::domain_management_utils::construct_uri();
my $_peeruri = virt_autotest::domain_management_utils::construct_uri(host => $_peerip);
foreach my $_guest (keys %parallel_guest_migration_base::guest_matrix) {
tie my %_single_guest_matrix, 'Tie::IxHash', ();
while (my ($_testindex, $_test) = each(@_guest_migration_test)) {
my $_command = $_full_test_matrix->{$_test};
$_command =~ s/guest/$_guest/g;
$_command =~ s/srcuri/$_localuri/g;
$_command =~ s/dsturi/$_peeruri/g;
$_command =~ s/dstip/$_peerip/g;
tie my %_single_test_matrix, 'Tie::IxHash', (status => 'FAILED', test_time => strftime("\%H:\%M:\%S", gmtime(0)), shortname => $_test);
$_single_guest_matrix{$_command} = \%_single_test_matrix;
}
$test_result{$_guest} = \%_single_guest_matrix;
}
set_var('TEST_RUN_RESULT', '');
bmwqemu::save_vars();
bmwqemu::load_vars();
print "Test Results After Initialization:\n", Dumper(\%test_result);
}
=head2 save_guest_asset
Save guest xml config for use down the road. Main arguments are guest to be manipulated
, directory in which xml config is stored and whether die (1) or not (0) if any failures
happen. This subroutine also calls construct_uri to determine the desired URI to be used
if the interested party is not localhost. Please refer to subroutine construct_uri for
the arguments related.
=cut
sub save_guest_asset {
my ($self, %args) = @_;
$args{guest} //= '';
$args{confdir} //= '/var/lib/libvirt/images';
$args{die} //= 0;
$args{driver} //= '';
$args{transport} //= 'ssh';
$args{user} //= '';
$args{host} //= 'localhost';
$args{port} //= '';
$args{path} //= 'system';
$args{extra} //= '';
croak("Guest to be saved must be given") if (!$args{guest});
my $_ret = 0;
my $_uri = "--connect=" . virt_autotest::domain_management_utils::construct_uri(driver => $args{driver}, transport => $args{transport}, user => $args{user}, host => $args{host}, port => $args{port}, path => $args{path}, extra => $args{extra});
foreach my $_guest (split(/ /, $args{guest})) {
record_info("Save $_guest asset");
my $_temp = 1;
$_temp = script_run("virsh $_uri dumpxml $_guest > $args{confdir}/$_guest.xml");
$_temp |= script_run("xmlstarlet ed --inplace --delete \"/domain/devices/interface/target\" $args{confdir}/$_guest.xml");
$_temp |= script_run("xmlstarlet ed --inplace --delete \"/domain/devices/interface/alias\" $args{confdir}/$_guest.xml");
$_temp |= script_run("xmlstarlet ed --inplace --delete \"/domain/devices/interface/source/\@portid\" $args{confdir}/$_guest.xml");
if (script_output("xmlstarlet sel -T -t -v \"//devices/interface/\@type\" $args{confdir}/$_guest.xml", type_command => 1, proceed_on_failure => 1) eq 'network') {
$_temp |= script_run("xmlstarlet ed --inplace --delete \"/domain/devices/interface/source/\@bridge\" $args{confdir}/$_guest.xml");
}
$_ret |= $_temp;
record_info("Guest $_guest asset saving failed", "Failed to save guest $_guest asset", result => 'fail') if ($_temp != 0);
}
save_screenshot;
croak("Guest asset saving for certain guest failed") if ($_ret != 0 and $args{die} == 1);
return $_ret;
}
=head2 restore_guest_asset
Find guest disk and xml config with domain name, restore them to their original
names and places. Main arguments are guest to be manipulated, whether die (1)
or not (0) if any failures happen, directories in which guest asset and config
are stored. This subroutine also calls construct_uri to determine the desired
URI to be connected if the interested party is not localhost. Please refer to
subroutine construct_uri for the arguments related.
=cut
sub restore_guest_asset {
my ($self, %args) = @_;
$args{guest} //= '';
$args{assetdir} //= '/var/lib/libvirt/images';
$args{confdir} //= '/var/lib/libvirt/images';
$args{die} //= 0;
$args{driver} //= '';
$args{transport} //= 'ssh';
$args{user} //= '';
$args{host} //= 'localhost';
$args{port} //= '';
$args{path} //= 'system';
$args{extra} //= '';
croak("Guest to be restored must be given") if (!$args{guest});
my $_ret = 0;
my $_uri = "--connect=" . virt_autotest::domain_management_utils::construct_uri(driver => $args{driver}, transport => $args{transport}, user => $args{user}, host => $args{host}, port => $args{port}, path => $args{path}, extra => $args{extra});
foreach my $_guest (split(/ /, $args{guest})) {
record_info("Restore $_guest asset");
my $_temp = 1;
my $_guest_asset_name = $_guest . '_on-host_' . get_required_var('DISTRI') . '-' . get_required_var('VERSION') . '*' . get_required_var('SYSTEM_ROLE') . '_' . get_required_var('ARCH');
my $_guest_disk_downloaded = script_output("find $args{assetdir} -type f \\( -iname \"*$_guest_asset_name*disk\" -o -iname \"*$_guest_asset_name*raw\" -o -iname \"*$_guest_asset_name*qcow2\" \\) | head -1", type_command => 1, proceed_on_failure => 1);
my $_guest_config = script_output("find $args{assetdir} -type f -iname \"*$_guest_asset_name*xml\" | head -1", type_command => 1, proceed_on_failure => 1);
my $_guest_disk_original = script_output("xmlstarlet sel -T -t -v \"//devices/disk/source/\@file\" $_guest_config", type_command => 1, proceed_on_failure => 1);
$_temp = script_run("nice ionice qemu-img convert -p -f qcow2 $_guest_disk_downloaded -O qcow2 $_guest_disk_original && rm -f -r $_guest_disk_downloaded", timeout => 300);
$_temp |= script_run("mv $_guest_config $args{confdir}/$_guest.xml");
$_temp |= script_run("xmlstarlet ed --inplace --delete \"/domain/devices/interface/target\" $args{confdir}/$_guest.xml");
$_temp |= script_run("xmlstarlet ed --inplace --delete \"/domain/devices/interface/alias\" $args{confdir}/$_guest.xml");
$_temp |= script_run("xmlstarlet ed --inplace --delete \"/domain/devices/interface/source/\@portid\" $args{confdir}/$_guest.xml");
if (script_output("xmlstarlet sel -T -t -v \"//devices/interface/\@type\" $args{confdir}/$_guest.xml", type_command => 1, proceed_on_failure => 1) eq 'network') {
$_temp |= script_run("xmlstarlet ed --inplace --delete \"/domain/devices/interface/source/\@bridge\" $args{confdir}/$_guest.xml");
}
$_ret |= $_temp;
save_screenshot;
record_info("Guest $_guest asset restoring failed", "Failed to restoring guest $_guest asset", result => 'fail') if ($_temp != 0);
}
croak("Guest asset restoring for certain guest failed") if ($_ret != 0 and $args{die} == 1);
return $_ret;
}
=head2 config_guest_clock
Configure guest clock to kvm-clock or tsc. Please refer to guest migration requirements:
https://susedoc.github.io/doc-sle/main/single-html/SLES-virtualization/#libvirt-admin-live-migration-requirements
Main arguments are guest to be configured, directory in which guest xml config is
stored and whether die (1) or not (0) if any failures happen.
=cut
sub config_guest_clock {
my ($self, %args) = @_;
$args{guest} //= '';
$args{confdir} //= '/var/lib/libvirt/images';
$args{die} //= 0;
croak("Guest to be configured must be given") if (!$args{guest});
my $_ret = 0;
foreach my $_guest (split(/ /, $args{guest})) {
record_info("Config $_guest clock");
my $_temp = 1;
script_run("cp $args{confdir}/$_guest.xml $args{confdir}/$_guest.xml.backup");
$_temp = script_run("xmlstarlet ed --inplace --delete \"/domain/clock\" $args{confdir}/$_guest.xml");
$_temp |= script_run("xmlstarlet ed --inplace --subnode \"/domain\" --type elem -n clock -v \"\" $args{confdir}/$_guest.xml");
$_temp |= script_run("xmlstarlet ed --inplace --insert \"/domain/clock\" --type attr -n offset -v utc $args{confdir}/$_guest.xml");
my $_clock = is_kvm_host ? "kvm-clock" : "tsc";
$_temp |= script_run("xmlstarlet ed --inplace --insert \"/domain/clock/timer\" --type attr -n name -v $_clock --insert \"/domain/clock/timer\" --type attr -n present -v yes $args{confdir}/$_guest.xml");
$_ret |= $_temp;
if ($_temp != 0) {
script_run("mv $args{confdir}/$_guest.xml.backup $args{confdir}/$_guest.xml");
record_info("Guest $_guest clock config failed", "Failed to configure guest $_guest clock settings", result => 'fail');
}
save_screenshot;
}
croak("Clock configuration failed for certain guest") if ($_ret != 0 and $args{die} == 1);
return $_ret;
}
=head2 config_guest_storage
Configure guest storage cache mode to none. Please refer to guest migration requirements:
https://susedoc.github.io/doc-sle/main/single-html/SLES-virtualization/#libvirt-admin-live-migration-requirements
Main arguments are guest to be configured, directory in which guest xml config is
stored and whether die (1) or not (0) if any failures happen.
=cut
sub config_guest_storage {
my ($self, %args) = @_;
$args{guest} //= '';
$args{confdir} //= '/var/lib/libvirt/images';
$args{die} //= 0;
croak("Guest to be configured must be given") if (!$args{guest});
my $_ret = 0;
foreach my $_guest (split(/ /, $args{guest})) {
record_info("Config $_guest storage");
my $_temp = 1;
$_temp = script_run("cp $args{confdir}/$_guest.xml $args{confdir}/$_guest.xml.backup");
$_temp |= script_run("xmlstarlet ed --inplace --delete \"/domain/devices/disk/driver/\@cache\" $args{confdir}/$_guest.xml");
$_temp |= script_run("xmlstarlet ed --inplace --insert \"/domain/devices/disk/driver[\@name=\'qemu\']\" --type attr -n cache -v none $args{confdir}/$_guest.xml");
$_ret |= $_temp;
if ($_temp != 0) {
script_run("mv $args{confdir}/$_guest.xml.backup $args{confdir}/$_guest.xml");
record_info("Guest $_guest storage config failed", "Failed to configure guest $_guest storage settings", result => 'fail');
}
save_screenshot;
}
croak("Storage configuration failed for certain guest") if ($_ret != 0 and $args{die} == 1);
return $_ret;
}
=head2 config_guest_console
Configure serial console for guest by using libguestfs tools. Main arguments are
guest to be configured, directory in which guest xml config is stored and whether
die (1) or not (0) if any failures happen. This subroutine also calls construct_uri
to determine the desired URI to be connected if the interested party is not localhost.
Please refer to subroutine construct_uri for the arguments related.
=cut
sub config_guest_console {
my ($self, %args) = @_;
$args{guest} //= '';
$args{confdir} //= '/var/lib/libvirt/images';
$args{die} //= 0;
$args{driver} //= '';
$args{transport} //= 'ssh';
$args{user} //= '';
$args{host} //= 'localhost';
$args{port} //= '';
$args{path} //= 'system';
$args{extra} //= '';
croak("Guest to be configured must be given") if (!$args{guest});
my $_host_console = '';
my $_guest_console = '';
if (is_kvm_host) {
$_host_console = script_output("dmesg | grep -i \"console.*enabled\" | grep -ioE \"tty[A-Z]{1,}\" | head -1", type_command => 1, proceed_on_failure => 1);
$_guest_console = $_host_console ? $_host_console . 0 : 'ttyS0';
}
my $_ret = 0;
my $_guest_device = '';
my $_uri = "--connect=" . virt_autotest::domain_management_utils::construct_uri(driver => $args{driver}, transport => $args{transport}, user => $args{user}, host => $args{host}, port => $args{port}, path => $args{path}, extra => $args{extra});
foreach my $_guest (split(/ /, $args{guest})) {
record_info("Config $_guest console");
my $_temp = 1;
$_guest_console = (is_fv_guest($_guest) ? 'ttyS0' : 'hvc0') if (is_xen_host);
script_run("virsh $_uri destroy $_guest");
$_temp = script_retry("! virsh $_uri list --all | grep \"$_guest \" | grep running", delay => 1, retry => 5, die => 0);
foreach my $_dev (split(/\/n/, script_output("virt-filesystems $_uri -d $_guest | grep -ioE \"^/dev.*[^@].*\$\"", type_command => 1, proceed_on_failure => 1))) {
if (script_run("virt-ls $_uri -d $_guest -m $_dev / | grep -ioE \"^boot\$\"") == 0) {
$_guest_device = $_dev;
last;
}
}
$_temp |= script_run("virt-edit $_uri -d $_guest -m $_guest_device /boot/grub2/grub.cfg -e \"s/\$/ console=tty console=$_guest_console,115200/ if /.*(linux|kernel).*\\\/boot\\\/(vmlinuz|image).*\$/i\"");
$_ret |= $_temp;
save_screenshot;
record_info("Guest $_guest console config failed", "Failed to configure console $_guest_device for guest $_guest", result => 'fail') if ($_temp != 0);
}
croak("Console configuration failed for certain guest") if ($_ret != 0 and $args{die} == 1);
return $_ret;
}
=head2 start_guest
Start guest by using virsh start and wait for it up and running if necessary. Main
arguments are guest to start, virtualization management tool to be used, whether
restart (1) or not (0), whether die (1) or not (0) if any failures happen and whether
wait (1) or not (0) for guest up and running. This subroutine also calls construct_uri
to determine the desired URI to be connected if the interested party is not localhost.
Please refer to subroutine construct_uri for the arguments related.
=cut
sub start_guest {
my ($self, %args) = @_;
$args{guest} //= '';
$args{virttool} //= 'virsh';
$args{restart} //= 0;
$args{die} //= 0;
$args{wait} //= 1;
$args{driver} //= '';
$args{transport} //= 'ssh';
$args{user} //= '';
$args{host} //= 'localhost';
$args{port} //= '';
$args{path} //= 'system';
$args{extra} //= '';
croak("Guest to be started must be given") if (!$args{guest});
my $_ret = 0;
my $_uri = "--connect=" . virt_autotest::domain_management_utils::construct_uri(driver => $args{driver}, transport => $args{transport}, user => $args{user}, host => $args{host}, port => $args{port}, path => $args{path}, extra => $args{extra});
foreach my $_guest (split(/ /, $args{guest})) {
my $_temp = 1;
$_temp = $args{restart} == 0 ? script_run("virsh $_uri start $_guest") : script_run("virsh $_uri reboot $_guest") if ($args{virttool} eq 'virsh');
$_temp = $args{restart} == 0 ? 0 : script_run("xl reboot -F $_guest") if ($args{virttool} eq 'xl');
$_temp |= $self->wait_guest(guest => $_guest) if ($_temp == 0 and $args{wait} == 1);
$_ret |= $_temp;
save_screenshot;
record_info("Guest $_guest starting failed", "Failed to start guest $_guest by using $args{virttool} ($_uri) start/create $_guest", result => 'fail') if ($_temp != 0);
}
croak("Failed to start all guests") if ($_ret != 0 and $args{die} == 1);
return $_ret;
}
=head2 wait_guest
Wait for guest up and running by obtaining ip address, adding mapping in /etc/hosts,
and calling wait_guest_ssh. Main arguments are guest to wait, whether check ip
address (1) or not (0) and whether die (1) or not (0) if any failures happen.
=cut
sub wait_guest {
my ($self, %args) = @_;
$args{guest} //= '';
$args{checkip} //= 1;
$args{die} //= 0;
croak("Guest to wait for must be given") if (!$args{guest});
my $_ret = 0;
foreach my $_guest (split(/ /, $args{guest})) {
my $_temp = 1;