forked from andy-js/schillix-installer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
copy.c
527 lines (464 loc) · 13.7 KB
/
copy.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
* 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 <[email protected]>
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <fcntl.h>
#include <ftw.h>
#include <sys/sendfile.h>
extern char temp_mount[PATH_MAX];
extern char cdrom_path[PATH_MAX];
/*
* Copy a file to a new destination
*/
static boolean_t
copy_file (const char *path, const char *dest, const struct stat *statptr)
{
int in_fd, out_fd;
struct stat in_stat;
off_t offset = 0;
/*
* Stat the file if the caller hasn't
*/
if (statptr == NULL)
{
if (stat (path, &in_stat) == -1)
{
fprintf (stderr, "Unable to stat file %s: %s\n", path, strerror (errno));
return B_FALSE;
}
}
else
in_stat = *statptr;
/*
* Open the files
*/
if ((in_fd = open (path, O_RDONLY)) == -1)
{
fprintf (stderr, "Unable to open file %s: %s\n", path, strerror (errno));
return B_FALSE;
}
if ((out_fd = creat (dest, in_stat.st_mode)) == -1)
{
if (errno == EEXIST)
{
if (unlink (dest) == -1)
{
fprintf (stderr, "Unable to remove file %s: %s\n", dest, strerror (errno));
(void) close (in_fd);
return B_FALSE;
}
if ((out_fd = creat (dest, in_stat.st_mode)) == -1)
{
fprintf (stderr, "Unable to recreate file %s: %s\n", dest, strerror (errno));
(void) close (in_fd);
return B_FALSE;
}
}
else
{
fprintf (stderr, "Unable to create file %s: %s\n", dest, strerror (errno));
(void) close (in_fd);
return B_FALSE;
}
}
/*
* Copy ownership
*/
if (chown (dest, in_stat.st_uid, in_stat.st_gid) == -1)
{
fprintf (stderr, "Unable to chown file %s: %s\n", dest, strerror (errno));
(void) close (in_fd);
(void) close (out_fd);
return B_FALSE;
}
/*
* Copy contents over
*/
if (sendfile (out_fd, in_fd, &offset, in_stat.st_size) == -1)
{
fprintf (stderr, "Unable to copy file %s: %s\n", path, strerror (errno));
(void) close (in_fd);
(void) close (out_fd);
return B_FALSE;
}
(void) close (in_fd);
(void) close (out_fd);
return B_TRUE;
}
#define BOOTRCPATH "/boot/solaris/bootenv.rc"
#define BOOTRCLEN 24
#define MENULSTPATH "/boot/grub/menu.lst"
#define MENULSTLEN 19
#define VFSTABPATH "/etc/vfstab"
#define VFSTABLEN 11
#define DOTCDROMPATH "/.cdrom"
#define DOTCDROMLEN 7
/*
* Install a file/directory/symlink. Called by copy_files
*/
static int
process_path (const char *path, const struct stat *statptr, int fileflag, struct FTW *pftw)
{
int read;
char base[PATH_MAX], dest[PATH_MAX], target[PATH_MAX];
/*
* Work out where we're copying to... again
*/
if (realpath (cdrom_path, base) == NULL)
{
perror ("Unable to resolve cdrom path");
return 1;
}
switch (fileflag)
{
case FTW_F:
(void) sprintf (dest, "%s/%s", temp_mount, path + strlen (base));
/*
* Replace /boot/solaris/bootenv.rc with a generated one
*/
if (strncmp (path + strlen (path) - BOOTRCLEN, BOOTRCPATH, BOOTRCLEN) == 0)
{
FILE *fp;
if ((fp = fopen (dest, "w+")) == NULL)
{
perror ("Unable to open bootenv.rc");
return 1;
}
fprintf (fp, "#\n");
fprintf (fp, "# Copyright 2005 Sun Microsystems, Inc. All rights reserved.\n");
fprintf (fp, "# Use is subject to license terms.\n");
fprintf (fp, "#\n");
fprintf (fp, "# bootenv.rc -- boot \"environment variables\"\n");
fprintf (fp, "#\n");
fprintf (fp, "#setprop kbd-type German\n");
fprintf (fp, "setprop kbd-type US-English\n");
fprintf (fp, "setprop ata-dma-enabled 1\n");
fprintf (fp, "setprop atapi-cd-dma-enabled 1\n");
fprintf (fp, "setprop ttyb-rts-dtr-off false\n");
fprintf (fp, "setprop ttyb-ignore-cd true\n");
fprintf (fp, "setprop ttya-rts-dtr-off false\n");
fprintf (fp, "setprop ttya-ignore-cd true\n");
fprintf (fp, "setprop ttyb-mode 9600,8,n,1,-\n");
fprintf (fp, "setprop ttya-mode 9600,8,n,1,-\n");
fprintf (fp, "setprop lba-access-ok 1\n");
(void) fclose (fp);
}
/*
* Replace /boot/grub/menu.lst with a generated one
*/
else if (strncmp (path + strlen (path) - MENULSTLEN, MENULSTPATH, MENULSTLEN) == 0)
{
FILE *fp;
if ((fp = fopen (dest, "w+")) == NULL)
{
perror ("Unable to open menu.lst");
return 1;
}
/*
* TODO: Find out if there is some menu.lst file parser library or
* SOMETHING that can be used instead of including this entire file!
*/
fprintf (fp, "#\n");
fprintf (fp, "# default menu entry to boot\n");
fprintf (fp, "default 0\n");
fprintf (fp, "#\n");
fprintf (fp, "# menu timeout in second before default OS is booted\n");
fprintf (fp, "# set to -1 to wait for user input\n");
fprintf (fp, "timeout 10\n");
fprintf (fp, "#\n");
fprintf (fp, "# To enable grub serial console to ttya uncomment the following lines\n");
fprintf (fp, "# and comment out the splashimage line below\n");
fprintf (fp, "# WARNING: don't enable grub serial console when BIOS console serial\n");
fprintf (fp, "# redirection is active!!!\n");
fprintf (fp, "# serial --unit=0 --speed=9600\n");
fprintf (fp, "# terminal serial\n");
fprintf (fp, "#\n");
fprintf (fp, "# Uncomment the following line to enable GRUB splashimage on console\n");
fprintf (fp, "# splashimage /boot/grub/splash.xpm.gz\n");
fprintf (fp, "splashimage /boot/grub/splash.xpm.gz\n");
fprintf (fp, "#\n");
fprintf (fp, "# To chainload another OS\n");
fprintf (fp, "#\n");
fprintf (fp, "# title Another OS\n");
fprintf (fp, "# root (hd<disk no>,<partition no>)\n");
fprintf (fp, "# chainloader +1\n");
fprintf (fp, "#\n");
fprintf (fp, "# To chainload a Solaris release not based on grub\n");
fprintf (fp, "#\n");
fprintf (fp, "# title Solaris 9\n");
fprintf (fp, "# root (hd<disk no>,<partition no>)\n");
fprintf (fp, "# chainloader +1\n");
fprintf (fp, "# makeactive\n");
fprintf (fp, "#\n");
fprintf (fp, "# To load a Solaris instance based on grub\n");
fprintf (fp, "# If GRUB determines if the booting system is 64-bit capable,\n");
fprintf (fp, "# the kernel$ and module$ commands expand $ISADIR to \"amd64\"\n");
fprintf (fp, "#\n");
fprintf (fp, "# title Solaris <version>\n");
fprintf (fp, "# root (hd<disk no>,<partition no>,x) --x = Solaris root slice\n");
fprintf (fp, "# kernel$ /platform/i86pc/kernel/$ISADIR/unix\n");
fprintf (fp, "# module$ /platform/i86pc/$ISADIR/boot_archive\n");
fprintf (fp, "\n");
fprintf (fp, "#\n");
fprintf (fp, "# To override Solaris boot args (see kernel(1M)), console device and\n");
fprintf (fp, "# properties set via eeprom(1M) edit the \"kernel\" line to:\n");
fprintf (fp, "#\n");
fprintf (fp, "# kernel /platform/i86pc/kernel/unix <boot-args> -B prop1=val1,prop2=val2,...\n");
fprintf (fp, "#\n");
fprintf (fp, "\n");
fprintf (fp, "title SchilliX build-147i partition a\n");
fprintf (fp, " root (hd0,0,a)\n");
fprintf (fp, " kernel$ /platform/i86pc/kernel/$ISADIR/unix -v -B $ZFS-BOOTFS\n");
fprintf (fp, " module$ /platform/i86pc/$ISADIR/boot_archive\n");
fprintf (fp, "\n");
fprintf (fp, "title SchilliX failsafe build-147i partition a\n");
fprintf (fp, " root (hd0,0,a)\n");
fprintf (fp, " kernel /platform/i86pc/kernel/unix -v -B $ZFS-BOOTFS,keyboard-layout=Ask\n");
fprintf (fp, " module /boot/grub/boot_archive\n");
fprintf (fp, "\n");
fprintf (fp, "title Memtest X86\n");
fprintf (fp, " root (hd0,0,a)\n");
fprintf (fp, " kernel /boot/grub/memtest.bin\n");
(void) fclose (fp);
}
/*
* Replace /etc/vfstab with a generated one
*/
else if (strncmp (path + strlen (path) - VFSTABLEN, VFSTABPATH, VFSTABLEN) == 0)
{
FILE *fp;
if ((fp = fopen (dest, "w+")) == NULL)
{
perror ("Unable to open vfstab");
return 1;
}
fprintf (fp, "#device device mount FS fsck mount mount\n");
fprintf (fp, "#to mount to fsck point type pass at boot options\n");
fprintf (fp, "#\n");
fprintf (fp, "/devices - /devices devfs - no -\n");
fprintf (fp, "/proc - /proc proc - no -\n");
fprintf (fp, "ctfs - /system/contract ctfs - no -\n");
fprintf (fp, "objfs - /system/object objfs - no -\n");
fprintf (fp, "sharefs - /etc/dfs/sharetab sharefs - no -\n");
fprintf (fp, "fd - /dev/fd fd - no -\n");
fprintf (fp, "swap - /tmp tmpfs - yes -\n");
(void) fclose (fp);
}
else
{
/*
* Copy file to new destination
*/
if (copy_file (path, dest, statptr) == B_FALSE)
{
fprintf (stderr, "Unable to copy %s\n", path);
return 1;
}
}
break;
case FTW_D:
/*
* Create new directory and copy permissions
*/
(void) sprintf (dest, "%s/%s", temp_mount, path + strlen (base));
/*
* Don't bother copying the /.cdrom dir as it confuses the
* boot scripts into thinking it's still running live
*/
if (strncmp (path + strlen (path) - DOTCDROMLEN, DOTCDROMPATH, DOTCDROMLEN) == 0
&& pftw->level == 1)
return 0;
if (mkdir (dest, statptr->st_mode) == -1)
{
/*
* If the directory exists just copy permissions as it might be a mountpoint
*/
if (errno == EEXIST)
{
/*
* But not on the parent directory/root mountpoint
*/
if (pftw->level == 0)
return 0;
if (chmod (dest, statptr->st_mode) == -1)
{
fprintf (stderr, "Unable to chmod directory %s: %s\n", dest, strerror (errno));
return 1;
}
}
else
{
fprintf (stderr, "Unable to create directory %s: %s\n", dest, strerror (errno));
return 1;
}
}
if (chown (dest, statptr->st_uid, statptr->st_gid) == -1)
{
fprintf (stderr, "Unable to chown directory %s: %s\n", dest, strerror (errno));
return 1;
}
break;
case FTW_SL:
/*
* Replicate symlink
*/
(void) sprintf (dest, "%s/%s", temp_mount, path + strlen (base));
if ((read = readlink (path, target, PATH_MAX)) == -1)
{
fprintf (stderr, "Unable to read symlink %s: %s\n", path, strerror (errno));
return 1;
}
target[read] = '\0';
if (symlink(target, dest) == -1)
{
/*
* If the symlink exists recreat it
*/
if (errno == EEXIST)
{
if (unlink (dest) == -1)
{
fprintf (stderr, "Unable to remove symlink %s: %s\n", dest, strerror (errno));
return 1;
}
if (symlink (target, dest) == -1)
{
fprintf (stderr, "Unable to recreate symlink %s: %s\n", dest, strerror (errno));
return 1;
}
}
else
{
fprintf (stderr, "Unable to replicate symlink %s: %s\n", path, strerror (errno));
return 1;
}
}
break;
default:
/*
* Ignoring an error might result in an unbootable system!
*/
abort();
}
return 0;
}
/*
* Copy livecd files to new root fs
*/
boolean_t
copy_files (void)
{
char path[PATH_MAX];
if (realpath(cdrom_path, path) == NULL)
{
perror ("Error: Unable to resolve cdrom path");
return B_FALSE;
}
if (nftw(path, &process_path, 0, FTW_PHYS) != 0)
{
fprintf (stderr, "Error: Unable to traverse directory: %s\n", path);
return B_FALSE;
}
return B_TRUE;
}
#define ROOT_USER 0
#define STAFF_GROUP 10
/*
* Copy grub files to rpool
*/
boolean_t
copy_grub (char *mnt, char *rpool)
{
char dest[PATH_MAX], path[PATH_MAX];
mode_t mode = 0;
/*
* 0755. U = RWX, G = RX, A = X
*/
mode |= S_IRUSR | S_IWUSR | S_IXUSR;
mode |= S_IRGRP | S_IXGRP;
mode |= S_IROTH |S_IXOTH;
/*
* ZFS boot pools have one global boot directory
*/
(void) sprintf (dest, "%s/%s/boot", mnt, rpool);
if (mkdir (dest, mode) == -1)
{
perror ("Error: Unable to create boot directory");
return B_FALSE;
}
if (chown (dest, ROOT_USER, STAFF_GROUP) == -1)
{
perror ("Error: Unable to chown boot directory");
return B_FALSE;
}
/*
* Create grub directory
*/
(void) sprintf (dest, "%s/%s/boot/grub", mnt, rpool);
if (mkdir (dest, mode) == -1)
{
perror ("Error: Unable to create grub directory");
return B_FALSE;
}
if (chown (dest, ROOT_USER, STAFF_GROUP) == -1)
{
perror ("Error: Unable to chown grub directory");
return B_FALSE;
}
/*
* Copy /grub/capability
*/
(void) sprintf (path, "%s/boot/grub/capability", mnt);
(void) sprintf (dest, "%s/%s/boot/grub/capability", mnt, rpool);
if (copy_file (path, dest, NULL) == B_FALSE)
{
fprintf (stderr, "Error: Unable to copy %s\n", path);
return B_FALSE;
}
/*
* Copy /grub/menu.lst
*/
(void) sprintf (path, "%s/boot/grub/menu.lst", mnt);
(void) sprintf (dest, "%s/%s/boot/grub/menu.lst", mnt, rpool);
if (copy_file (path, dest, NULL) == B_FALSE)
{
fprintf (stderr, "Error: Unable to copy %s\n", path);
return B_FALSE;
}
/*
* Copy /grub/splash.xpm.gz
*/
(void) sprintf (path, "%s/boot/grub/splash.xpm.gz", mnt);
(void) sprintf (dest, "%s/%s/boot/grub/splash.xpm.gz", mnt, rpool);
if (copy_file (path, dest, NULL) == B_FALSE)
{
fprintf (stderr, "Error: Unable to copy %s\n", path);
return B_FALSE;
}
return B_TRUE;
}