sbin/newfs_hammer2: Add newfs(8) -s option equivalent option
[dragonfly.git] / sbin / newfs_hammer2 / newfs_hammer2.c
1 /*
2  * Copyright (c) 2011-2015 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@dragonflybsd.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 #include <sys/types.h>
36 #include <sys/param.h>
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <stddef.h>
41 #include <unistd.h>
42 #include <string.h>
43 #include <err.h>
44
45 #include "mkfs_hammer2.h"
46
47 static void usage(void);
48
49 int
50 main(int ac, char **av)
51 {
52         hammer2_mkfs_options_t opt;
53         int ch;
54         int label_specified = 0;
55
56         /*
57          * Initialize option structure.
58          */
59         hammer2_mkfs_init(&opt);
60
61         /*
62          * Parse arguments.
63          */
64         while ((ch = getopt(ac, av, "L:b:r:V:s:d")) != -1) {
65                 switch(ch) {
66                 case 'b':
67                         opt.BootAreaSize = getsize(optarg,
68                                          HAMMER2_NEWFS_ALIGN,
69                                          HAMMER2_BOOT_MAX_BYTES, 2);
70                         break;
71                 case 'r':
72                         opt.AuxAreaSize = getsize(optarg,
73                                          HAMMER2_NEWFS_ALIGN,
74                                          HAMMER2_AUX_MAX_BYTES, 2);
75                         break;
76                 case 'V':
77                         opt.Hammer2Version = strtol(optarg, NULL, 0);
78                         if (opt.Hammer2Version < HAMMER2_VOL_VERSION_MIN ||
79                             opt.Hammer2Version >= HAMMER2_VOL_VERSION_WIP) {
80                                 errx(1, "I don't understand how to format "
81                                      "HAMMER2 version %d",
82                                      opt.Hammer2Version);
83                         }
84                         break;
85                 case 'L':
86                         label_specified = 1;
87                         if (strcasecmp(optarg, "none") == 0) {
88                                 break;
89                         }
90                         if (opt.NLabels >= MAXLABELS) {
91                                 errx(1, "Limit of %d local labels",
92                                      MAXLABELS - 1);
93                         }
94                         if (strlen(optarg) == 0) {
95                                 errx(1, "Volume label '%s' cannot be 0-length",
96                                         optarg);
97                         }
98                         if (strlen(optarg) >= HAMMER2_INODE_MAXNAME) {
99                                 errx(1, "Volume label '%s' is too long "
100                                         "(%d chars max)",
101                                         optarg,
102                                         HAMMER2_INODE_MAXNAME - 1);
103                         }
104                         opt.Label[opt.NLabels++] = strdup(optarg);
105                         break;
106                 case 's':
107                         /* XXX 0x7fffffffffffffff isn't limitation of HAMMER2 */
108                         opt.FileSystemSize = getsize(optarg,
109                                          HAMMER2_FREEMAP_LEVEL1_SIZE,
110                                          0x7fffffffffffffff, 2);
111                         break;
112                 case 'd':
113                         opt.DebugOpt = 1;
114                         break;
115                 default:
116                         usage();
117                         break;
118                 }
119         }
120
121         ac -= optind;
122         av += optind;
123
124         if (ac == 0)
125                 errx(1, "You must specify at least one disk device");
126         if (ac > HAMMER2_MAX_VOLUMES)
127                 errx(1, "The maximum number of volumes is %d",
128                      HAMMER2_MAX_VOLUMES);
129
130         /*
131          * Check default label type.
132          */
133         if (!label_specified) {
134                 char c = av[0][strlen(av[0]) - 1];
135                 if (c == 'a')
136                         opt.DefaultLabelType = HAMMER2_LABEL_BOOT;
137                 else if (c == 'd')
138                         opt.DefaultLabelType = HAMMER2_LABEL_ROOT;
139                 else
140                         opt.DefaultLabelType = HAMMER2_LABEL_DATA;
141         }
142
143         /*
144          * Create Hammer2 filesystem.
145          */
146         hammer2_mkfs(ac, av, &opt);
147
148         /*
149          * Cleanup option structure.
150          */
151         hammer2_mkfs_cleanup(&opt);
152
153         return(0);
154 }
155
156 static
157 void
158 usage(void)
159 {
160         fprintf(stderr,
161                 "usage: newfs_hammer2 [-b bootsize] [-r auxsize] "
162                 "[-V version] [-L label ...] [-s size] special ...\n"
163         );
164         exit(1);
165 }