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