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
|
#!/usr/bin/env perl
# Copyright (C) 2010-2023 Red Hat Inc.
#
# This program 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# Stochastic testing of virt-resize.
use strict;
use warnings;
use Getopt::Long;
use Sys::Guestfs;
if ($ENV{SKIP_TEST_VIRT_RESIZE_PL}) {
print "$0: test skipped because environment variable is set\n";
exit 77
}
# So srand returns the seed.
if ($] < 5.014) {
print "$0: test skipped because perl < 5.14\n";
exit 77
}
# Byte calculations go screwy on 32 bit.
if (~1 == 4294967294) {
print "$0: this program does not work on a 32 bit host\n";
exit 77
}
# Command line arguments.
my $help = 0;
my $seed = 0;
GetOptions ("help|?" => \$help,
"seed=i" => \$seed);
if ($help) {
print "$0 [--seed=SEED]\n";
print " --seed=SEED Set the starting seed (to reproduce a test)\n";
exit 0
}
if ($seed == 0) {
# Choose a random seed.
$seed = srand ();
}
else {
# User specified --seed option.
srand ($seed);
}
$| = 1;
# The size of each partition, in MB and sectors.
my $part_size_mb = 1024;
my $part_size_sectors = 1024 * 1024 * $part_size_mb / 512;
my $expand_target_size_mb = 1200;
my $shrink_target_size_mb = 400;
# Create the handle.
my $g = Sys::Guestfs->new ();
my $backend = $g->get_backend ();
# Choose a random test.
my $part_type = "mbr";
if (rand () <= 0.5) {
$part_type = "gpt"
}
# If $nr_parts >= 4 && $part_type = "mbr" then this implies creating
# an extended partition (#4) and zero or more logical partitions.
my $nr_parts = 1 + int (rand (7));
# Currently virt-resize is broken when dealing with any extended
# partition, so don't test this for the moment.
if ($part_type eq "mbr" && $nr_parts >= 4) {
$nr_parts = 3;
}
# expand (1) or shrink (0)
my $expand = 0;
if (rand () >= 0.2) {
$expand = 1;
}
my $source_format = "raw";
if (rand () < 0.2) {
$source_format = "qcow2";
}
my $target_format = "raw";
if (rand () < 0.2) {
$target_format = "qcow2";
}
my $no_extra_partition = 0;
if ($part_type eq "mbr" && $nr_parts > 3) {
$no_extra_partition = 1;
}
if ($expand && rand () < 0.5) {
$no_extra_partition = 1;
}
# Print the test before starting, so it will appear in failure output.
print "seed: $seed\n";
print "partition type: $part_type\n";
print "nr partitions: $nr_parts\n";
print "partition size: ${part_size_mb}M ($part_size_sectors sectors)\n";
print "expand: $expand\n";
print "no extra part: $no_extra_partition\n";
print "source format: $source_format\n";
print "target format: $target_format\n";
# Make a structure for each partition, recording what it will contain
# and whether we will --resize / --expand / --shrink it.
# Note this array is numbered from 1!
my @parts;
my $i;
for ($i = 1; $i <= $nr_parts; ++$i) {
$parts[$i] = { name => "sda".$i, resize => 0 };
if ($part_type eq "mbr") {
if ($i < 4) {
if (rand () < 0.5) {
$parts[$i]->{resize} = 1;
}
} elsif ($i == 4) {
$parts[$i]->{content} = "extended";
}
} else {
if (rand () < 0.5) {
$parts[$i]->{resize} = 1;
}
}
}
# Pick a partition at random to expand or shrink.
if ($part_type eq "mbr") {
# virt-resize cannot shrink extended or logical partitions, so we
# set $max so that these cannot be chosen:
my $max = 3;
$max = $nr_parts if $max > $nr_parts;
$i = 1 + int (rand ($max));
} else {
$i = 1 + int (rand ($nr_parts));
}
$parts[$i]->{resize} = 0;
$parts[$i]->{expand_shrink} = 1;
# Size of the source disk. It's always roughly nr_parts * size of
# each partition + a bit extra. For btrfs we have to choose a large
# partition size.
my $source_size = (10 + $nr_parts * $part_size_mb) * 1024 * 1024;
# Create the source disk.
my $source_file = "test-virt-resize-source.img";
$g->disk_create ($source_file, $source_format, $source_size);
$g->add_drive ($source_file, format => $source_format);
$g->launch ();
# After launching we can detect if various filesystem types are
# supported and use that to further parameterize the test.
my $vfs_type = "ext2";
my $r = rand ();
if ($r < 0.15) {
$vfs_type = "lvm";
} elsif ($r < 0.30) {
if ($g->feature_available (["ntfs3g", "ntfsprogs"])) {
$vfs_type = "ntfs"
}
} elsif ($r < 0.45) {
if ($g->filesystem_available ("btrfs")) {
$vfs_type = "btrfs"
}
} elsif ($r < 0.60) {
# XFS cannot shrink.
if ($expand && $g->filesystem_available ("xfs")) {
$vfs_type = "xfs"
}
}
print "filesystem: $vfs_type\n";
my $lv_expand = "";
if ($vfs_type eq "lvm" && $expand && rand () < 0.5) {
$lv_expand = "/dev/VG/LV";
}
print "LV expand: $lv_expand\n";
for ($i = 1; $i <= $nr_parts; ++$i) {
$parts[$i]->{content} = $vfs_type unless exists $parts[$i]->{content};
}
for ($i = 1; $i <= $nr_parts; ++$i) {
printf "partition %d: %s %s ", $i, $parts[$i]->{name}, $parts[$i]->{content};
if ($parts[$i]->{resize}) {
print "resize"
} elsif ($parts[$i]->{expand_shrink}) {
if ($expand) {
print "expand"
}
else {
print "shrink"
}
} else {
print "-";
}
print "\n";
}
# Create the source disk partitions.
$g->part_init ("/dev/sda", $part_type);
my $start = 2048;
if ($part_type eq "gpt") {
for (my $i = 1; $i <= $nr_parts; ++$i) {
my $end = $start + $part_size_sectors - 1;
$g->part_add ("/dev/sda", "primary", $start, $end);
$start = $end+1;
}
} else {
# MBR is nuts ...
for ($i = 1; $i <= $nr_parts; ++$i) {
if ($i <= 3) {
my $end = $start + $part_size_sectors - 1;
$g->part_add ("/dev/sda", "primary", $start, $end);
$start = $end+1;
}
elsif ($i == 4) {
# Extended partition is a container, so it just stretches
# to the end of the disk.
$g->part_add ("/dev/sda", "extended", $start, -2048);
$start += 1024;
}
else {
# Logical partitions sit inside the container, but the
# confusing thing about them is we have to take into
# account the sector that contains the linked list of
# logical partitions, hence -2/+2 below.
my $end = $start + $part_size_sectors - 2;
$g->part_add ("/dev/sda", "logical", $start, $end);
$start = $end+2;
}
}
}
# Create the filesystem content.
for ($i = 1; $i <= $nr_parts; ++$i) {
my $dev = "/dev/" . $parts[$i]->{name};
my $content = $parts[$i]->{content};
if ($content eq "extended") {
# do nothing - it's the extended partition
} elsif ($content eq "lvm") {
$g->pvcreate ($dev);
# If shrinking, shrink the PV to < shrink size.
if (!$expand) {
$g->pvresize_size ($dev, ($shrink_target_size_mb-4) * 1024 * 1024);
}
} else {
$g->mkfs ($content, $dev);
# If shrinking, shrink the filesystem to < shrink size.
if (!$expand) {
if ($content eq "ext2") {
$g->resize2fs_size ($dev,
($shrink_target_size_mb-4) * 1024 * 1024);
} elsif ($content eq "btrfs") {
$g->mount ($dev, "/");
$g->btrfs_filesystem_resize
("/",
size => ($shrink_target_size_mb-4) * 1024 * 1024);
$g->umount_all ();
} elsif ($content eq "ntfs") {
$g->ntfsresize
($dev,
size => ($shrink_target_size_mb-4) * 1024 * 1024);
} else {
die "internal error: content = $content";
}
}
}
}
# For LVM, create the LV.
if ($vfs_type eq "lvm") {
my @pvs = $g->pvs ();
$g->vgcreate ("VG", \@pvs);
$g->lvcreate ("LV", "VG", 256);
$g->mkfs ("ext2", "/dev/VG/LV");
}
# Close the source.
$g->shutdown ();
$g->close ();
# What size should the target be? It depends on how many partitions
# will be resized.
my $target_size = 0;
for ($i = 1; $i <= $nr_parts; ++$i) {
if ($parts[$i]->{resize}) {
if ($expand) {
$target_size += $part_size_mb + 256;
} else {
$target_size += $part_size_mb - 256;
}
} elsif ($parts[$i]->{expand_shrink}) {
if ($expand) {
$target_size += $expand_target_size_mb;
} else {
$target_size += $shrink_target_size_mb;
}
} else {
$target_size += $part_size_mb; # remain at original size
}
}
$target_size += 10;
$target_size *= 1024 * 1024;
# Create the empty target container.
my $target_file = "test-virt-resize-target.img";
Sys::Guestfs->new ()->disk_create ($target_file, $target_format, $target_size);
# Create the virt-resize command.
my @command = (
"virt-resize",
"--debug",
"--format", $source_format,
"--output-format", $target_format,
);
if ($vfs_type eq "ntfs") {
push @command, "--ntfsresize-force"
}
for ($i = 1; $i <= $nr_parts; ++$i) {
my $dev = "/dev/" . $parts[$i]->{name};
if ($expand) {
if ($parts[$i]->{resize}) {
push @command, "--resize", $dev."=+256M";
} else {
if ($parts[$i]->{expand_shrink}) {
push @command, "--expand", $dev;
}
}
} else { # shrink
if ($parts[$i]->{resize}) {
push @command, "--resize", $dev."=-256M";
} else {
if ($parts[$i]->{expand_shrink}) {
push @command, "--shrink", $dev;
}
}
}
}
if ($lv_expand) {
push @command, "--lvexpand", $lv_expand
}
if ($no_extra_partition) {
push @command, "--no-extra-partition"
}
push @command, $source_file, $target_file;
print (join(" ", @command), "\n");
system (@command) == 0 or die "command: '@command' failed: $?\n";
# Clean up.
unlink $source_file;
unlink $target_file;
exit 0
|