1 package PVE
::GuestHelpers
;
9 use POSIX
qw(strftime);
10 use Scalar
::Util
qw(weaken);
12 # We use a separate lock to block migration while a replication job
15 our $lockdir = '/var/lock/pve-manager';
17 sub guest_migration_lock
{
18 my ($vmid, $timeout, $func, @param) = @_;
20 my $lockid = "pve-migrate-$vmid";
24 my $res = PVE
::Tools
::lock_file
("$lockdir/$lockid", $timeout, $func, @param);
30 sub check_hookscript
{
31 my ($volid, $storecfg) = @_;
33 $storecfg = PVE
::Storage
::config
() if !defined($storecfg);
34 my ($path, undef, $type) = PVE
::Storage
::path
($storecfg, $volid);
36 die "'$volid' is not in the snippets directory\n"
37 if $type ne 'snippets';
39 die "script '$volid' does not exist\n"
42 die "script '$volid' is not executable\n"
49 my ($conf, $vmid, $phase, $stop_on_error) = @_;
51 return if !$conf->{hookscript
};
54 my $hookscript = check_hookscript
($conf->{hookscript
});
57 PVE
::Tools
::run_command
([$hookscript, $vmid, $phase]);
60 my $errmsg = "hookscript error for $vmid on $phase: $err\n";
61 die $errmsg if ($stop_on_error);
66 # takes a snapshot list (e.g., qm/pct snapshot_list API call result) and
67 # prints it out in a nice tree sorted by age. Can cope with multiple roots
68 sub print_snapshot_tree
{
69 my ($snapshot_list) = @_;
71 my $snapshots = { map { $_->{name
} => $_ } @$snapshot_list };
74 foreach my $e (@$snapshot_list) {
76 if (($parent = $e->{parent
}) && defined $snapshots->{$parent}) {
77 push @{$snapshots->{$parent}->{children
}}, $e->{name
};
79 push @roots, $e->{name
};
83 # sort the elements by snaptime - with "current" (no snaptime) highest
84 my $snaptimesort = sub {
85 return +1 if !defined $snapshots->{$a}->{snaptime
};
86 return -1 if !defined $snapshots->{$b}->{snaptime
};
87 return $snapshots->{$a}->{snaptime
} <=> $snapshots->{$b}->{snaptime
};
90 # recursion function for displaying the tree
91 my $snapshottree_weak;
92 $snapshottree_weak = sub {
93 my ($prefix, $root, $snapshots) = @_;
94 my $e = $snapshots->{$root};
96 my $description = $e->{description
} || 'no-description';
97 ($description) = $description =~ m/(.*)$/m;
100 if (defined $e->{snaptime
}) {
101 $timestring = strftime
("%F %H:%M:%S", localtime($e->{snaptime
}));
104 my $len = 30 - length($prefix); # for aligning the description
105 printf("%s %-${len}s %-23s %s\n", $prefix, $root, $timestring, $description);
107 if ($e->{children
}) {
108 $prefix = " $prefix";
109 foreach my $child (sort $snaptimesort @{$e->{children
}}) {
110 $snapshottree_weak->($prefix, $child, $snapshots);
114 my $snapshottree = $snapshottree_weak;
115 weaken
($snapshottree_weak);
117 foreach my $root (sort $snaptimesort @roots) {
118 $snapshottree->('`->', $root, $snapshots);
124 foreach my $item (sort { $a->{key
} cmp $b->{key
}} @$data) {
125 my $k = $item->{key
};
126 next if $k eq 'digest';
127 my $v = $item->{value
};
128 my $p = $item->{pending
};
129 if ($k eq 'description') {
130 $v = PVE
::Tools
::encode_text
($v) if defined($v);
131 $p = PVE
::Tools
::encode_text
($p) if defined($p);
134 if ($item->{delete}) {
135 print "del $k: $v\n";
136 } elsif (defined($p)) {
137 print "cur $k: $v\n";
138 print "new $k: $p\n";
140 print "cur $k: $v\n";
142 } elsif (defined($p)) {
143 print "new $k: $p\n";
148 # returns the config as an array of hashes, each hash can have the following keys:
149 # key (the config property name, non-optional)
150 # value (the current value in effect - if any)
151 # pending (a new, still pending, value - if any)
152 # delete (when deletions are pending, this is set to either 2 (force) or 1 (graceful))
153 sub config_with_pending_array
{
154 my ($conf, $pending_delete_hash) = @_;
158 foreach my $opt (keys %$conf) {
159 next if ref($conf->{$opt});
160 my $item = { key
=> $opt };
161 $item->{value
} = $conf->{$opt} if defined($conf->{$opt});
162 $item->{pending
} = $conf->{pending
}->{$opt} if defined($conf->{pending
}->{$opt});
163 $item->{delete} = ($pending_delete_hash->{$opt}->{force
} ?
2 : 1) if exists $pending_delete_hash->{$opt};
168 foreach my $opt (keys %{$conf->{pending
}}) {
169 next if $opt eq 'delete';
170 next if ref($conf->{pending
}->{$opt}); # just to be sure
171 next if defined($conf->{$opt});
172 my $item = { key
=> $opt };
173 $item->{pending
} = $conf->{pending
}->{$opt};
178 while (my ($opt, $force) = each %$pending_delete_hash) {
179 next if $conf->{pending
}->{$opt}; # just to be sure
180 next if $conf->{$opt};
181 my $item = { key
=> $opt, delete => ($force ?
2 : 1)};