]>
git.proxmox.com Git - pve-guest-common.git/blob - PVE/GuestHelpers.pm
a1ec76f50f7bba165e546266669776fc7a5ed8cb
1 package PVE
::GuestHelpers
;
9 use POSIX
qw(strftime);
11 # We use a separate lock to block migration while a replication job
14 our $lockdir = '/var/lock/pve-manager';
16 sub guest_migration_lock
{
17 my ($vmid, $timeout, $func, @param) = @_;
19 my $lockid = "pve-migrate-$vmid";
23 my $res = PVE
::Tools
::lock_file
("$lockdir/$lockid", $timeout, $func, @param);
29 sub check_hookscript
{
30 my ($volid, $storecfg) = @_;
32 $storecfg = PVE
::Storage
::config
() if !defined($storecfg);
33 my ($path, undef, $type) = PVE
::Storage
::path
($storecfg, $volid);
35 die "'$volid' is not in the snippets directory\n"
36 if $type ne 'snippets';
38 die "script '$volid' does not exists\n"
41 die "script '$volid' is not executable\n"
48 my ($conf, $vmid, $phase, $stop_on_error) = @_;
50 return if !$conf->{hookscript
};
53 my $hookscript = check_hookscript
($conf->{hookscript
});
56 PVE
::Tools
::run_command
([$hookscript, $vmid, $phase]);
59 my $errmsg = "hookscript error for $vmid on $phase: $err\n";
60 die $errmsg if ($stop_on_error);
65 # takes a snapshot list (e.g., qm/pct snapshot_list API call result) and
66 # prints it out in a nice tree sorted by age. Can cope with multiple roots
67 sub print_snapshot_tree
{
68 my ($snapshot_list) = @_;
70 my $snapshots = { map { $_->{name
} => $_ } @$snapshot_list };
73 foreach my $e (@$snapshot_list) {
75 if (($parent = $e->{parent
}) && defined $snapshots->{$parent}) {
76 push @{$snapshots->{$parent}->{children
}}, $e->{name
};
78 push @roots, $e->{name
};
82 # sort the elements by snaptime - with "current" (no snaptime) highest
83 my $snaptimesort = sub {
84 return +1 if !defined $snapshots->{$a}->{snaptime
};
85 return -1 if !defined $snapshots->{$b}->{snaptime
};
86 return $snapshots->{$a}->{snaptime
} <=> $snapshots->{$b}->{snaptime
};
89 # recursion function for displaying the tree
92 my ($prefix, $root, $snapshots) = @_;
93 my $e = $snapshots->{$root};
95 my $description = $e->{description
} || 'no-description';
96 ($description) = $description =~ m/(.*)$/m;
99 if (defined $e->{snaptime
}) {
100 $timestring = strftime
("%F %H:%M:%S", localtime($e->{snaptime
}));
103 my $len = 30 - length($prefix); # for aligning the description
104 printf("%s %-${len}s %-23s %s\n", $prefix, $root, $timestring, $description);
106 if ($e->{children
}) {
107 $prefix = " $prefix";
108 foreach my $child (sort $snaptimesort @{$e->{children
}}) {
109 $snapshottree->($prefix, $child, $snapshots);
114 foreach my $root (sort $snaptimesort @roots) {
115 $snapshottree->('`->', $root, $snapshots);
121 foreach my $item (sort { $a->{key
} cmp $b->{key
}} @$data) {
122 my $k = $item->{key
};
123 next if $k eq 'digest';
124 my $v = $item->{value
};
125 my $p = $item->{pending
};
126 if ($k eq 'description') {
127 $v = PVE
::Tools
::encode_text
($v) if defined($v);
128 $p = PVE
::Tools
::encode_text
($p) if defined($p);
131 if ($item->{delete}) {
132 print "del $k: $v\n";
133 } elsif (defined($p)) {
134 print "cur $k: $v\n";
135 print "new $k: $p\n";
137 print "cur $k: $v\n";
139 } elsif (defined($p)) {
140 print "new $k: $p\n";
145 sub conf_table_with_pending
{
146 my ($conf, $pending_delete_hash) = @_;
150 foreach my $opt (keys %$conf) {
151 next if ref($conf->{$opt});
152 my $item = { key
=> $opt };
153 $item->{value
} = $conf->{$opt} if defined($conf->{$opt});
154 $item->{pending
} = $conf->{pending
}->{$opt} if defined($conf->{pending
}->{$opt});
155 $item->{delete} = ($pending_delete_hash->{$opt}->{force
} ?
2 : 1) if exists $pending_delete_hash->{$opt};
160 foreach my $opt (keys %{$conf->{pending
}}) {
161 next if $opt eq 'delete';
162 next if ref($conf->{pending
}->{$opt}); # just to be sure
163 next if defined($conf->{$opt});
164 my $item = { key
=> $opt };
165 $item->{pending
} = $conf->{pending
}->{$opt};
170 while (my ($opt, $force) = each %$pending_delete_hash) {
171 next if $conf->{pending
}->{$opt}; # just to be sure
172 next if $conf->{$opt};
173 my $item = { key
=> $opt, delete => ($force ?
2 : 1)};