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
|
/* libguestfs
* Copyright (C) 2009-2020 Red Hat Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; 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 <string.h>
#include <stdbool.h>
#include <inttypes.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/signal.h>
#include <libintl.h>
#include "cloexec.h"
#include "guestfs.h"
#include "guestfs-internal.h"
#include "guestfs_protocol.h"
/* Per-handle data. */
struct backend_uml_data {
pid_t pid; /* vmlinux PID. */
pid_t recoverypid; /* Recovery process PID. */
#define UML_UMID_LEN 16
char umid[UML_UMID_LEN+1]; /* umid=<...> unique ID. */
};
static void print_vmlinux_command_line (guestfs_h *g, char **argv);
/* Run uml_mkcow to create a COW overlay. */
static char *
make_cow_overlay (guestfs_h *g, const char *original)
{
CLEANUP_CMD_CLOSE struct command *cmd = guestfs_int_new_command (g);
char *overlay;
int r;
overlay = guestfs_int_make_temp_path (g, "overlay", "qcow2");
if (!overlay)
return NULL;
guestfs_int_cmd_add_arg (cmd, "uml_mkcow");
guestfs_int_cmd_add_arg (cmd, overlay);
guestfs_int_cmd_add_arg (cmd, original);
r = guestfs_int_cmd_run (cmd);
if (r == -1) {
free (overlay);
return NULL;
}
if (!WIFEXITED (r) || WEXITSTATUS (r) != 0) {
guestfs_int_external_command_failed (g, r, "uml_mkcow", original);
free (overlay);
return NULL;
}
return overlay;
}
static char *
create_cow_overlay_uml (guestfs_h *g, void *datav, struct drive *drv)
{
return make_cow_overlay (g, drv->src.u.path);
}
/* Test for features which are not supported by the UML backend.
* Possibly some of these should just be warnings, not errors.
*/
static bool
uml_supported (guestfs_h *g)
{
size_t i;
struct drive *drv;
if (g->enable_network) {
error (g, _("uml backend does not support networking"));
return false;
}
if (g->smp > 1) {
error (g, _("uml backend does not support SMP"));
return false;
}
ITER_DRIVES (g, i, drv) {
if (drv->src.protocol != drive_protocol_file) {
error (g, _("uml backend does not support remote drives"));
return false;
}
if (drv->src.format && STRNEQ (drv->src.format, "raw")) {
error (g, _("uml backend does not support non-raw-format drives"));
return false;
}
if (drv->iface) {
error (g,
_("uml backend does not support drives with ‘iface’ parameter"));
return false;
}
if (drv->disk_label) {
error (g,
_("uml backend does not support drives with ‘label’ parameter"));
return false;
}
/* Note that discard == "besteffort" is fine. */
if (drv->discard == discard_enable) {
error (g,
_("uml backend does not support drives with ‘discard’ parameter set to ‘enable’"));
return false;
}
if (drv->blocksize) {
error (g,
_("uml backend does not support drives with ‘blocksize’ parameter"));
return false;
}
}
return true;
}
static int
launch_uml (guestfs_h *g, void *datav, const char *arg)
{
struct backend_uml_data *data = datav;
CLEANUP_FREE_STRINGSBUF DECLARE_STRINGSBUF (cmdline);
int console_sock = -1, daemon_sock = -1;
int r;
int csv[2], dsv[2];
CLEANUP_FREE char *kernel = NULL, *initrd = NULL, *appliance = NULL;
int has_appliance_drive;
CLEANUP_FREE char *appliance_cow = NULL;
uint32_t size;
CLEANUP_FREE void *buf = NULL;
struct drive *drv;
size_t i;
struct hv_param *hp;
char *term = getenv ("TERM");
if (!uml_supported (g))
return -1;
if (!g->nr_drives) {
error (g, _("you must call guestfs_add_drive before guestfs_launch"));
return -1;
}
/* Assign a random unique ID to this run. */
if (guestfs_int_random_string (data->umid, UML_UMID_LEN) == -1) {
perrorf (g, "guestfs_int_random_string");
return -1;
}
/* Locate and/or build the appliance. */
if (guestfs_int_build_appliance (g, &kernel, &initrd, &appliance) == -1)
return -1;
has_appliance_drive = appliance != NULL;
/* Create COW overlays for the appliance. Note that the documented
* syntax ubd0=cow,orig does not work since kernel 3.3. See:
* http://thread.gmane.org/gmane.linux.uml.devel/13556
*/
if (has_appliance_drive) {
appliance_cow = make_cow_overlay (g, appliance);
if (!appliance_cow)
goto cleanup0;
}
/* The socket that the daemon will talk to us on.
*/
if (socketpair (AF_LOCAL, SOCK_STREAM|SOCK_CLOEXEC, 0, dsv) == -1) {
perrorf (g, "socketpair");
goto cleanup0;
}
/* The console socket. */
if (!g->direct_mode) {
if (socketpair (AF_LOCAL, SOCK_STREAM|SOCK_CLOEXEC, 0, csv) == -1) {
perrorf (g, "socketpair");
close (dsv[0]);
close (dsv[1]);
goto cleanup0;
}
}
/* Construct the vmlinux command line. We have to do this before
* forking, because after fork we are not allowed to use
* non-signal-safe functions such as malloc.
*/
#define ADD_CMDLINE(str) \
guestfs_int_add_string (g, &cmdline, (str))
#define ADD_CMDLINE_PRINTF(fs,...) \
guestfs_int_add_sprintf (g, &cmdline, (fs), ##__VA_ARGS__)
ADD_CMDLINE (g->hv);
/* Give this instance a unique random ID. */
ADD_CMDLINE_PRINTF ("umid=%s", data->umid);
/* Set memory size. */
ADD_CMDLINE_PRINTF ("mem=%dM", g->memsize);
/* vmlinux appears to ignore this, but let's add it anyway. */
ADD_CMDLINE_PRINTF ("initrd=%s", initrd);
/* Make sure our appliance init script runs first. */
ADD_CMDLINE ("init=/init");
/* This tells the /init script not to reboot at the end. */
ADD_CMDLINE ("guestfs_noreboot=1");
/* Root filesystem should be mounted read-write (default seems to
* be "ro").
*/
ADD_CMDLINE ("rw");
/* See also guestfs_int_appliance_command_line. */
if (g->verbose)
ADD_CMDLINE ("guestfs_verbose=1");
ADD_CMDLINE ("panic=1");
ADD_CMDLINE_PRINTF ("TERM=%s", term ? term : "linux");
if (g->selinux)
ADD_CMDLINE ("selinux=1 enforcing=0");
else
ADD_CMDLINE ("selinux=0");
/* XXX This isn't quite right. Multiple append args won't work. */
if (g->append)
ADD_CMDLINE (g->append);
/* Add the drives. */
ITER_DRIVES (g, i, drv) {
if (!drv->overlay)
ADD_CMDLINE_PRINTF ("ubd%zu=%s", i, drv->src.u.path);
else
ADD_CMDLINE_PRINTF ("ubd%zu=%s", i, drv->overlay);
}
/* Add the ext2 appliance drive (after all the drives). */
if (has_appliance_drive) {
char drv_name[64] = "ubd";
guestfs_int_drive_name (g->nr_drives, &drv_name[3]);
ADD_CMDLINE_PRINTF ("ubd%zu=%s", g->nr_drives, appliance_cow);
ADD_CMDLINE_PRINTF ("root=/dev/%s", drv_name);
}
/* Create the daemon socket. */
ADD_CMDLINE_PRINTF ("ssl3=fd:%d", dsv[1]);
ADD_CMDLINE ("guestfs_channel=/dev/ttyS3");
/* Add any vmlinux parameters. */
for (hp = g->hv_params; hp; hp = hp->next) {
ADD_CMDLINE (hp->hv_param);
if (hp->hv_value)
ADD_CMDLINE (hp->hv_value);
}
/* Finish off the command line. */
guestfs_int_end_stringsbuf (g, &cmdline);
r = fork ();
if (r == -1) {
perrorf (g, "fork");
if (!g->direct_mode) {
close (csv[0]);
close (csv[1]);
}
close (dsv[0]);
close (dsv[1]);
goto cleanup0;
}
if (r == 0) { /* Child (vmlinux). */
/* Set up the daemon socket for the child. */
close (dsv[0]);
set_cloexec_flag (dsv[1], 0); /* so it doesn't close across exec */
if (!g->direct_mode) {
/* Set up stdin, stdout, stderr. */
close (0);
close (1);
close (csv[0]);
/* We set the FD_CLOEXEC flag on the socket above, but now (in
* the child) it's safe to unset this flag so vmlinux can use the
* socket.
*/
set_cloexec_flag (csv[1], 0);
/* Stdin. */
if (dup (csv[1]) == -1) {
dup_failed:
perror ("dup failed");
_exit (EXIT_FAILURE);
}
/* Stdout. */
if (dup (csv[1]) == -1)
goto dup_failed;
/* Send stderr to the pipe as well. */
close (2);
if (dup (csv[1]) == -1)
goto dup_failed;
close (csv[1]);
/* RHBZ#1123007 */
close_file_descriptors (fd > 2 && fd != dsv[1]);
}
/* RHBZ#1460338. */
guestfs_int_unblock_sigterm ();
/* Dump the command line (after setting up stderr above). */
if (g->verbose)
print_vmlinux_command_line (g, cmdline.argv);
/* Put vmlinux in a new process group. */
if (g->pgroup)
setpgid (0, 0);
setenv ("LC_ALL", "C", 1);
execv (g->hv, cmdline.argv); /* Run vmlinux. */
perror (g->hv);
_exit (EXIT_FAILURE);
}
/* Parent (library). */
data->pid = r;
/* Fork the recovery process off which will kill vmlinux if the
* parent process fails to do so (eg. if the parent segfaults).
*/
data->recoverypid = -1;
if (g->recovery_proc) {
r = fork ();
if (r == 0) {
struct sigaction sa;
pid_t vmlinux_pid = data->pid;
pid_t parent_pid = getppid ();
/* Remove all signal handlers. See the justification here:
* https://www.redhat.com/archives/libvir-list/2008-August/msg00303.html
* We don't mask signal handlers yet, so this isn't completely
* race-free, but better than not doing it at all.
*/
memset (&sa, 0, sizeof sa);
sa.sa_handler = SIG_DFL;
sa.sa_flags = 0;
sigemptyset (&sa.sa_mask);
for (i = 1; i < NSIG; ++i)
sigaction (i, &sa, NULL);
/* Close all other file descriptors. This ensures that we don't
* hold open (eg) pipes from the parent process.
*/
close_file_descriptors (1);
/* RHBZ#1460338 */
guestfs_int_unblock_sigterm ();
/* It would be nice to be able to put this in the same process
* group as vmlinux (ie. setpgid (0, vmlinux_pid)). However
* this is not possible because we don't have any guarantee here
* that the vmlinux process has started yet.
*/
if (g->pgroup)
setpgid (0, 0);
/* Writing to argv is hideously complicated and error prone. See:
* http://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/utils/misc/ps_status.c;hb=HEAD
*/
/* Loop around waiting for one or both of the other processes to
* disappear. It's fair to say this is very hairy. The PIDs that
* we are looking at might be reused by another process. We are
* effectively polling. Is the cure worse than the disease?
*/
for (;;) {
if (kill (vmlinux_pid, 0) == -1)
/* vmlinux's gone away, we aren't needed */
_exit (EXIT_SUCCESS);
if (kill (parent_pid, 0) == -1) {
/* Parent's gone away, vmlinux still around, so kill vmlinux. */
kill (data->pid, SIGKILL);
_exit (EXIT_SUCCESS);
}
sleep (2);
}
}
/* Don't worry, if the fork failed, this will be -1. The recovery
* process isn't essential.
*/
data->recoverypid = r;
}
if (!g->direct_mode) {
/* Close the other end of the console socketpair. */
close (csv[1]);
console_sock = csv[0]; /* stdin of child */
csv[0] = -1;
}
daemon_sock = dsv[0];
close (dsv[1]);
dsv[0] = -1;
g->state = LAUNCHING;
/* Wait for vmlinux to start and to connect back to us via
* virtio-serial and send the GUESTFS_LAUNCH_FLAG message.
*/
g->conn =
guestfs_int_new_conn_socket_connected (g, daemon_sock, console_sock);
if (!g->conn)
goto cleanup1;
/* g->conn now owns these sockets. */
daemon_sock = console_sock = -1;
/* We now have to wait for vmlinux to start up, the daemon to start
* running, and for it to send the GUESTFS_LAUNCH_FLAG to us.
*/
r = guestfs_int_recv_from_daemon (g, &size, &buf);
if (r == -1) {
guestfs_int_launch_failed_error (g);
goto cleanup1;
}
if (size != GUESTFS_LAUNCH_FLAG) {
guestfs_int_launch_failed_error (g);
goto cleanup1;
}
debug (g, "appliance is up");
/* This is possible in some really strange situations, such as
* guestfsd starts up OK but then vmlinux immediately exits. Check
* for it because the caller is probably expecting to be able to
* send commands after this function returns.
*/
if (g->state != READY) {
error (g, _("vmlinux launched and contacted daemon, but state != READY"));
goto cleanup1;
}
if (has_appliance_drive)
guestfs_int_add_dummy_appliance_drive (g);
return 0;
cleanup1:
if (!g->direct_mode && csv[0] >= 0)
close (csv[0]);
if (dsv[0] >= 0)
close (dsv[0]);
if (data->pid > 0) kill (data->pid, SIGKILL);
if (data->recoverypid > 0) kill (data->recoverypid, SIGKILL);
if (data->pid > 0) guestfs_int_waitpid_noerror (data->pid);
if (data->recoverypid > 0) guestfs_int_waitpid_noerror (data->recoverypid);
data->pid = 0;
data->recoverypid = 0;
memset (&g->launch_t, 0, sizeof g->launch_t);
cleanup0:
if (daemon_sock >= 0)
close (daemon_sock);
if (console_sock >= 0)
close (console_sock);
if (g->conn) {
g->conn->ops->free_connection (g, g->conn);
g->conn = NULL;
}
g->state = CONFIG;
return -1;
}
/* This is called from the forked subprocess just before vmlinux runs,
* so it can just print the message straight to stderr, where it will
* be picked up and funnelled through the usual appliance event API.
*/
static void
print_vmlinux_command_line (guestfs_h *g, char **argv)
{
size_t i = 0;
int needs_quote;
struct timeval tv;
gettimeofday (&tv, NULL);
fprintf (stderr, "[%05" PRIi64 "ms] ",
guestfs_int_timeval_diff (&g->launch_t, &tv));
while (argv[i]) {
if (i > 0) fputc (' ', stderr);
/* Does it need shell quoting? This only deals with simple cases. */
needs_quote = strcspn (argv[i], " ") != strlen (argv[i]);
if (needs_quote) fputc ('\'', stderr);
fprintf (stderr, "%s", argv[i]);
if (needs_quote) fputc ('\'', stderr);
i++;
}
fputc ('\n', stderr);
}
static int
shutdown_uml (guestfs_h *g, void *datav, int check_for_errors)
{
struct backend_uml_data *data = datav;
int ret = 0;
int status;
/* Signal vmlinux to shutdown cleanly, and kill the recovery process. */
if (data->pid > 0) {
debug (g, "sending SIGTERM to process %d", data->pid);
kill (data->pid, SIGTERM);
}
if (data->recoverypid > 0) kill (data->recoverypid, 9);
/* Wait for subprocess(es) to exit. */
if (data->pid > 0) {
if (guestfs_int_waitpid (g, data->pid, &status, "vmlinux") == -1)
ret = -1;
/* Note it's normal for the pre-3.11 vmlinux process to exit with
* status "killed by signal 15" (where 15 == SIGTERM). Post 3.11
* the exit status can normally be 1.
*
* So don't consider those to be an error.
*/
else if (!(WIFSIGNALED (status) && WTERMSIG (status) == SIGTERM) &&
!(WIFEXITED (status) && WEXITSTATUS (status) == 0) &&
!(WIFEXITED (status) && WEXITSTATUS (status) == 1)) {
guestfs_int_external_command_failed (g, status, g->hv, NULL);
ret = -1;
}
}
if (data->recoverypid > 0) guestfs_int_waitpid_noerror (data->recoverypid);
data->pid = data->recoverypid = 0;
return ret;
}
static int
get_pid_uml (guestfs_h *g, void *datav)
{
struct backend_uml_data *data = datav;
if (data->pid > 0)
return data->pid;
else {
error (g, "get_pid: no vmlinux subprocess");
return -1;
}
}
/* UML appears to use a single major, and puts ubda at minor 0 with
* each partition at minors 1-15, ubdb at minor 16, etc. So the
* maximum is 256/16 = 16. However one disk is used by the appliance,
* so it's one less than this. I tested both 15 & 16 disks, and found
* that 15 worked and 16 failed.
*/
static int
max_disks_uml (guestfs_h *g, void *datav)
{
return 15;
}
static struct backend_ops backend_uml_ops = {
.data_size = sizeof (struct backend_uml_data),
.create_cow_overlay = create_cow_overlay_uml,
.launch = launch_uml,
.shutdown = shutdown_uml,
.get_pid = get_pid_uml,
.max_disks = max_disks_uml,
};
void
guestfs_int_init_uml_backend (void)
{
guestfs_int_register_backend ("uml", &backend_uml_ops);
}
|