]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sbin/geom/class/label/geom_label.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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
44 uint32_t lib_version = G_LIB_VERSION;
45 uint32_t version = G_LABEL_VERSION;
46
47 static void label_main(struct gctl_req *req, unsigned flags);
48 static void label_clear(struct gctl_req *req);
49 static void label_dump(struct gctl_req *req);
50 static void label_label(struct gctl_req *req);
51
52 struct g_command class_commands[] = {
53         { "clear", G_FLAG_VERBOSE, label_main, G_NULL_OPTS, NULL,
54             "[-v] dev ..."
55         },
56         { "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL, G_NULL_OPTS,
57             NULL, "[-v] name dev"
58         },
59         { "destroy", G_FLAG_VERBOSE, NULL,
60             {
61                 { 'f', "force", NULL, G_TYPE_BOOL },
62                 G_OPT_SENTINEL
63             },
64             NULL, "[-fv] name ..."
65         },
66         { "dump", 0, label_main, G_NULL_OPTS, NULL,
67             "dev ..."
68         },
69         { "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, label_main, G_NULL_OPTS,
70             NULL, "[-v] name dev"
71         },
72         { "stop", G_FLAG_VERBOSE, NULL,
73             {
74                 { 'f', "force", NULL, G_TYPE_BOOL },
75                 G_OPT_SENTINEL
76             },
77             NULL, "[-fv] name ..."
78         },
79         G_CMD_SENTINEL
80 };
81
82 static int verbose = 0;
83
84 static void
85 label_main(struct gctl_req *req, unsigned flags)
86 {
87         const char *name;
88
89         if ((flags & G_FLAG_VERBOSE) != 0)
90                 verbose = 1;
91
92         name = gctl_get_ascii(req, "verb");
93         if (name == NULL) {
94                 gctl_error(req, "No '%s' argument.", "verb");
95                 return;
96         }
97         if (strcmp(name, "label") == 0)
98                 label_label(req);
99         else if (strcmp(name, "clear") == 0)
100                 label_clear(req);
101         else if (strcmp(name, "dump") == 0)
102                 label_dump(req);
103         else
104                 gctl_error(req, "Unknown command: %s.", name);
105 }
106
107 static void
108 label_label(struct gctl_req *req)
109 {
110         struct g_label_metadata md;
111         const char *name, *label;
112         u_char sector[512];
113         int error, nargs;
114
115         nargs = gctl_get_int(req, "nargs");
116         if (nargs != 2) {
117                 gctl_error(req, "Invalid number of arguments.");
118                 return;
119         }
120
121         /*
122          * Clear last sector first to spoil all components if device exists.
123          */
124         name = gctl_get_ascii(req, "arg1");
125         error = g_metadata_clear(name, NULL);
126         if (error != 0) {
127                 gctl_error(req, "Can't store metadata on %s: %s.", name,
128                     strerror(error));
129                 return;
130         }
131
132         strlcpy(md.md_magic, G_LABEL_MAGIC, sizeof(md.md_magic));
133         md.md_version = G_LABEL_VERSION;
134         label = gctl_get_ascii(req, "arg0");
135         strlcpy(md.md_label, label, sizeof(md.md_label));
136         md.md_provsize = g_get_mediasize(name);
137         if (md.md_provsize == 0) {
138                 gctl_error(req, "Can't get mediasize of %s: %s.", name,
139                     strerror(errno));
140                 return;
141         }
142
143         /*
144          * Ok, store metadata.
145          */
146         label_metadata_encode(&md, sector);
147         error = g_metadata_store(name, sector, sizeof(sector));
148         if (error != 0) {
149                 fprintf(stderr, "Can't store metadata on %s: %s.\n", name,
150                     strerror(error));
151                 gctl_error(req, "Not done.");
152         }
153         if (verbose)
154                 printf("Metadata value stored on %s.\n", name);
155 }
156
157 static void
158 label_clear(struct gctl_req *req)
159 {
160         const char *name;
161         int error, i, nargs;
162
163         nargs = gctl_get_int(req, "nargs");
164         if (nargs < 1) {
165                 gctl_error(req, "Too few arguments.");
166                 return;
167         }
168
169         for (i = 0; i < nargs; i++) {
170                 name = gctl_get_ascii(req, "arg%d", i);
171                 error = g_metadata_clear(name, G_LABEL_MAGIC);
172                 if (error != 0) {
173                         fprintf(stderr, "Can't clear metadata on %s: %s.\n",
174                             name, strerror(error));
175                         gctl_error(req, "Not fully done.");
176                         continue;
177                 }
178                 if (verbose)
179                         printf("Metadata cleared on %s.\n", name);
180         }
181 }
182
183 static void
184 label_metadata_dump(const struct g_label_metadata *md)
185 {
186
187         printf("    Magic string: %s\n", md->md_magic);
188         printf("Metadata version: %u\n", (u_int)md->md_version);
189         printf("           Label: %s\n", md->md_label);
190 }
191
192 static void
193 label_dump(struct gctl_req *req)
194 {
195         struct g_label_metadata md, tmpmd;
196         const char *name;
197         int error, i, nargs;
198
199         nargs = gctl_get_int(req, "nargs");
200         if (nargs < 1) {
201                 gctl_error(req, "Too few arguments.");
202                 return;
203         }
204
205         for (i = 0; i < nargs; i++) {
206                 name = gctl_get_ascii(req, "arg%d", i);
207                 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
208                     G_LABEL_MAGIC);
209                 if (error != 0) {
210                         fprintf(stderr, "Can't read metadata from %s: %s.\n",
211                             name, strerror(error));
212                         gctl_error(req, "Not fully done.");
213                         continue;
214                 }
215                 label_metadata_decode((u_char *)&tmpmd, &md);
216                 printf("Metadata on %s:\n", name);
217                 label_metadata_dump(&md);
218                 printf("\n");
219         }
220 }