dkscan_bsdlabel.c revision 1.4
11.4Schs/* $NetBSD: dkscan_bsdlabel.c,v 1.4 2017/06/08 22:24:29 chs Exp $ */
21.1Smartin
31.1Smartin/*-
41.1Smartin * Copyright (c) 2007 The NetBSD Foundation, Inc.
51.1Smartin * All rights reserved.
61.1Smartin *
71.1Smartin * This code is derived from software contributed to The NetBSD Foundation
81.1Smartin * by Martin Husemann <martin@NetBSD.org>.
91.1Smartin *
101.1Smartin * Redistribution and use in source and binary forms, with or without
111.1Smartin * modification, are permitted provided that the following conditions
121.1Smartin * are met:
131.1Smartin * 1. Redistributions of source code must retain the above copyright
141.1Smartin *    notice, this list of conditions and the following disclaimer.
151.1Smartin * 2. Redistributions in binary form must reproduce the above copyright
161.1Smartin *    notice, this list of conditions and the following disclaimer in the
171.1Smartin *    documentation and/or other materials provided with the distribution.
181.1Smartin *
191.1Smartin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Smartin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Smartin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Smartin * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Smartin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Smartin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Smartin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Smartin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Smartin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Smartin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Smartin * POSSIBILITY OF SUCH DAMAGE.
301.1Smartin */
311.1Smartin
321.1Smartin#include <stdio.h>
331.1Smartin#include <stdlib.h>
341.1Smartin#include <stddef.h>
351.1Smartin#include <string.h>
361.1Smartin#include <unistd.h>
371.1Smartin#include <fcntl.h>
381.1Smartin#include <err.h>
391.1Smartin#include <util.h>
401.1Smartin#include <sys/disk.h>
411.1Smartin
421.1Smartin#include "dkscan_util.h"
431.1Smartin
441.4Schsstruct disk {
451.4Schs	const char	*dk_name;	/* disk name */
461.4Schs	int		dk_blkshift;	/* shift to convert DEV_BSIZE to blks */
471.4Schs};
481.4Schs
491.1Smartin#include "dkwedge_bsdlabel.c"
501.1Smartin
511.3Sjoerg__dead static void usage(void);
521.3Sjoerg
531.1Smartinint
541.1Smartinmain(int argc, char **argv)
551.1Smartin{
561.1Smartin	struct disk d;
571.1Smartin	int ch;
581.1Smartin	char buf[PATH_MAX];
591.1Smartin	const char *devpart;
601.1Smartin
611.1Smartin	if (argc < 2)
621.1Smartin		usage();
631.1Smartin
641.1Smartin	while ((ch = getopt(argc, argv, "nv")) != -1) {
651.1Smartin		switch (ch) {
661.1Smartin		case 'n':
671.1Smartin			no_action = 1;
681.1Smartin			break;
691.1Smartin		case 'v':
701.1Smartin			verbose++;
711.1Smartin			break;
721.1Smartin		default:
731.1Smartin			usage();
741.1Smartin		}
751.1Smartin	}
761.1Smartin	if (optind >= argc)
771.1Smartin		usage();
781.1Smartin
791.1Smartin	disk_fd = opendisk(argv[optind], O_RDWR, buf, PATH_MAX, 0);
801.1Smartin	if (disk_fd == -1)
811.1Smartin		err(1, "%s", argv[optind]);
821.1Smartin
831.1Smartin	devpart = strrchr(argv[optind], '/');
841.1Smartin	if (devpart == NULL)
851.1Smartin		devpart = argv[optind];
861.1Smartin
871.1Smartin	memset(&d, 0, sizeof(d));
881.4Schs	d.dk_name  = devpart;
891.1Smartin	dkwedge_discover_bsdlabel(&d, NULL);
901.1Smartin
911.1Smartin	close(disk_fd);
921.1Smartin	return 0;
931.1Smartin}
941.1Smartin
951.1Smartinvoid
961.1Smartinusage(void)
971.1Smartin{
981.1Smartin	fprintf(stderr, "usage: %s [-vn] <diskname>\n"
991.1Smartin	    "  where\n"
1001.1Smartin	    "    -n don't change anything, just print info\n"
1011.1Smartin	    "    -v be more verbose\n"
1021.1Smartin	    "    <diskname> device to scan\n",
1031.1Smartin	    getprogname());
1041.1Smartin	exit(1);
1051.1Smartin}
106