-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.c
320 lines (271 loc) · 6.9 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
* Installer for Schillix
* (c) Copyright 2013 - Andrew Stormont <andyjstormont@gmail.com>
*/
#include <stdio.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
#include <dirent.h>
#include <libzfs.h>
#include "config.h"
#include "disk.h"
#include "copy.h"
char program_name[] = "schillix-install";
char temp_mount[PATH_MAX] = DEFAULT_MNT_POINT;
char cdrom_path[PATH_MAX] = DEFAULT_CDROM_PATH;
/*
* Print usage and exit
*/
void
usage (int retval)
{
FILE *out;
/*
* Print to stderr only on error
*/
if (retval == 0)
out = stdout;
else
out = stderr;
/*
* Add some space between error messages
*/
if (retval != 0)
fprintf (out, "\n");
fprintf (out, "Installer for Schillix\n");
fprintf (out, "(c) Copyright 2013 - Andrew Stormont\n");
fprintf (out, "\n");
fprintf (out, "usage: schillix-install [opts] /path/to/disk or devname\n");
fprintf (out, "\n");
fprintf (out, "Where opts is:\n");
fprintf (out, "\t-r name or new rpool (default is " DEFAULT_RPOOL_NAME ")\n");
fprintf (out, "\t-m temporary mountpoint (default is " DEFAULT_MNT_POINT ")\n");
fprintf (out, "\t-c path to livecd contents (default is " DEFAULT_CDROM_PATH ")\n");
fprintf (out, "\t-u don't unmount or export rpool after install\n");
fprintf (out, "\t-? print this message and exit\n");
exit (retval);
}
int
main (int argc, char **argv)
{
char c, disk[PATH_MAX] = { '\0' }, rpool[ZPOOL_MAXNAMELEN] = DEFAULT_RPOOL_NAME;
int i;
DIR *dir;
libzfs_handle_t *libzfs_handle;
boolean_t unmount = B_TRUE;
/*
* Parse command line arguments
*/
while ((c = getopt (argc, argv, "r:m:c:u?")) != -1)
{
switch (c)
{
case 'r':
/*
* Set alternative rpool name
*/
if (strlen (optarg) >= ZPOOL_MAXNAMELEN)
{
fprintf (stderr, "Error: rpool name too long\n");
usage (EXIT_FAILURE);
}
strcpy (rpool, optarg);
break;
case 'm':
/*
* Set alternative temp mountpoint
*/
if (strlen (optarg) >= PATH_MAX)
{
fprintf (stderr, "Error: mountpoint path too long\n");
usage (EXIT_FAILURE);
}
strcpy (temp_mount, optarg);
break;
case 'c':
/*
* Set alternative path to livecd contents
*/
if (strlen (optarg) >= PATH_MAX)
{
fprintf (stderr, "Error: livecd path too long\n");
usage (EXIT_FAILURE);
}
strcpy (cdrom_path, optarg);
break;
case 'u':
/*
* Don't unmount or export zpool with done
*/
unmount = B_FALSE;
break;
case '?':
/*
* We get here if an argument is missing or
* the user gave -?
*/
if (optopt == '?')
usage (EXIT_SUCCESS);
else
usage (EXIT_FAILURE);
break;
default:
/*
* Ignoring opts is bad!
*/
abort();
}
}
/*
* Fix any given disk paths
* TODO: Support for creating mirrored rpools!
*/
#define DISK_PATH "/dev/dsk"
#define RDISK_PATH "/dev/rdsk"
#define DISK_LEN 8
#define RDISK_LEN 9
for (i = optind; i < argc; i++)
{
if (disk[0] == '\0')
{
/*
* Disk path is too long
*/
if (strlen (disk) >= PATH_MAX)
{
fprintf (stderr, "Error: disk path is too long\n");
usage (EXIT_FAILURE);
}
/*
* Path is correct already
*/
else if (strncmp (RDISK_PATH, argv[i], RDISK_LEN) == 0)
strcpy (disk, argv[i]);
/*
* Replace /dev/dsk with /dev/rdsk
*/
else if (strncmp (DISK_PATH, argv[i], DISK_LEN) == 0)
sprintf (disk, RDISK_PATH "/%s", argv[i] + DISK_LEN + 1);
/*
* Need to append /dev/rdsk
*/
else
sprintf (disk, RDISK_PATH "/%s", argv[i]);
}
else
{
fprintf (stderr, "Error: Please specify only one disk\n");
usage (EXIT_FAILURE);
}
}
if (disk[0] == '\0')
{
fprintf (stderr, "Error: No disk specified\n");
usage (EXIT_FAILURE);
}
/*
* Ensure that the path to the livecd contents is a directory
* and that it can be opened.
*/
if ((dir = opendir (cdrom_path)) == NULL)
{
fprintf (stderr, "Error: unable to open %s: %s\n", cdrom_path, strerror (errno));
usage (EXIT_FAILURE);
}
(void) closedir (dir);
/*
* Get libzfs handle before outputting anything to stdout/stderr
* otherwise we won't be able to get it later (seriously)
*/
if ((libzfs_handle = libzfs_init ()) == NULL)
{
fprintf (stderr, "Error: Unable to get libzfs handle\n");
return EXIT_FAILURE;
}
/*
* Warn the user before touching the disk
*/
printf ("All data on %s will be destroyed. Continue? [yn] ", disk);
while (scanf ("%c", &c) == 0 || (c != 'y' && c != 'n'))
printf ("\rContinue? [yn] ");
if (c == 'n')
{
fprintf (stderr, "User aborted format\n");
return EXIT_FAILURE;
}
if (disk_in_use (libzfs_handle, disk) == B_TRUE)
return EXIT_FAILURE;
/*
* Reformat disk
*/
puts ("Reformatting disk...");
if (create_root_partition (disk) == B_FALSE)
return EXIT_FAILURE;
if (create_root_vtoc (disk) == B_FALSE)
return EXIT_FAILURE;
/*
* Create new ZFS filesystem
*/
puts ("Creating new filesystem...");
if (create_root_pool (libzfs_handle, disk, rpool, temp_mount) == B_FALSE)
return EXIT_FAILURE;
if (create_root_datasets (libzfs_handle, rpool) == B_FALSE)
return EXIT_FAILURE;
if (set_root_bootfs (libzfs_handle, rpool) == B_FALSE)
return EXIT_FAILURE;
/*
* Mount new filesystem and copy files
*/
puts ("Mounting filesystem...");
if (mount_root_datasets (libzfs_handle, rpool) == B_FALSE)
return EXIT_FAILURE;
printf ("Copying files...\n");
if (copy_files () == B_FALSE)
return EXIT_FAILURE;
if (copy_grub (temp_mount, rpool) == B_FALSE)
return EXIT_FAILURE;
/*
* Install grub to mbr, create boot archive, etc
*/
puts ("Finishing up...");
if (config_grub (temp_mount, disk) == B_FALSE)
return EXIT_FAILURE;
if (config_devfs (temp_mount) == B_FALSE)
return EXIT_FAILURE;
if (config_bootadm (temp_mount) == B_FALSE)
return EXIT_FAILURE;
/*
* Unmount and export new rpool
*/
if (unmount == B_TRUE)
{
puts ("Unmounting filesystem...");
if (unmount_root_datasets (libzfs_handle, rpool) == B_FALSE)
return EXIT_FAILURE;
if (export_root_pool (libzfs_handle, rpool) == B_FALSE)
return EXIT_FAILURE;
}
(void) libzfs_fini (libzfs_handle);
puts ("Done.");
return EXIT_SUCCESS;
}