Home | History | Annotate | Line # | Download | only in rump_allserver
rump_allserver.c revision 1.22.2.1
      1  1.22.2.1   yamt /*	$NetBSD: rump_allserver.c,v 1.22.2.1 2014/05/22 11:42:49 yamt Exp $	*/
      2       1.1  pooka 
      3       1.1  pooka /*-
      4      1.16  pooka  * Copyright (c) 2010, 2011 Antti Kantee.  All Rights Reserved.
      5       1.1  pooka  *
      6       1.1  pooka  * Redistribution and use in source and binary forms, with or without
      7       1.1  pooka  * modification, are permitted provided that the following conditions
      8       1.1  pooka  * are met:
      9       1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     10       1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     11       1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     13       1.1  pooka  *    documentation and/or other materials provided with the distribution.
     14       1.1  pooka  *
     15       1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16       1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17       1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18       1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19       1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20       1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21       1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22       1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23       1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24       1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25       1.1  pooka  * SUCH DAMAGE.
     26       1.1  pooka  */
     27       1.1  pooka 
     28  1.22.2.1   yamt #include <rump/rumpuser_port.h>
     29  1.22.2.1   yamt 
     30       1.1  pooka #ifndef lint
     31  1.22.2.1   yamt __RCSID("$NetBSD: rump_allserver.c,v 1.22.2.1 2014/05/22 11:42:49 yamt Exp $");
     32       1.1  pooka #endif /* !lint */
     33       1.1  pooka 
     34       1.1  pooka #include <sys/types.h>
     35  1.22.2.1   yamt #include <sys/stat.h>
     36       1.1  pooka 
     37       1.6  pooka #include <dlfcn.h>
     38       1.1  pooka #include <errno.h>
     39       1.9  pooka #include <fcntl.h>
     40       1.4  pooka #include <semaphore.h>
     41  1.22.2.1   yamt #include <signal.h>
     42       1.1  pooka #include <stdio.h>
     43       1.1  pooka #include <stdlib.h>
     44  1.22.2.1   yamt #include <stdint.h>
     45       1.1  pooka #include <string.h>
     46       1.1  pooka #include <unistd.h>
     47  1.22.2.1   yamt 
     48  1.22.2.1   yamt #include <rump/rump.h>
     49  1.22.2.1   yamt #include <rump/rump_syscalls.h>
     50  1.22.2.1   yamt #include <rump/rumpdefs.h>
     51  1.22.2.1   yamt #include <rump/rumperr.h>
     52       1.1  pooka 
     53      1.22  joerg __dead static void
     54       1.1  pooka usage(void)
     55       1.1  pooka {
     56       1.1  pooka 
     57  1.22.2.1   yamt #ifndef PLATFORM_HAS_SETGETPROGNAME
     58  1.22.2.1   yamt #define getprogname() "rump_server"
     59  1.22.2.1   yamt #endif
     60      1.13    wiz 	fprintf(stderr, "usage: %s [-s] [-c ncpu] [-d drivespec] [-l libs] "
     61      1.12  pooka 	    "[-m modules] bindurl\n", getprogname());
     62       1.1  pooka 	exit(1);
     63       1.1  pooka }
     64       1.1  pooka 
     65      1.22  joerg __dead static void
     66  1.22.2.1   yamt diedie(int sflag, const char *reason, int error, const char *errstr)
     67       1.2  pooka {
     68       1.2  pooka 
     69  1.22.2.1   yamt 	if (reason != NULL)
     70  1.22.2.1   yamt 		fputs(reason, stderr);
     71  1.22.2.1   yamt 	if (errstr) {
     72  1.22.2.1   yamt 		fprintf(stderr, ": %s", errstr);
     73  1.22.2.1   yamt 	}
     74  1.22.2.1   yamt 	fputc('\n', stderr);
     75       1.2  pooka 	if (!sflag)
     76       1.2  pooka 		rump_daemonize_done(error);
     77       1.2  pooka 	exit(1);
     78       1.2  pooka }
     79       1.2  pooka 
     80  1.22.2.1   yamt __dead static void
     81  1.22.2.1   yamt die(int sflag, int error, const char *reason)
     82  1.22.2.1   yamt {
     83  1.22.2.1   yamt 
     84  1.22.2.1   yamt 	diedie(sflag, reason, error, error == 0 ? NULL : strerror(error));
     85  1.22.2.1   yamt }
     86  1.22.2.1   yamt 
     87  1.22.2.1   yamt __dead static void
     88  1.22.2.1   yamt die_rumperr(int sflag, int error, const char *reason)
     89  1.22.2.1   yamt {
     90  1.22.2.1   yamt 
     91  1.22.2.1   yamt 	diedie(sflag, reason, error, error == 0 ? NULL : rump_strerror(error));
     92  1.22.2.1   yamt }
     93  1.22.2.1   yamt 
     94       1.4  pooka static sem_t sigsem;
     95       1.4  pooka static void
     96       1.4  pooka sigreboot(int sig)
     97       1.4  pooka {
     98       1.4  pooka 
     99       1.4  pooka 	sem_post(&sigsem);
    100       1.4  pooka }
    101       1.4  pooka 
    102       1.9  pooka static const char *const disktokens[] = {
    103       1.9  pooka #define DKEY 0
    104       1.9  pooka 	"key",
    105       1.9  pooka #define DFILE 1
    106       1.9  pooka 	"hostpath",
    107       1.9  pooka #define DSIZE 2
    108      1.18  pooka #define DSIZE_E -1
    109       1.9  pooka 	"size",
    110      1.14  pooka #define DOFFSET 3
    111      1.14  pooka 	"offset",
    112      1.16  pooka #define DLABEL 4
    113      1.16  pooka 	"disklabel",
    114      1.17  pooka #define DTYPE 5
    115      1.17  pooka 	"type",
    116       1.9  pooka 	NULL
    117       1.9  pooka };
    118       1.9  pooka 
    119       1.9  pooka struct etfsreg {
    120       1.9  pooka 	const char *key;
    121       1.9  pooka 	const char *hostpath;
    122       1.9  pooka 	off_t flen;
    123      1.14  pooka 	off_t foffset;
    124      1.16  pooka 	char partition;
    125       1.9  pooka 	enum rump_etfs_type type;
    126       1.9  pooka };
    127       1.9  pooka 
    128      1.17  pooka struct etfstype {
    129      1.17  pooka 	const char *name;
    130      1.17  pooka 	enum rump_etfs_type type;
    131      1.17  pooka } etfstypes[] = {
    132      1.17  pooka 	{ "blk", RUMP_ETFS_BLK },
    133      1.17  pooka 	{ "chr", RUMP_ETFS_CHR },
    134      1.17  pooka 	{ "reg", RUMP_ETFS_REG },
    135      1.17  pooka };
    136      1.17  pooka 
    137  1.22.2.1   yamt static void processlabel(int, int, int, off_t *, off_t *);
    138  1.22.2.1   yamt 
    139  1.22.2.1   yamt #define ALLOCCHUNK 32
    140  1.22.2.1   yamt 
    141       1.1  pooka int
    142       1.1  pooka main(int argc, char *argv[])
    143       1.1  pooka {
    144       1.2  pooka 	const char *serverurl;
    145       1.9  pooka 	struct etfsreg *etfs = NULL;
    146       1.9  pooka 	unsigned netfs = 0, curetfs = 0;
    147       1.1  pooka 	int error;
    148  1.22.2.1   yamt 	int ch, sflag, onthepath;
    149  1.22.2.1   yamt 	unsigned i;
    150  1.22.2.1   yamt 	char **modarray = NULL, **libarray = NULL;
    151  1.22.2.1   yamt 	unsigned nmods = 0, curmod = 0, nlibs = 0, curlib = 0, libidx;
    152  1.22.2.1   yamt 	unsigned liblast = -1; /* XXXgcc */
    153       1.1  pooka 
    154  1.22.2.1   yamt #ifdef PLATFORM_HAS_SETGETPROGNAME
    155       1.1  pooka 	setprogname(argv[0]);
    156  1.22.2.1   yamt #endif
    157       1.1  pooka 
    158       1.2  pooka 	sflag = 0;
    159      1.20  pooka 	while ((ch = getopt(argc, argv, "c:d:l:m:r:sv")) != -1) {
    160       1.2  pooka 		switch (ch) {
    161      1.12  pooka 		case 'c':
    162      1.12  pooka 			setenv("RUMP_NCPU", optarg, 1);
    163      1.12  pooka 			break;
    164       1.9  pooka 		case 'd': {
    165       1.9  pooka 			char *options, *value;
    166       1.9  pooka 			char *key, *hostpath;
    167      1.14  pooka 			long long flen, foffset;
    168      1.16  pooka 			char partition;
    169      1.17  pooka 			int ftype;
    170       1.9  pooka 
    171      1.14  pooka 			flen = foffset = 0;
    172      1.16  pooka 			partition = 0;
    173       1.9  pooka 			key = hostpath = NULL;
    174      1.17  pooka 			ftype = -1;
    175       1.9  pooka 			options = optarg;
    176       1.9  pooka 			while (*options) {
    177       1.9  pooka 				switch (getsubopt(&options,
    178       1.9  pooka 				    __UNCONST(disktokens), &value)) {
    179       1.9  pooka 				case DKEY:
    180      1.11  pooka 					if (key != NULL) {
    181      1.11  pooka 						fprintf(stderr,
    182      1.11  pooka 						    "key already given\n");
    183      1.11  pooka 						usage();
    184      1.11  pooka 					}
    185       1.9  pooka 					key = value;
    186       1.9  pooka 					break;
    187      1.16  pooka 
    188       1.9  pooka 				case DFILE:
    189      1.11  pooka 					if (hostpath != NULL) {
    190      1.11  pooka 						fprintf(stderr,
    191      1.11  pooka 						    "hostpath already given\n");
    192      1.11  pooka 						usage();
    193      1.11  pooka 					}
    194       1.9  pooka 					hostpath = value;
    195       1.9  pooka 					break;
    196      1.16  pooka 
    197       1.9  pooka 				case DSIZE:
    198      1.11  pooka 					if (flen != 0) {
    199      1.11  pooka 						fprintf(stderr,
    200      1.11  pooka 						    "size already given\n");
    201      1.11  pooka 						usage();
    202      1.11  pooka 					}
    203      1.19  pooka 					if (strcmp(value, "host") == 0) {
    204      1.18  pooka 						if (foffset != 0) {
    205      1.18  pooka 							fprintf(stderr,
    206      1.18  pooka 							    "cannot specify "
    207      1.18  pooka 							    "offset with "
    208      1.21  pooka 							    "size=host\n");
    209      1.18  pooka 							usage();
    210      1.18  pooka 						}
    211      1.18  pooka 						flen = DSIZE_E;
    212      1.18  pooka 					} else {
    213  1.22.2.1   yamt #ifdef PLATFORM_HAS_STRSUFTOLL
    214      1.18  pooka 						/* XXX: off_t max? */
    215      1.18  pooka 						flen = strsuftoll("-d size",
    216      1.18  pooka 						    value, 0, LLONG_MAX);
    217  1.22.2.1   yamt #else
    218  1.22.2.1   yamt 						flen = strtoull(value,
    219  1.22.2.1   yamt 						    NULL, 10);
    220  1.22.2.1   yamt #endif
    221      1.18  pooka 					}
    222       1.9  pooka 					break;
    223      1.14  pooka 				case DOFFSET:
    224      1.14  pooka 					if (foffset != 0) {
    225      1.14  pooka 						fprintf(stderr,
    226      1.14  pooka 						    "offset already given\n");
    227      1.14  pooka 						usage();
    228      1.14  pooka 					}
    229      1.18  pooka 					if (flen == DSIZE_E) {
    230      1.18  pooka 						fprintf(stderr, "cannot "
    231      1.18  pooka 						    "specify offset with "
    232      1.21  pooka 						    "size=host\n");
    233      1.18  pooka 						usage();
    234      1.18  pooka 					}
    235  1.22.2.1   yamt #ifdef PLATFORM_HAS_STRSUFTOLL
    236      1.14  pooka 					/* XXX: off_t max? */
    237      1.14  pooka 					foffset = strsuftoll("-d offset", value,
    238      1.14  pooka 					    0, LLONG_MAX);
    239  1.22.2.1   yamt #else
    240  1.22.2.1   yamt 					foffset = strtoull(value, NULL, 10);
    241  1.22.2.1   yamt #endif
    242      1.14  pooka 					break;
    243      1.16  pooka 
    244      1.16  pooka 				case DLABEL:
    245      1.16  pooka 					if (foffset != 0 || flen != 0) {
    246      1.16  pooka 						fprintf(stderr,
    247      1.16  pooka 						    "disklabel needs to be "
    248      1.16  pooka 						    "used alone\n");
    249      1.16  pooka 						usage();
    250      1.16  pooka 					}
    251      1.16  pooka 					if (strlen(value) != 1 ||
    252      1.16  pooka 					    *value < 'a' || *value > 'z') {
    253      1.16  pooka 						fprintf(stderr,
    254      1.16  pooka 						    "invalid label part\n");
    255      1.16  pooka 						usage();
    256      1.16  pooka 					}
    257      1.16  pooka 					partition = *value;
    258      1.16  pooka 					break;
    259      1.16  pooka 
    260      1.17  pooka 				case DTYPE:
    261      1.17  pooka 					if (ftype != -1) {
    262      1.17  pooka 						fprintf(stderr,
    263      1.17  pooka 						    "type already specified\n");
    264      1.17  pooka 						usage();
    265      1.17  pooka 					}
    266      1.17  pooka 
    267      1.17  pooka 					for (i = 0;
    268      1.17  pooka 					    i < __arraycount(etfstypes);
    269      1.17  pooka 					    i++) {
    270      1.17  pooka 						if (strcmp(etfstypes[i].name,
    271      1.17  pooka 						    value) == 0)
    272      1.17  pooka 							break;
    273      1.17  pooka 					}
    274      1.17  pooka 					if (i == __arraycount(etfstypes)) {
    275      1.17  pooka 						fprintf(stderr,
    276      1.17  pooka 						    "invalid type %s\n", value);
    277      1.17  pooka 						usage();
    278      1.17  pooka 					}
    279      1.17  pooka 					ftype = etfstypes[i].type;
    280      1.17  pooka 					break;
    281      1.17  pooka 
    282       1.9  pooka 				default:
    283       1.9  pooka 					fprintf(stderr, "invalid dtoken\n");
    284       1.9  pooka 					usage();
    285       1.9  pooka 					break;
    286       1.9  pooka 				}
    287       1.9  pooka 			}
    288       1.9  pooka 
    289      1.16  pooka 			if (key == NULL || hostpath == NULL ||
    290      1.16  pooka 			    (flen == 0 && partition == 0)) {
    291       1.9  pooka 				fprintf(stderr, "incomplete drivespec\n");
    292       1.9  pooka 				usage();
    293       1.9  pooka 			}
    294      1.17  pooka 			if (ftype == -1)
    295      1.17  pooka 				ftype = RUMP_ETFS_BLK;
    296       1.9  pooka 
    297       1.9  pooka 			if (netfs - curetfs == 0) {
    298  1.22.2.1   yamt 				etfs = realloc(etfs,
    299  1.22.2.1   yamt 				    (netfs+ALLOCCHUNK)*sizeof(*etfs));
    300       1.9  pooka 				if (etfs == NULL)
    301  1.22.2.1   yamt 					die(1, errno, "realloc etfs");
    302  1.22.2.1   yamt 				netfs += ALLOCCHUNK;
    303       1.9  pooka 			}
    304       1.9  pooka 
    305       1.9  pooka 			etfs[curetfs].key = key;
    306       1.9  pooka 			etfs[curetfs].hostpath = hostpath;
    307       1.9  pooka 			etfs[curetfs].flen = flen;
    308      1.14  pooka 			etfs[curetfs].foffset = foffset;
    309      1.16  pooka 			etfs[curetfs].partition = partition;
    310      1.17  pooka 			etfs[curetfs].type = ftype;
    311       1.9  pooka 			curetfs++;
    312       1.9  pooka 
    313       1.9  pooka 			break;
    314       1.9  pooka 		}
    315       1.6  pooka 		case 'l':
    316  1.22.2.1   yamt 			if (nlibs - curlib == 0) {
    317  1.22.2.1   yamt 				libarray = realloc(libarray,
    318  1.22.2.1   yamt 				    (nlibs+ALLOCCHUNK) * sizeof(char *));
    319  1.22.2.1   yamt 				if (libarray == NULL)
    320  1.22.2.1   yamt 					die(1, errno, "realloc");
    321  1.22.2.1   yamt 				nlibs += ALLOCCHUNK;
    322       1.8  pooka 			}
    323  1.22.2.1   yamt 			libarray[curlib++] = optarg;
    324       1.6  pooka 			break;
    325       1.7  pooka 		case 'm':
    326       1.7  pooka 			if (nmods - curmod == 0) {
    327       1.7  pooka 				modarray = realloc(modarray,
    328  1.22.2.1   yamt 				    (nmods+ALLOCCHUNK) * sizeof(char *));
    329       1.7  pooka 				if (modarray == NULL)
    330  1.22.2.1   yamt 					die(1, errno, "realloc");
    331  1.22.2.1   yamt 				nmods += ALLOCCHUNK;
    332       1.7  pooka 			}
    333       1.7  pooka 			modarray[curmod++] = optarg;
    334       1.7  pooka 			break;
    335      1.20  pooka 		case 'r':
    336      1.20  pooka 			setenv("RUMP_MEMLIMIT", optarg, 1);
    337      1.20  pooka 			break;
    338       1.2  pooka 		case 's':
    339       1.2  pooka 			sflag = 1;
    340       1.2  pooka 			break;
    341      1.20  pooka 		case 'v':
    342      1.20  pooka 			setenv("RUMP_VERBOSE", "1", 1);
    343      1.20  pooka 			break;
    344       1.2  pooka 		default:
    345       1.2  pooka 			usage();
    346       1.2  pooka 			/*NOTREACHED*/
    347       1.2  pooka 		}
    348       1.2  pooka 	}
    349       1.2  pooka 
    350       1.2  pooka 	argc -= optind;
    351       1.2  pooka 	argv += optind;
    352       1.2  pooka 
    353       1.2  pooka 	if (argc != 1)
    354       1.1  pooka 		usage();
    355       1.1  pooka 
    356  1.22.2.1   yamt 	/*
    357  1.22.2.1   yamt 	 * Automatically "resolve" component dependencies, i.e.
    358  1.22.2.1   yamt 	 * try to load libs in a loop until all are loaded or a
    359  1.22.2.1   yamt 	 * full loop completes with no loads (latter case is an error).
    360  1.22.2.1   yamt 	 */
    361  1.22.2.1   yamt 	for (onthepath = 1, nlibs = curlib; onthepath && nlibs > 0;) {
    362  1.22.2.1   yamt 		onthepath = 0;
    363  1.22.2.1   yamt 		for (libidx = 0; libidx < curlib; libidx++) {
    364  1.22.2.1   yamt 			/* loaded already? */
    365  1.22.2.1   yamt 			if (libarray[libidx] == NULL)
    366  1.22.2.1   yamt 				continue;
    367  1.22.2.1   yamt 
    368  1.22.2.1   yamt 			/* try to load */
    369  1.22.2.1   yamt 			liblast = libidx;
    370  1.22.2.1   yamt 			if (dlopen(libarray[libidx],
    371  1.22.2.1   yamt 			    RTLD_LAZY|RTLD_GLOBAL) == NULL) {
    372  1.22.2.1   yamt 				char pb[MAXPATHLEN];
    373  1.22.2.1   yamt 				/* try to mimic linker -l syntax */
    374  1.22.2.1   yamt 				snprintf(pb, sizeof(pb),
    375  1.22.2.1   yamt 				    "lib%s.so", libarray[libidx]);
    376  1.22.2.1   yamt 				if (dlopen(pb, RTLD_LAZY|RTLD_GLOBAL) == NULL)
    377  1.22.2.1   yamt 					continue;
    378  1.22.2.1   yamt 			}
    379  1.22.2.1   yamt 
    380  1.22.2.1   yamt 			/* managed to load that one */
    381  1.22.2.1   yamt 			libarray[libidx] = NULL;
    382  1.22.2.1   yamt 			nlibs--;
    383  1.22.2.1   yamt 			onthepath = 1;
    384  1.22.2.1   yamt 		}
    385  1.22.2.1   yamt 	}
    386  1.22.2.1   yamt 	if (nlibs > 0) {
    387  1.22.2.1   yamt 		fprintf(stderr,
    388  1.22.2.1   yamt 		    "failed to load -libraries, last error from \"%s\":\n",
    389  1.22.2.1   yamt 		    libarray[liblast]);
    390  1.22.2.1   yamt 		fprintf(stderr, "  %s", dlerror());
    391  1.22.2.1   yamt 		die(1, 0, NULL);
    392  1.22.2.1   yamt 	}
    393  1.22.2.1   yamt 	free(libarray);
    394  1.22.2.1   yamt 
    395       1.2  pooka 	serverurl = argv[0];
    396       1.2  pooka 
    397       1.2  pooka 	if (!sflag) {
    398       1.2  pooka 		error = rump_daemonize_begin();
    399       1.2  pooka 		if (error)
    400  1.22.2.1   yamt 			die_rumperr(1, error, "rump daemonize");
    401       1.2  pooka 	}
    402       1.2  pooka 
    403       1.1  pooka 	error = rump_init();
    404       1.1  pooka 	if (error)
    405  1.22.2.1   yamt 		die_rumperr(sflag, error, "rump init failed");
    406       1.7  pooka 
    407       1.9  pooka 	/* load modules */
    408       1.7  pooka 	for (i = 0; i < curmod; i++) {
    409  1.22.2.1   yamt 		struct rump_modctl_load ml;
    410       1.7  pooka 
    411       1.7  pooka #define ETFSKEY "/module.mod"
    412       1.7  pooka 		if ((error = rump_pub_etfs_register(ETFSKEY,
    413       1.7  pooka 		    modarray[0], RUMP_ETFS_REG)) != 0)
    414  1.22.2.1   yamt 			die_rumperr(sflag,
    415  1.22.2.1   yamt 			    error, "module etfs register failed");
    416       1.7  pooka 		memset(&ml, 0, sizeof(ml));
    417       1.7  pooka 		ml.ml_filename = ETFSKEY;
    418  1.22.2.1   yamt 		/*
    419  1.22.2.1   yamt 		 * XXX: since this is a syscall, error namespace depends
    420  1.22.2.1   yamt 		 * on loaded emulations.  revisit and fix.
    421  1.22.2.1   yamt 		 */
    422  1.22.2.1   yamt 		if (rump_sys_modctl(RUMP_MODCTL_LOAD, &ml) == -1)
    423       1.7  pooka 			die(sflag, errno, "module load failed");
    424       1.7  pooka 		rump_pub_etfs_remove(ETFSKEY);
    425       1.9  pooka #undef ETFSKEY
    426       1.9  pooka 	}
    427  1.22.2.1   yamt 	free(modarray);
    428       1.9  pooka 
    429       1.9  pooka 	/* register host drives */
    430       1.9  pooka 	for (i = 0; i < curetfs; i++) {
    431      1.14  pooka 		struct stat sb;
    432      1.16  pooka 		off_t foffset, flen, fendoff;
    433      1.18  pooka 		int fd, oflags;
    434       1.9  pooka 
    435      1.18  pooka 		oflags = etfs[i].flen == DSIZE_E ? 0 : O_CREAT;
    436      1.18  pooka 		fd = open(etfs[i].hostpath, O_RDWR | oflags, 0644);
    437       1.9  pooka 		if (fd == -1)
    438      1.18  pooka 			die(sflag, errno, "etfs hostpath open");
    439      1.16  pooka 
    440      1.16  pooka 		if (etfs[i].partition) {
    441  1.22.2.1   yamt 			processlabel(sflag, fd, etfs[i].partition - 'a',
    442  1.22.2.1   yamt 			    &foffset, &flen);
    443      1.16  pooka 		} else {
    444      1.16  pooka 			foffset = etfs[i].foffset;
    445      1.16  pooka 			flen = etfs[i].flen;
    446      1.16  pooka 		}
    447      1.16  pooka 
    448      1.14  pooka 		if (fstat(fd, &sb) == -1)
    449      1.14  pooka 			die(sflag, errno, "fstat etfs hostpath");
    450      1.18  pooka 		if (flen == DSIZE_E) {
    451      1.21  pooka 			if (sb.st_size == 0)
    452      1.21  pooka 				die(sflag, EINVAL, "size=host, but cannot "
    453      1.21  pooka 				    "query non-zero size");
    454      1.18  pooka 			flen = sb.st_size;
    455      1.18  pooka 		}
    456      1.18  pooka 		fendoff = foffset + flen;
    457      1.16  pooka 		if (S_ISREG(sb.st_mode) && sb.st_size < fendoff) {
    458      1.16  pooka 			if (ftruncate(fd, fendoff) == -1)
    459      1.14  pooka 				die(sflag, errno, "truncate");
    460      1.14  pooka 		}
    461       1.9  pooka 		close(fd);
    462       1.9  pooka 
    463      1.14  pooka 		if ((error = rump_pub_etfs_register_withsize(etfs[i].key,
    464      1.16  pooka 		    etfs[i].hostpath, etfs[i].type, foffset, flen)) != 0)
    465  1.22.2.1   yamt 			die_rumperr(sflag, error, "etfs register");
    466       1.7  pooka 	}
    467       1.7  pooka 
    468       1.2  pooka 	error = rump_init_server(serverurl);
    469       1.1  pooka 	if (error)
    470  1.22.2.1   yamt 		die_rumperr(sflag, error, "rump server init failed");
    471       1.2  pooka 
    472       1.2  pooka 	if (!sflag)
    473       1.3  pooka 		rump_daemonize_done(RUMP_DAEMONIZE_SUCCESS);
    474       1.2  pooka 
    475       1.4  pooka 	sem_init(&sigsem, 0, 0);
    476       1.4  pooka 	signal(SIGTERM, sigreboot);
    477       1.4  pooka 	signal(SIGINT, sigreboot);
    478       1.4  pooka 	sem_wait(&sigsem);
    479       1.4  pooka 
    480       1.4  pooka 	rump_sys_reboot(0, NULL);
    481       1.5  pooka 	/*NOTREACHED*/
    482       1.5  pooka 
    483       1.4  pooka 	return 0;
    484       1.1  pooka }
    485  1.22.2.1   yamt 
    486  1.22.2.1   yamt /*
    487  1.22.2.1   yamt  * Copyright (c) 1987, 1988, 1993
    488  1.22.2.1   yamt  *	The Regents of the University of California.  All rights reserved.
    489  1.22.2.1   yamt  *
    490  1.22.2.1   yamt  * Redistribution and use in source and binary forms, with or without
    491  1.22.2.1   yamt  * modification, are permitted provided that the following conditions
    492  1.22.2.1   yamt  * are met:
    493  1.22.2.1   yamt  * 1. Redistributions of source code must retain the above copyright
    494  1.22.2.1   yamt  *    notice, this list of conditions and the following disclaimer.
    495  1.22.2.1   yamt  * 2. Redistributions in binary form must reproduce the above copyright
    496  1.22.2.1   yamt  *    notice, this list of conditions and the following disclaimer in the
    497  1.22.2.1   yamt  *    documentation and/or other materials provided with the distribution.
    498  1.22.2.1   yamt  * 3. Neither the name of the University nor the names of its contributors
    499  1.22.2.1   yamt  *    may be used to endorse or promote products derived from this software
    500  1.22.2.1   yamt  *    without specific prior written permission.
    501  1.22.2.1   yamt  *
    502  1.22.2.1   yamt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    503  1.22.2.1   yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    504  1.22.2.1   yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    505  1.22.2.1   yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    506  1.22.2.1   yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    507  1.22.2.1   yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    508  1.22.2.1   yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    509  1.22.2.1   yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    510  1.22.2.1   yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    511  1.22.2.1   yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    512  1.22.2.1   yamt  * SUCH DAMAGE.
    513  1.22.2.1   yamt  *
    514  1.22.2.1   yamt  *	@(#)disklabel.h	8.2 (Berkeley) 7/10/94
    515  1.22.2.1   yamt  */
    516  1.22.2.1   yamt 
    517  1.22.2.1   yamt #define	RUMPSERVER_MAXPARTITIONS	22
    518  1.22.2.1   yamt #define	RUMPSERVER_DISKMAGIC		((uint32_t)0x82564557)	/* magic */
    519  1.22.2.1   yamt #define	RUMPSERVER_DEVSHIFT		9
    520  1.22.2.1   yamt 
    521  1.22.2.1   yamt struct rumpserver_disklabel {
    522  1.22.2.1   yamt 	uint32_t d_magic;		/* the magic number */
    523  1.22.2.1   yamt 	uint16_t d_type;		/* drive type */
    524  1.22.2.1   yamt 	uint16_t d_subtype;		/* controller/d_type specific */
    525  1.22.2.1   yamt 	char	 d_typename[16];	/* type name, e.g. "eagle" */
    526  1.22.2.1   yamt 
    527  1.22.2.1   yamt 	/*
    528  1.22.2.1   yamt 	 * d_packname contains the pack identifier and is returned when
    529  1.22.2.1   yamt 	 * the disklabel is read off the disk or in-core copy.
    530  1.22.2.1   yamt 	 * d_boot0 and d_boot1 are the (optional) names of the
    531  1.22.2.1   yamt 	 * primary (block 0) and secondary (block 1-15) bootstraps
    532  1.22.2.1   yamt 	 * as found in /usr/mdec.  These are returned when using
    533  1.22.2.1   yamt 	 * getdiskbyname(3) to retrieve the values from /etc/disktab.
    534  1.22.2.1   yamt 	 */
    535  1.22.2.1   yamt 	union {
    536  1.22.2.1   yamt 		char	un_d_packname[16];	/* pack identifier */
    537  1.22.2.1   yamt 		struct {
    538  1.22.2.1   yamt 			char *un_d_boot0;	/* primary bootstrap name */
    539  1.22.2.1   yamt 			char *un_d_boot1;	/* secondary bootstrap name */
    540  1.22.2.1   yamt 		} un_b;
    541  1.22.2.1   yamt 	} d_un;
    542  1.22.2.1   yamt #define	d_packname	d_un.un_d_packname
    543  1.22.2.1   yamt #define	d_boot0		d_un.un_b.un_d_boot0
    544  1.22.2.1   yamt #define	d_boot1		d_un.un_b.un_d_boot1
    545  1.22.2.1   yamt 
    546  1.22.2.1   yamt 			/* disk geometry: */
    547  1.22.2.1   yamt 	uint32_t d_secsize;		/* # of bytes per sector */
    548  1.22.2.1   yamt 	uint32_t d_nsectors;		/* # of data sectors per track */
    549  1.22.2.1   yamt 	uint32_t d_ntracks;		/* # of tracks per cylinder */
    550  1.22.2.1   yamt 	uint32_t d_ncylinders;		/* # of data cylinders per unit */
    551  1.22.2.1   yamt 	uint32_t d_secpercyl;		/* # of data sectors per cylinder */
    552  1.22.2.1   yamt 	uint32_t d_secperunit;		/* # of data sectors per unit */
    553  1.22.2.1   yamt 
    554  1.22.2.1   yamt 	/*
    555  1.22.2.1   yamt 	 * Spares (bad sector replacements) below are not counted in
    556  1.22.2.1   yamt 	 * d_nsectors or d_secpercyl.  Spare sectors are assumed to
    557  1.22.2.1   yamt 	 * be physical sectors which occupy space at the end of each
    558  1.22.2.1   yamt 	 * track and/or cylinder.
    559  1.22.2.1   yamt 	 */
    560  1.22.2.1   yamt 	uint16_t d_sparespertrack;	/* # of spare sectors per track */
    561  1.22.2.1   yamt 	uint16_t d_sparespercyl;	/* # of spare sectors per cylinder */
    562  1.22.2.1   yamt 	/*
    563  1.22.2.1   yamt 	 * Alternative cylinders include maintenance, replacement,
    564  1.22.2.1   yamt 	 * configuration description areas, etc.
    565  1.22.2.1   yamt 	 */
    566  1.22.2.1   yamt 	uint32_t d_acylinders;		/* # of alt. cylinders per unit */
    567  1.22.2.1   yamt 
    568  1.22.2.1   yamt 			/* hardware characteristics: */
    569  1.22.2.1   yamt 	/*
    570  1.22.2.1   yamt 	 * d_interleave, d_trackskew and d_cylskew describe perturbations
    571  1.22.2.1   yamt 	 * in the media format used to compensate for a slow controller.
    572  1.22.2.1   yamt 	 * Interleave is physical sector interleave, set up by the
    573  1.22.2.1   yamt 	 * formatter or controller when formatting.  When interleaving is
    574  1.22.2.1   yamt 	 * in use, logically adjacent sectors are not physically
    575  1.22.2.1   yamt 	 * contiguous, but instead are separated by some number of
    576  1.22.2.1   yamt 	 * sectors.  It is specified as the ratio of physical sectors
    577  1.22.2.1   yamt 	 * traversed per logical sector.  Thus an interleave of 1:1
    578  1.22.2.1   yamt 	 * implies contiguous layout, while 2:1 implies that logical
    579  1.22.2.1   yamt 	 * sector 0 is separated by one sector from logical sector 1.
    580  1.22.2.1   yamt 	 * d_trackskew is the offset of sector 0 on track N relative to
    581  1.22.2.1   yamt 	 * sector 0 on track N-1 on the same cylinder.  Finally, d_cylskew
    582  1.22.2.1   yamt 	 * is the offset of sector 0 on cylinder N relative to sector 0
    583  1.22.2.1   yamt 	 * on cylinder N-1.
    584  1.22.2.1   yamt 	 */
    585  1.22.2.1   yamt 	uint16_t d_rpm;		/* rotational speed */
    586  1.22.2.1   yamt 	uint16_t d_interleave;		/* hardware sector interleave */
    587  1.22.2.1   yamt 	uint16_t d_trackskew;		/* sector 0 skew, per track */
    588  1.22.2.1   yamt 	uint16_t d_cylskew;		/* sector 0 skew, per cylinder */
    589  1.22.2.1   yamt 	uint32_t d_headswitch;		/* head switch time, usec */
    590  1.22.2.1   yamt 	uint32_t d_trkseek;		/* track-to-track seek, usec */
    591  1.22.2.1   yamt 	uint32_t d_flags;		/* generic flags */
    592  1.22.2.1   yamt #define	NDDATA 5
    593  1.22.2.1   yamt 	uint32_t d_drivedata[NDDATA];	/* drive-type specific information */
    594  1.22.2.1   yamt #define	NSPARE 5
    595  1.22.2.1   yamt 	uint32_t d_spare[NSPARE];	/* reserved for future use */
    596  1.22.2.1   yamt 	uint32_t d_magic2;		/* the magic number (again) */
    597  1.22.2.1   yamt 	uint16_t d_checksum;		/* xor of data incl. partitions */
    598  1.22.2.1   yamt 
    599  1.22.2.1   yamt 			/* filesystem and partition information: */
    600  1.22.2.1   yamt 	uint16_t d_npartitions;	/* number of partitions in following */
    601  1.22.2.1   yamt 	uint32_t d_bbsize;		/* size of boot area at sn0, bytes */
    602  1.22.2.1   yamt 	uint32_t d_sbsize;		/* max size of fs superblock, bytes */
    603  1.22.2.1   yamt 	struct	rumpserver_partition {	/* the partition table */
    604  1.22.2.1   yamt 		uint32_t p_size;	/* number of sectors in partition */
    605  1.22.2.1   yamt 		uint32_t p_offset;	/* starting sector */
    606  1.22.2.1   yamt 		union {
    607  1.22.2.1   yamt 			uint32_t fsize; /* FFS, ADOS:
    608  1.22.2.1   yamt 					    filesystem basic fragment size */
    609  1.22.2.1   yamt 			uint32_t cdsession; /* ISO9660: session offset */
    610  1.22.2.1   yamt 		} __partition_u2;
    611  1.22.2.1   yamt #define	p_fsize		__partition_u2.fsize
    612  1.22.2.1   yamt #define	p_cdsession	__partition_u2.cdsession
    613  1.22.2.1   yamt 		uint8_t p_fstype;	/* filesystem type, see below */
    614  1.22.2.1   yamt 		uint8_t p_frag;	/* filesystem fragments per block */
    615  1.22.2.1   yamt 		union {
    616  1.22.2.1   yamt 			uint16_t cpg;	/* UFS: FS cylinders per group */
    617  1.22.2.1   yamt 			uint16_t sgs;	/* LFS: FS segment shift */
    618  1.22.2.1   yamt 		} __partition_u1;
    619  1.22.2.1   yamt #define	p_cpg	__partition_u1.cpg
    620  1.22.2.1   yamt #define	p_sgs	__partition_u1.sgs
    621  1.22.2.1   yamt 	} d_partitions[RUMPSERVER_MAXPARTITIONS];	/* actually may be more */
    622  1.22.2.1   yamt };
    623  1.22.2.1   yamt 
    624  1.22.2.1   yamt 
    625  1.22.2.1   yamt /* for swapping disklabel, so don't care about perf, just portability */
    626  1.22.2.1   yamt #define bs32(x) \
    627  1.22.2.1   yamt 	((((x) & 0xff000000) >> 24)| \
    628  1.22.2.1   yamt 	(((x) & 0x00ff0000) >>  8) | \
    629  1.22.2.1   yamt 	(((x) & 0x0000ff00) <<  8) | \
    630  1.22.2.1   yamt 	(((x) & 0x000000ff) << 24))
    631  1.22.2.1   yamt #define bs16(x) ((((x) & 0xff00) >> 8) | (((x) & 0x00ff) << 8))
    632  1.22.2.1   yamt 
    633  1.22.2.1   yamt /*
    634  1.22.2.1   yamt  * From:
    635  1.22.2.1   yamt  *	$NetBSD: disklabel_dkcksum.c,v 1.4 2005/05/15 21:01:34 thorpej Exp
    636  1.22.2.1   yamt  */
    637  1.22.2.1   yamt 
    638  1.22.2.1   yamt /*-
    639  1.22.2.1   yamt  * Copyright (c) 1991, 1993
    640  1.22.2.1   yamt  *	The Regents of the University of California.  All rights reserved.
    641  1.22.2.1   yamt  *
    642  1.22.2.1   yamt  * Redistribution and use in source and binary forms, with or without
    643  1.22.2.1   yamt  * modification, are permitted provided that the following conditions
    644  1.22.2.1   yamt  * are met:
    645  1.22.2.1   yamt  * 1. Redistributions of source code must retain the above copyright
    646  1.22.2.1   yamt  *    notice, this list of conditions and the following disclaimer.
    647  1.22.2.1   yamt  * 2. Redistributions in binary form must reproduce the above copyright
    648  1.22.2.1   yamt  *    notice, this list of conditions and the following disclaimer in the
    649  1.22.2.1   yamt  *    documentation and/or other materials provided with the distribution.
    650  1.22.2.1   yamt  * 3. Neither the name of the University nor the names of its contributors
    651  1.22.2.1   yamt  *    may be used to endorse or promote products derived from this software
    652  1.22.2.1   yamt  *    without specific prior written permission.
    653  1.22.2.1   yamt  *
    654  1.22.2.1   yamt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    655  1.22.2.1   yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    656  1.22.2.1   yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    657  1.22.2.1   yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    658  1.22.2.1   yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    659  1.22.2.1   yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    660  1.22.2.1   yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    661  1.22.2.1   yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    662  1.22.2.1   yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    663  1.22.2.1   yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    664  1.22.2.1   yamt  * SUCH DAMAGE.
    665  1.22.2.1   yamt  */
    666  1.22.2.1   yamt 
    667  1.22.2.1   yamt static uint16_t
    668  1.22.2.1   yamt rs_dl_dkcksum(struct rumpserver_disklabel *lp, int imswapped)
    669  1.22.2.1   yamt {
    670  1.22.2.1   yamt 	uint16_t *start, *end;
    671  1.22.2.1   yamt 	uint16_t sum;
    672  1.22.2.1   yamt 	uint16_t npart;
    673  1.22.2.1   yamt 
    674  1.22.2.1   yamt 	if (imswapped)
    675  1.22.2.1   yamt 		npart = bs16(lp->d_npartitions);
    676  1.22.2.1   yamt 	else
    677  1.22.2.1   yamt 		npart = lp->d_npartitions;
    678  1.22.2.1   yamt 
    679  1.22.2.1   yamt 	sum = 0;
    680  1.22.2.1   yamt 	start = (uint16_t *)(void *)lp;
    681  1.22.2.1   yamt 	end = (uint16_t *)(void *)&lp->d_partitions[npart];
    682  1.22.2.1   yamt 	while (start < end) {
    683  1.22.2.1   yamt 		if (imswapped)
    684  1.22.2.1   yamt 			sum ^= bs16(*start);
    685  1.22.2.1   yamt 		else
    686  1.22.2.1   yamt 			sum ^= *start;
    687  1.22.2.1   yamt 		start++;
    688  1.22.2.1   yamt 	}
    689  1.22.2.1   yamt 	return (sum);
    690  1.22.2.1   yamt }
    691  1.22.2.1   yamt 
    692  1.22.2.1   yamt /*
    693  1.22.2.1   yamt  * From:
    694  1.22.2.1   yamt  * NetBSD: disklabel_scan.c,v 1.3 2009/01/18 12:13:03 lukem Exp
    695  1.22.2.1   yamt  */
    696  1.22.2.1   yamt 
    697  1.22.2.1   yamt /*-
    698  1.22.2.1   yamt  * Copyright (c) 2002 The NetBSD Foundation, Inc.
    699  1.22.2.1   yamt  * All rights reserved.
    700  1.22.2.1   yamt  *
    701  1.22.2.1   yamt  * This code is derived from software contributed to The NetBSD Foundation
    702  1.22.2.1   yamt  * by Roland C. Dowdeswell.
    703  1.22.2.1   yamt  *
    704  1.22.2.1   yamt  * Redistribution and use in source and binary forms, with or without
    705  1.22.2.1   yamt  * modification, are permitted provided that the following conditions
    706  1.22.2.1   yamt  * are met:
    707  1.22.2.1   yamt  * 1. Redistributions of source code must retain the above copyright
    708  1.22.2.1   yamt  *    notice, this list of conditions and the following disclaimer.
    709  1.22.2.1   yamt  * 2. Redistributions in binary form must reproduce the above copyright
    710  1.22.2.1   yamt  *    notice, this list of conditions and the following disclaimer in the
    711  1.22.2.1   yamt  *    documentation and/or other materials provided with the distribution.
    712  1.22.2.1   yamt  *
    713  1.22.2.1   yamt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
    714  1.22.2.1   yamt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
    715  1.22.2.1   yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    716  1.22.2.1   yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
    717  1.22.2.1   yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    718  1.22.2.1   yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    719  1.22.2.1   yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    720  1.22.2.1   yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    721  1.22.2.1   yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    722  1.22.2.1   yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    723  1.22.2.1   yamt  * POSSIBILITY OF SUCH DAMAGE.
    724  1.22.2.1   yamt  */
    725  1.22.2.1   yamt 
    726  1.22.2.1   yamt static int
    727  1.22.2.1   yamt rs_dl_scan(struct rumpserver_disklabel *lp, int *isswapped,
    728  1.22.2.1   yamt 	char *buf, size_t buflen)
    729  1.22.2.1   yamt {
    730  1.22.2.1   yamt 	size_t i;
    731  1.22.2.1   yamt 	int imswapped;
    732  1.22.2.1   yamt 	uint16_t npart;
    733  1.22.2.1   yamt 
    734  1.22.2.1   yamt 	/* scan for the correct magic numbers. */
    735  1.22.2.1   yamt 
    736  1.22.2.1   yamt 	for (i=0; i <= buflen - sizeof(*lp); i += 4) {
    737  1.22.2.1   yamt 		memcpy(lp, buf + i, sizeof(*lp));
    738  1.22.2.1   yamt 		if (lp->d_magic == RUMPSERVER_DISKMAGIC &&
    739  1.22.2.1   yamt 		    lp->d_magic2 == RUMPSERVER_DISKMAGIC) {
    740  1.22.2.1   yamt 			imswapped = 0;
    741  1.22.2.1   yamt 			goto sanity;
    742  1.22.2.1   yamt 		}
    743  1.22.2.1   yamt 		if (lp->d_magic == bs32(RUMPSERVER_DISKMAGIC) &&
    744  1.22.2.1   yamt 		    lp->d_magic2 == bs32(RUMPSERVER_DISKMAGIC)) {
    745  1.22.2.1   yamt 			imswapped = 1;
    746  1.22.2.1   yamt 			goto sanity;
    747  1.22.2.1   yamt 		}
    748  1.22.2.1   yamt 	}
    749  1.22.2.1   yamt 
    750  1.22.2.1   yamt 	return 1;
    751  1.22.2.1   yamt 
    752  1.22.2.1   yamt sanity:
    753  1.22.2.1   yamt 	if (imswapped)
    754  1.22.2.1   yamt 		npart = bs16(lp->d_npartitions);
    755  1.22.2.1   yamt 	else
    756  1.22.2.1   yamt 		npart = lp->d_npartitions;
    757  1.22.2.1   yamt 	/* we've found something, let's sanity check it */
    758  1.22.2.1   yamt 	if (npart > RUMPSERVER_MAXPARTITIONS
    759  1.22.2.1   yamt 	    || rs_dl_dkcksum(lp, imswapped))
    760  1.22.2.1   yamt 		return 1;
    761  1.22.2.1   yamt 
    762  1.22.2.1   yamt 	*isswapped = imswapped;
    763  1.22.2.1   yamt 	return 0;
    764  1.22.2.1   yamt }
    765  1.22.2.1   yamt 
    766  1.22.2.1   yamt static void
    767  1.22.2.1   yamt processlabel(int sflag, int fd, int partition, off_t *foffp, off_t *flenp)
    768  1.22.2.1   yamt {
    769  1.22.2.1   yamt 	struct rumpserver_disklabel dl;
    770  1.22.2.1   yamt 	char buf[1<<16];
    771  1.22.2.1   yamt 	uint32_t foffset, flen;
    772  1.22.2.1   yamt 	int imswapped;
    773  1.22.2.1   yamt 
    774  1.22.2.1   yamt 	if (pread(fd, buf, sizeof(buf), 0) == -1)
    775  1.22.2.1   yamt 		die(sflag, errno, "could not read disk device");
    776  1.22.2.1   yamt 	if (rs_dl_scan(&dl, &imswapped, buf, sizeof(buf)))
    777  1.22.2.1   yamt 		die(sflag, ENOENT, "disklabel not found");
    778  1.22.2.1   yamt 
    779  1.22.2.1   yamt 	if (partition >= dl.d_npartitions)
    780  1.22.2.1   yamt 		die(sflag, ENOENT, "partition not available");
    781  1.22.2.1   yamt 
    782  1.22.2.1   yamt 	foffset = dl.d_partitions[partition].p_offset << RUMPSERVER_DEVSHIFT;
    783  1.22.2.1   yamt 	flen = dl.d_partitions[partition].p_size << RUMPSERVER_DEVSHIFT;
    784  1.22.2.1   yamt 	if (imswapped) {
    785  1.22.2.1   yamt 		foffset = bs32(foffset);
    786  1.22.2.1   yamt 		flen = bs32(flen);
    787  1.22.2.1   yamt 	}
    788  1.22.2.1   yamt 
    789  1.22.2.1   yamt 	*foffp = (off_t)foffset;
    790  1.22.2.1   yamt 	*flenp = (off_t)flen;
    791  1.22.2.1   yamt }
    792