]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sbin/geom/class/label/geom_label.c
MFC r323314, r323338, r328849
[FreeBSD/stable/10.git] / sbin / geom / class / label / geom_label.c
1 /*-
2  * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <errno.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <strings.h>
36 #include <assert.h>
37 #include <libgeom.h>
38 #include <geom/label/g_label.h>
39
40 #include "core/geom.h"
41 #include "misc/subr.h"
42
43 #ifdef STATIC_GEOM_CLASSES
44 #define PUBSYM(x)       glabel_##x
45 #else
46 #define PUBSYM(x)       x
47 #endif
48
49 uint32_t PUBSYM(lib_version) = G_LIB_VERSION;
50 uint32_t PUBSYM(version) = G_LABEL_VERSION;
51
52 static void label_main(struct gctl_req *req, unsigned flags);
53 static void label_clear(struct gctl_req *req);
54 static void label_dump(struct gctl_req *req);
55 static void label_label(struct gctl_req *req);
56
57 struct g_command PUBSYM(class_commands)[] = {
58         { "clear", G_FLAG_VERBOSE, label_main, G_NULL_OPTS,
59             "[-v] dev ..."
60         },
61         { "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL, G_NULL_OPTS,
62             "[-v] name dev"
63         },
64         { "destroy", G_FLAG_VERBOSE, NULL,
65             {
66                 { 'f', "force", NULL, G_TYPE_BOOL },
67                 G_OPT_SENTINEL
68             },
69             "[-fv] name ..."
70         },
71         { "dump", 0, label_main, G_NULL_OPTS,
72             "dev ..."
73         },
74         { "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, label_main, G_NULL_OPTS,
75             "[-v] name dev"
76         },
77         { "stop", G_FLAG_VERBOSE, NULL,
78             {
79                 { 'f', "force", NULL, G_TYPE_BOOL },
80                 G_OPT_SENTINEL
81             },
82             "[-fv] name ..."
83         },
84         G_CMD_SENTINEL
85 };
86
87 static int verbose = 0;
88
89 static void
90 label_main(struct gctl_req *req, unsigned flags)
91 {
92         const char *name;
93
94         if ((flags & G_FLAG_VERBOSE) != 0)
95                 verbose = 1;
96
97         name = gctl_get_ascii(req, "verb");
98         if (name == NULL) {
99                 gctl_error(req, "No '%s' argument.", "verb");
100                 return;
101         }
102         if (strcmp(name, "label") == 0)
103                 label_label(req);
104         else if (strcmp(name, "clear") == 0)
105                 label_clear(req);
106         else if (strcmp(name, "dump") == 0)
107                 label_dump(req);
108         else
109                 gctl_error(req, "Unknown command: %s.", name);
110 }
111
112 static void
113 label_label(struct gctl_req *req)
114 {
115         struct g_label_metadata md;
116         const char *name, *label;
117         u_char sector[512];
118         int error, nargs;
119
120         bzero(sector, sizeof(sector));
121         nargs = gctl_get_int(req, "nargs");
122         if (nargs != 2) {
123                 gctl_error(req, "Invalid number of arguments.");
124                 return;
125         }
126
127         /*
128          * Clear last sector first to spoil all components if device exists.
129          */
130         name = gctl_get_ascii(req, "arg1");
131         error = g_metadata_clear(name, NULL);
132         if (error != 0) {
133                 gctl_error(req, "Can't store metadata on %s: %s.", name,
134                     strerror(error));
135                 return;
136         }
137
138         strlcpy(md.md_magic, G_LABEL_MAGIC, sizeof(md.md_magic));
139         md.md_version = G_LABEL_VERSION;
140         label = gctl_get_ascii(req, "arg0");
141         bzero(md.md_label, sizeof(md.md_label));
142         strlcpy(md.md_label, label, sizeof(md.md_label));
143         md.md_provsize = g_get_mediasize(name);
144         if (md.md_provsize == 0) {
145                 gctl_error(req, "Can't get mediasize of %s: %s.", name,
146                     strerror(errno));
147                 return;
148         }
149
150         /*
151          * Ok, store metadata.
152          */
153         label_metadata_encode(&md, sector);
154         error = g_metadata_store(name, sector, sizeof(sector));
155         if (error != 0) {
156                 fprintf(stderr, "Can't store metadata on %s: %s.\n", name,
157                     strerror(error));
158                 gctl_error(req, "Not done.");
159         }
160         if (verbose)
161                 printf("Metadata value stored on %s.\n", name);
162 }
163
164 static void
165 label_clear(struct gctl_req *req)
166 {
167         const char *name;
168         int error, i, nargs;
169
170         nargs = gctl_get_int(req, "nargs");
171         if (nargs < 1) {
172                 gctl_error(req, "Too few arguments.");
173                 return;
174         }
175
176         for (i = 0; i < nargs; i++) {
177                 name = gctl_get_ascii(req, "arg%d", i);
178                 error = g_metadata_clear(name, G_LABEL_MAGIC);
179                 if (error != 0) {
180                         fprintf(stderr, "Can't clear metadata on %s: %s.\n",
181                             name, strerror(error));
182                         gctl_error(req, "Not fully done.");
183                         continue;
184                 }
185                 if (verbose)
186                         printf("Metadata cleared on %s.\n", name);
187         }
188 }
189
190 static void
191 label_metadata_dump(const struct g_label_metadata *md)
192 {
193
194         printf("    Magic string: %s\n", md->md_magic);
195         printf("Metadata version: %u\n", (u_int)md->md_version);
196         printf("           Label: %s\n", md->md_label);
197 }
198
199 static void
200 label_dump(struct gctl_req *req)
201 {
202         struct g_label_metadata md, tmpmd;
203         const char *name;
204         int error, i, nargs;
205
206         nargs = gctl_get_int(req, "nargs");
207         if (nargs < 1) {
208                 gctl_error(req, "Too few arguments.");
209                 return;
210         }
211
212         for (i = 0; i < nargs; i++) {
213                 name = gctl_get_ascii(req, "arg%d", i);
214                 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
215                     G_LABEL_MAGIC);
216                 if (error != 0) {
217                         fprintf(stderr, "Can't read metadata from %s: %s.\n",
218                             name, strerror(error));
219                         gctl_error(req, "Not fully done.");
220                         continue;
221                 }
222                 label_metadata_decode((u_char *)&tmpmd, &md);
223                 printf("Metadata on %s:\n", name);
224                 label_metadata_dump(&md);
225                 printf("\n");
226         }
227 }