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
|
/* Test libguestfs with large/maximum number of disks.
* Copyright (C) 2012-2020 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.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <getopt.h>
#include <errno.h>
#include <error.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <assert.h>
#include <guestfs.h>
#include "guestfs-utils.h"
#include "getprogname.h"
static ssize_t get_max_disks (guestfs_h *g);
static void do_test (guestfs_h *g, size_t ndisks, bool just_add);
static void make_disks (const char *tmpdir);
static void rm_disks (void);
static size_t ndisks;
static char **disks;
static void __attribute__((noreturn))
usage (int status)
{
if (status != EXIT_SUCCESS)
fprintf (stderr, "Try ‘%s --help’ for more information.\n",
getprogname ());
else {
printf ("Test libguestfs with large/maximum number of disks.\n"
"\n"
"Usage:\n"
" %s -n NR_DISKS\n"
" Do a full test with NR_DISKS.\n"
" %s --max\n"
" Do a full test with the max number of disks *.\n"
" %s --just-add [-n N | --max]\n"
" Don't do a full test, only add the disks and exit.\n"
"\n"
"Options:\n"
" --help Display this help and exit.\n"
" --just-add Only add the disks and exit if successful.\n"
" --max Test max disks possible *.\n"
" -n NR_DISKS Test NR_DISKS.\n"
" -v | --verbose Enable libguestfs debugging.\n"
" -x | --trace Enable libguestfs tracing.\n"
"\n"
"* Note that the max number of disks depends on the backend and\n"
" limit on the number of open file descriptors (ulimit -n).\n",
getprogname (), getprogname (), getprogname ());
}
exit (status);
}
int
main (int argc, char *argv[])
{
enum { HELP_OPTION = CHAR_MAX + 1 };
static const char options[] = "amn:vVx";
static const struct option long_options[] = {
{ "help", 0, 0, HELP_OPTION },
{ "just-add", 0, 0, 0 },
{ "max", 0, 0, 'm' },
{ "trace", 0, 0, 'x' },
{ "verbose", 0, 0, 'v' },
{ "version", 0, 0, 'V' },
{ 0, 0, 0, 0 }
};
int c;
int option_index;
bool just_add = false;
guestfs_h *g;
char *tmpdir;
ssize_t n = -1; /* -1: not set 0: max > 0: specific value */
g = guestfs_create ();
if (g == NULL)
error (EXIT_FAILURE, errno, "guestfs_create");
for (;;) {
c = getopt_long (argc, argv, options, long_options, &option_index);
if (c == -1) break;
switch (c) {
case 0: /* options which are long only */
if (STREQ (long_options[option_index].name, "just-add"))
just_add = true;
else
error (EXIT_FAILURE, 0,
"unknown long option: %s (%d)",
long_options[option_index].name, option_index);
break;
case 'm':
n = 0;
break;
case 'n':
if (sscanf (optarg, "%zd", &n) != 1 || n <= 0)
error (EXIT_FAILURE, 0, "cannot parse -n option");
break;
case 'x':
guestfs_set_trace (g, 1);
break;
case 'v':
guestfs_set_verbose (g, 1);
break;
case 'V':
printf ("%s %s\n",
getprogname (),
PACKAGE_VERSION_FULL);
exit (EXIT_SUCCESS);
case HELP_OPTION:
usage (EXIT_SUCCESS);
default:
usage (EXIT_FAILURE);
}
}
if (n == -1)
error (EXIT_FAILURE, 0, "either -n NR_DISKS or --max must be specified");
if (n == 0) {
n = get_max_disks (g);
if (n == -1)
error (EXIT_FAILURE, 0, "cannot calculate --max disks");
}
ndisks = n;
tmpdir = guestfs_get_cachedir (g);
if (tmpdir == NULL)
exit (EXIT_FAILURE);
make_disks (tmpdir);
free (tmpdir);
atexit (rm_disks);
do_test (g, ndisks, just_add);
if (guestfs_shutdown (g) == -1)
exit (EXIT_FAILURE);
guestfs_close (g);
exit (EXIT_SUCCESS);
}
/**
* Work out the maximum number of disks that could be added to the
* libguestfs appliance, based on these factors:
*
* =over 4
*
* =item the current backend
*
* =item the max number of open file descriptors (RLIMIT_NOFILE)
*
* =back
*/
static ssize_t
get_max_disks (guestfs_h *g)
{
ssize_t ret;
struct rlimit rlim;
/* We reserve a little bit of "headroom" because qemu uses more
* file descriptors than just the disk files.
*/
const unsigned fd_headroom = 32;
ret = guestfs_max_disks (g);
if (ret == -1)
return -1;
if (getrlimit (RLIMIT_NOFILE, &rlim) == -1) {
perror ("getrlimit: RLIMIT_NOFILE");
return -1;
}
if (rlim.rlim_cur > fd_headroom) {
if ((size_t) ret > rlim.rlim_cur - fd_headroom) {
if (rlim.rlim_max > rlim.rlim_cur)
fprintf (stderr,
"%s: warning: to get more complete testing, increase\n"
"file limit up to hard limit:\n"
"\n"
"$ ulimit -Hn %lu\n"
"\n",
getprogname (), (unsigned long) rlim.rlim_max);
else {
struct passwd *pw;
unsigned long suggested_limit = ret + fd_headroom;
pw = getpwuid (geteuid ());
fprintf (stderr,
"%s: warning: to get more complete testing, increase\n"
"file descriptor limit to >= %lu.\n"
"\n"
"To do this, add this line to /etc/security/limits.conf:\n"
"\n"
"%s hard nofile %lu\n"
"\n",
getprogname (), suggested_limit,
pw ? pw->pw_name : "your_username",
suggested_limit);
}
ret = rlim.rlim_cur - fd_headroom;
}
}
printf ("max_disks = %zd\n", ret);
return ret;
}
static void
do_test (guestfs_h *g, size_t ndisks, bool just_add)
{
size_t i, j, k, n;
unsigned errors;
CLEANUP_FREE_STRING_LIST char **devices = NULL;
CLEANUP_FREE_STRING_LIST char **partitions = NULL;
for (i = 0; i < ndisks; ++i) {
if (guestfs_add_drive_opts (g, disks[i],
GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
GUESTFS_ADD_DRIVE_OPTS_CACHEMODE, "unsafe",
-1) == -1)
exit (EXIT_FAILURE);
}
if (guestfs_launch (g) == -1)
exit (EXIT_FAILURE);
/* Check the disks were added. */
devices = guestfs_list_devices (g);
if (devices == NULL)
exit (EXIT_FAILURE);
n = guestfs_int_count_strings (devices);
if (n != ndisks) {
fprintf (stderr, "%s: incorrect number of devices returned by guestfs_list_devices:\n",
getprogname ());
fprintf (stderr, "counted %zu, expecting %zu\n", n, ndisks);
fprintf (stderr, "devices:\n");
for (i = 0; i < n; ++i)
fprintf (stderr, "\t%s\n", devices[i]);
exit (EXIT_FAILURE);
}
/* If the --just-add option was given, we're done. */
if (just_add)
return;
/* Check each device has the expected drive name, eg. /dev/sda,
* /dev/sdb, ..., /dev/sdaa, ...
*/
for (i = 0; i < ndisks; ++i) {
char expected[64];
guestfs_int_drive_name (i, expected);
if (!STRSUFFIX (devices[i], expected)) {
fprintf (stderr,
"%s: incorrect device name at index %zu: "
"%s (expected suffix %s)\n",
getprogname (), i, devices[i], expected);
exit (EXIT_FAILURE);
}
}
/* Check drive index. */
for (i = 0; i < ndisks; ++i) {
int idx;
idx = guestfs_device_index (g, devices[i]);
if (idx == -1)
exit (EXIT_FAILURE);
if ((int) i != idx) {
fprintf (stderr,
"%s: incorrect device index for %s: "
"expected %zu by got %d\n",
getprogname (), devices[i], i, idx);
exit (EXIT_FAILURE);
}
}
/* Check the disk index written at the start of each disk. This
* ensures that disks are added to the appliance in the same order
* that we called guestfs_add_drive.
*/
errors = 0;
for (i = 0; i < ndisks; ++i) {
CLEANUP_FREE char *buf = NULL;
size_t j, r;
buf = guestfs_pread_device (g, devices[i], sizeof j, 0, &r);
if (buf == NULL)
exit (EXIT_FAILURE);
if (r != sizeof j)
error (EXIT_FAILURE, 0, "pread_device read incorrect number of bytes");
memcpy (&j, buf, r);
if (i != j) {
if (errors == 0)
fprintf (stderr, "%s: incorrect device enumeration\n",
getprogname ());
errors++;
fprintf (stderr, "%s at device index %zu was added with index %zu\n",
devices[i], i, j);
}
}
if (errors > 0)
exit (EXIT_FAILURE);
/* Put some data on each disk to check they are writable and
* mountable.
*/
for (i = 0; i < ndisks; ++i) {
CLEANUP_FREE char *mp = NULL;
CLEANUP_FREE char *part = NULL;
CLEANUP_FREE char *file = NULL;
CLEANUP_FREE char *data = NULL;
if (asprintf (&mp, "/mp%zu", i) == -1)
error (EXIT_FAILURE, errno, "asprintf");
if (guestfs_mkmountpoint (g, mp) == -1)
exit (EXIT_FAILURE);
/* To save time in the test, add 15 partitions to the first disk
* and last disks only, and 1 partition to every other disk. Note
* that 15 partitions is the max allowed by virtio-blk.
*/
if (i == 0 || i == ndisks-1) {
if (guestfs_part_init (g, devices[i], "gpt") == -1)
exit (EXIT_FAILURE);
for (j = 1; j <= 14; ++j) {
if (guestfs_part_add (g, devices[i], "p", 64*j, 64*j+63) == -1)
exit (EXIT_FAILURE);
}
if (guestfs_part_add (g, devices[i], "p", 64*15, -64) == -1)
exit (EXIT_FAILURE);
if (asprintf (&part, "%s15", devices[i]) == -1)
error (EXIT_FAILURE, errno, "asprintf");
}
else {
if (guestfs_part_disk (g, devices[i], "mbr") == -1)
exit (EXIT_FAILURE);
if (asprintf (&part, "%s1", devices[i]) == -1)
error (EXIT_FAILURE, errno, "asprintf");
}
if (guestfs_mkfs (g, "ext2", part) == -1)
exit (EXIT_FAILURE);
if (guestfs_mount (g, part, mp) == -1)
exit (EXIT_FAILURE);
if (asprintf (&file, "%s/disk%zu", mp, i) == -1)
error (EXIT_FAILURE, errno, "asprintf");
if (asprintf (&data, "This is disk %zu.", i) == -1)
error (EXIT_FAILURE, errno, "asprintf");
if (guestfs_write (g, file, data, strlen (data)) == -1)
exit (EXIT_FAILURE);
}
for (i = 0; i < ndisks; ++i) {
CLEANUP_FREE char *file = NULL, *expected = NULL, *actual = NULL;
if (asprintf (&file, "/mp%zu/disk%zu", i, i) == -1)
error (EXIT_FAILURE, errno, "asprintf");
if (asprintf (&expected, "This is disk %zu.", i) == -1)
error (EXIT_FAILURE, errno, "asprintf");
actual = guestfs_cat (g, file);
if (actual == NULL)
exit (EXIT_FAILURE);
if (STRNEQ (expected, actual)) {
fprintf (stderr,
"%s: unexpected content in file %s: "
"expected \"%s\", actual \"%s\"\n",
getprogname (), file,
expected, actual);
exit (EXIT_FAILURE);
}
}
/* Finally check the partition list. */
partitions = guestfs_list_partitions (g);
if (partitions == NULL)
exit (EXIT_FAILURE);
k = 0;
for (i = 0; i < ndisks; ++i) {
char dev[64];
guestfs_int_drive_name (i, dev);
if (i == 0 || i == ndisks-1) {
for (j = 1; j <= 15; ++j) {
CLEANUP_FREE char *expected = NULL;
const char *p;
if (asprintf (&expected, "%s%zu", dev, j) == -1)
error (EXIT_FAILURE, errno, "asprintf");
p = partitions[k++];
if (!STRSUFFIX (p, expected)) {
fprintf (stderr,
"%s: incorrect partition name at index %zu, %zu: "
"%s (expected suffix %s)\n",
getprogname (), i, j, p, expected);
exit (EXIT_FAILURE);
}
}
}
else {
CLEANUP_FREE char *expected = NULL;
const char *p;
if (asprintf (&expected, "%s1", dev) == -1)
error (EXIT_FAILURE, errno, "asprintf");
p = partitions[k++];
if (!STRSUFFIX (p, expected)) {
fprintf (stderr,
"%s: incorrect partition name at index %zu: "
"%s (expected suffix %s)\n",
getprogname (), i, p, expected);
exit (EXIT_FAILURE);
}
}
}
}
static void
make_disks (const char *tmpdir)
{
size_t i;
int fd;
assert (ndisks > 0);
disks = calloc (ndisks, sizeof (char *));
if (disks == NULL)
error (EXIT_FAILURE, errno, "calloc");
for (i = 0; i < ndisks; ++i) {
if (asprintf (&disks[i], "%s/testdiskXXXXXX", tmpdir) == -1)
error (EXIT_FAILURE, errno, "asprintf");
fd = mkstemp (disks[i]);
if (fd == -1)
error (EXIT_FAILURE, errno, "mkstemp: %s", disks[i]);
/* Create a raw format 1MB disk, and write the disk number at the
* start of the disk, so that we can later check that disks are
* added in the right order to the appliance.
*
* Note that we write the disk number in whatever is the current
* endian/integer size, which is fine because we'll only check it
* from the same program.
*/
if (ftruncate (fd, 1024*1024) == -1)
error (EXIT_FAILURE, errno, "ftruncate: %s", disks[i]);
if (write (fd, &i, sizeof i) != sizeof i)
error (EXIT_FAILURE, errno, "write: %s", disks[i]);
if (close (fd) == -1)
error (EXIT_FAILURE, errno, "close: %s", disks[i]);
}
}
/* Called by an atexit handler. */
static void
rm_disks (void)
{
size_t i;
if (disks == NULL)
return;
for (i = 0; i < ndisks; ++i) {
unlink (disks[i]);
free (disks[i]);
}
free (disks);
disks = NULL;
}
|