mdconfig.c revision 70448
1/*
2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
8 *
9 * $FreeBSD: head/sbin/mdconfig/mdconfig.c 70448 2000-12-28 20:57:57Z phk $
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <fcntl.h>
16#include <unistd.h>
17#include <string.h>
18#include <err.h>
19#include <sys/ioctl.h>
20#include <sys/param.h>
21#include <sys/mdioctl.h>
22
23struct md_ioctl mdio;
24
25enum {UNSET, ATTACH, DETACH} action = UNSET;
26
27int
28main(int argc, char **argv)
29{
30	int ch, fd, i;
31
32	mdio.md_options = MD_CLUSTER | MD_AUTOUNIT;
33
34	for (;;) {
35		ch = getopt(argc, argv, "adf:o:s:t:u:");
36		if (ch == -1)
37			break;
38		switch (ch) {
39		case 'a':
40			action = ATTACH;
41			break;
42		case 'd':
43			action = DETACH;
44			break;
45		case 'f':
46			strncpy(mdio.md_file, optarg, sizeof(mdio.md_file) - 1);
47			break;
48		case 'o':
49			if (!strcmp(optarg, "cluster"))
50				mdio.md_options |= MD_CLUSTER;
51			else if (!strcmp(optarg, "nocluster"))
52				mdio.md_options &= ~MD_CLUSTER;
53			else if (!strcmp(optarg, "reserve"))
54				mdio.md_options |= MD_RESERVE;
55			else if (!strcmp(optarg, "noreserve"))
56				mdio.md_options &= ~MD_RESERVE;
57			else if (!strcmp(optarg, "autounit"))
58				mdio.md_options |= MD_AUTOUNIT;
59			else if (!strcmp(optarg, "noautounit"))
60				mdio.md_options &= ~MD_AUTOUNIT;
61			else
62				errx(1, "Unknown option.");
63			break;
64		case 's':
65			mdio.md_size = strtoul(optarg, NULL, 0);
66			break;
67		case 't':
68			if (!strcmp(optarg, "malloc"))
69				mdio.md_type = MD_MALLOC;
70			else if (!strcmp(optarg, "preload"))
71				mdio.md_type = MD_PRELOAD;
72			else if (!strcmp(optarg, "vnode"))
73				mdio.md_type = MD_VNODE;
74			else if (!strcmp(optarg, "swap"))
75				mdio.md_type = MD_SWAP;
76			else
77				errx(1, "Unknown type.");
78			break;
79		case 'u':
80			mdio.md_unit = strtoul(optarg, NULL, 0);
81			mdio.md_options &= ~MD_AUTOUNIT;
82			break;
83		default:
84			errx(1, "Usage: %s [-ad] [-f file] [-o option] [-s size] [-t type ] [-u unit].", argv[0]);
85		}
86	}
87
88	fd = open("/dev/mdctl", O_RDWR, 0);
89	if (fd < 0)
90		err(1, "/dev/mdctl");
91	if (action == ATTACH)
92		i = ioctl(fd, MDIOCATTACH, &mdio);
93	else if (action == DETACH)
94		i = ioctl(fd, MDIOCDETACH, &mdio);
95	else
96		errx(1, "Neither -a(ttach) nor -d(etach) options present.");
97	if (i < 0)
98		err(1, "ioctl(/dev/mdctl)");
99	return (0);
100}
101
102