1 /* $NetBSD: newfs_v7fs.c,v 1.5 2017/01/10 20:53:09 christos Exp $ */
4 * Copyright (c) 2004, 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: newfs_v7fs.c,v 1.5 2017/01/10 20:53:09 christos Exp $");
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/disklabel.h>
40 #include <sys/ioctl.h>
50 #include <fs/v7fs/v7fs.h>
51 #include "v7fs_impl.h"
53 #include "newfs_v7fs.h"
55 static void usage(void) __dead
;
56 static bool progress_bar_enable
= false;
57 int v7fs_newfs_verbose
= 3; /* newfs compatible */
60 main(int argc
, char **argv
)
70 int endian
= _BYTE_ORDER
;
76 Fflag
= Zflag
= partsize
= 0;
77 while ((ch
= getopt(argc
, argv
, "Fs:Zs:n:B:V:")) != -1) {
80 v7fs_newfs_verbose
= atoi(optarg
);
86 partsize
= atoi(optarg
);
89 maxfile
= atoi(optarg
);
97 endian
= _LITTLE_ENDIAN
;
100 endian
= _BIG_ENDIAN
;
103 endian
= _PDP_ENDIAN
;
119 progress_bar_enable
= v7fs_newfs_verbose
> 1;
122 if (progress_bar_enable
) {
123 progress_switch(progress_bar_enable
);
125 progress(&(struct progress_arg
){ .cdev
= device
});
129 if ((fd
= open(device
, O_RDWR
)) == -1) {
130 err(EXIT_FAILURE
, "%s", device
);
132 if (fstat(fd
, &st
) != 0) {
135 if (!S_ISCHR(st
.st_mode
)) {
136 warnx("not a raw device");
139 part
= DISKPART(st
.st_rdev
);
141 if (ioctl(fd
, DIOCGDINFO
, &d
) == -1) {
144 p
= &d
.d_partitions
[part
];
145 if (v7fs_newfs_verbose
) {
146 printf("partition=%d size=%d offset=%d fstype=%d"
147 " secsize=%d\n", part
, p
->p_size
, p
->p_offset
,
148 p
->p_fstype
, d
.d_secsize
);
150 if (p
->p_fstype
!= FS_V7
) {
151 warnx("not a Version 7 partition");
154 partsize
= p
->p_size
;
157 uint8_t zbuf
[8192] = {0, };
160 errx(EXIT_FAILURE
, "-F requires -s");
163 filesize
= partsize
<< V7FS_BSHIFT
;
165 fd
= open(device
, O_RDWR
|O_CREAT
|O_TRUNC
, 0666);
167 err(EXIT_FAILURE
, "%s", device
);
171 while (filesize
> 0) {
172 size_t writenow
= MIN(filesize
,
173 (off_t
)sizeof(zbuf
));
175 if ((size_t)write(fd
, zbuf
, writenow
) !=
177 err(EXIT_FAILURE
, NULL
);
179 filesize
-= writenow
;
182 if (lseek(fd
, filesize
- 1, SEEK_SET
) == -1) {
185 if (write(fd
, zbuf
, 1) != 1) {
188 if (lseek(fd
, 0, SEEK_SET
) == -1) {
194 if (v7fs_newfs(&(struct v7fs_mount_device
)
195 { .device
.fd
= fd
, .endian
= endian
, .sectors
= partsize
},
204 err(EXIT_FAILURE
, NULL
);
208 progress(const struct progress_arg
*p
)
210 static struct progress_arg Progress
;
211 static char cdev
[32];
212 static char label
[32];
214 if (!progress_bar_enable
)
220 strcpy(cdev
, p
->cdev
);
222 strcpy(label
, p
->label
);
227 if (++Progress
.cnt
> Progress
.tick
) {
230 progress_bar(cdev
, label
, Progress
.total
, PROGRESS_BAR_GRANULE
);
238 (void)fprintf(stderr
, "usage: \n%s [-FZ] [-B byte-order]"
239 " [-n inodes] [-s sectors] [-V verbose] special\n", getprogname());