newfs_sysvbfs.c revision 1.1 1 /* $NetBSD: newfs_sysvbfs.c,v 1.1 2005/12/29 14:53:45 tsutsui Exp $ */
2
3 /*-
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <fcntl.h>
46 #include <time.h>
47 #include <assert.h>
48 #include <sys/errno.h>
49 #include <sys/ioctl.h>
50 #include <sys/disklabel.h>
51
52 #include <fs/sysvbfs/bfs.h>
53
54 static void usage(void);
55 static int bfs_newfs(int, uint32_t);
56
57 int
58 main(int argc, char **argv)
59 {
60 const char *device;
61 struct disklabel d;
62 struct partition *p;
63 struct stat st;
64 int part;
65 int fd;
66
67 if (argc != 2)
68 usage();
69 device = argv[1];
70
71 if ((fd = open(device, O_RDWR)) == -1) {
72 perror("open device");
73 exit(EXIT_FAILURE);
74 }
75 if (fstat(fd, &st) != 0) {
76 perror("device stat");
77 goto err_exit;
78 }
79 if (!S_ISCHR(st.st_mode)) {
80 fprintf(stderr, "WARNING: not a raw device.\n");
81 }
82
83 part = DISKPART(st.st_rdev);
84
85 if (ioctl(fd, DIOCGDINFO, &d) == -1) {
86 perror("disklabel");
87 goto err_exit;
88 }
89 p = &d.d_partitions[part];
90 printf("partition = %d\n", part);
91 printf("size=%d offset=%d fstype=%d secsize=%d\n",
92 p->p_size, p->p_offset, p->p_fstype, d.d_secsize);
93
94 if (p->p_fstype != FS_SYSVBFS) {
95 fprintf(stderr, "not a SysVBFS partition.\n");
96 goto err_exit;
97 }
98
99 if (bfs_newfs(fd, p->p_size) != 0)
100 goto err_exit;
101
102 close(fd);
103
104 return 0;
105 err_exit:
106 close(fd);
107 exit(EXIT_FAILURE);
108 }
109
110 int
111 bfs_newfs(int fd, uint32_t nsectors)
112 {
113 uint8_t buf[DEV_BSIZE];
114 struct bfs_super_block *bfs = (void *)buf;
115 struct bfs_inode *inode = (void *)buf;
116 struct bfs_dirent *dirent = (void *)buf;
117 time_t t = time(0);
118 int err;
119
120 /* Super block */
121 memset(buf, 0, DEV_BSIZE);
122 bfs->header.magic = BFS_MAGIC;
123 bfs->header.data_start_byte = DEV_BSIZE * 2; /* super block + inode */
124 bfs->header.data_end_byte = nsectors * BFS_BSIZE - 1;
125 bfs->compaction.from = 0xffffffff;
126 bfs->compaction.to = 0xffffffff;
127 bfs->compaction.from_backup = 0xffffffff;
128 bfs->compaction.to_backup = 0xffffffff;
129
130 if ((err = lseek(fd, 0, SEEK_SET)) == -1) {
131 perror("seek super block");
132 return -1;
133 }
134 if (write(fd, buf, BFS_BSIZE) < 0) {
135 perror("write super block");
136 return -1;
137 }
138
139 /* i-node table */
140 memset(buf, 0, BFS_BSIZE);
141 inode->number = BFS_ROOT_INODE;
142 inode->start_sector = 2;
143 inode->end_sector = 2;
144 inode->eof_offset_byte = sizeof(struct bfs_dirent) +
145 inode->start_sector * BFS_BSIZE;
146 inode->attr.atime = t;
147 inode->attr.mtime = t;
148 inode->attr.ctime = t;
149 inode->attr.mode = 0755;
150 inode->attr.type = 2; /* DIR */
151 inode->attr.nlink = 2; /* . + .. */
152 if (write(fd, buf, BFS_BSIZE) < 0) {
153 perror("write i-node");
154 return -1;
155 }
156
157 /* dirent table */
158 memset(buf, 0, BFS_BSIZE);
159 dirent->inode = BFS_ROOT_INODE;
160 sprintf(dirent->name, ".");
161 dirent++;
162 dirent->inode = BFS_ROOT_INODE;
163 sprintf(dirent->name, "..");
164 if (write(fd, buf, BFS_BSIZE) < 0) {
165 perror("write dirent");
166 return -1;
167 }
168
169 return 0;
170 }
171
172 void
173 usage(void)
174 {
175
176 (void)fprintf(stderr, "usage: %s special-device\n", getprogname());
177 exit(EXIT_FAILURE);
178 }
179