Menu

[r4752]: / trunk / etc / inc / install.inc  Maximize  Restore  History

Download this file

675 lines (549 with data), 20.6 kB

  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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
<?php
/*
install.inc
Copyright (C) 2007-2009 Volker Theile (votdev@gmx.de)
All rights reserved.
part of FreeNAS (http://www.freenas.org)
Copyright (C) 2005-2009 Olivier Cochard-Labbe <olivier@freenas.org>.
All rights reserved.
Based on m0n0wall (http://m0n0.ch/wall)
Copyright (C) 2003-2006 Manuel Kasper <mk@neon1.net>.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
require_once("functions.inc");
require_once("util.inc");
// Mount the CDROM.
// Return 0 if successful, otherwise 1
function install_mount_cd($cdrom)
{
write_console("Mount CDROM.\n");
$mntpath = "/mnt/cdrom";
// Creating temporary directory to mount CDROM.
if (1 == mwexec("/bin/mkdir -p {$mntpath}")) {
write_console("Error: Failed to create directory '{$mntpath}'!\n");
return 1;
}
// Mounting the CDROM.
if (1 == mwexec("/sbin/mount_cd9660 /dev/{$cdrom} {$mntpath}")) {
write_console("Error: Failed to mount device '{$cdrom}'!\n");
@rmdir($mntpath);
return 1;
}
return 0;
}
// Unmount CDROM.
// Return 0 if successful, otherwise 1
function install_unmount_cd()
{
write_console("Unmount CDROM.\n");
$result = 0;
$mntpath = "/mnt/cdrom";
// Unmount CDROM
if (1 == mwexec("/sbin/umount {$mntpath}")) {
$result = 1;
}
// Remove temporary mount directory
@rmdir($mntpath);
return $result;
}
// Install the IMG on the destination harddrive.
// Return 0 if successful, otherwise 1
function install_dd_image($harddrive)
{
global $g;
$imgfilename=get_product_name(). "-{$g['arch']}-embedded.gz";
// Test firmware file integrity.
if (mwexec("/usr/bin/gzip -t /mnt/cdrom/{$imgfilename}")) {
write_console("Error: Firmware file is corrupted.\n");
return 1;
}
// dd image file on the destination disk.
write_console("Installing system image on device {$harddrive}.\n");
if (mwexec("/usr/bin/gzip -cd /mnt/cdrom/{$imgfilename} | /bin/dd of=/dev/{$harddrive} bs=16k > /dev/null 2>&1")) {
write_console("Error: Failed to dd image on '/dev/{$harddrive}'!\n");
return 1;
}
return 0;
}
// Install FreeNAS on the destination harddrive.
// Return 0 if successful, otherwise 1
function install_harddrive_image($harddrive)
{
global $g;
$mntpath = "/mnt/inst_{$harddrive}";
// Mount disk used to install OS.
mwexec("/bin/mkdir -p {$mntpath}");
if (1 == mwexec("/sbin/mount /dev/{$harddrive}s1a {$mntpath}")) {
write_console("Error: Failed to mount '/dev/{$harddrive}s1a'!\n");
return 1;
}
// Install system files.
write_console("Installing system files on device {$harddrive}s1.\n");
mwexec("/usr/bin/tar --exclude mnt/ --exclude dev/ --exclude var/ --exclude tmp/ --exclude {$g['cf_path']}/ -c -f - -C / . | tar -xvpf - -C {$mntpath}");
mwexec("/bin/mkdir -p {$mntpath}/var");
mwexec("/bin/mkdir -p {$mntpath}/dev");
mwexec("/bin/mkdir -p {$mntpath}/mnt");
mwexec("/bin/mkdir -p {$mntpath}/tmp");
mwexec("/bin/mkdir -p {$mntpath}/boot/defaults");
mwexec("/bin/cp -v /mnt/cdrom/boot/* {$mntpath}/boot");
mwexec("/bin/cp -v /mnt/cdrom/boot/defaults/* {$mntpath}/boot/defaults");
mwexec("/bin/cp -v /mnt/cdrom/boot/kernel/* {$mntpath}/boot/kernel");
// Install configuration file
mwexec("/bin/mkdir -p {$mntpath}{$g['cf_conf_path']}");
mwexec("/bin/cp -v {$g['conf_default_path']}/config.xml {$mntpath}{$g['conf_path']}");
// Generate new loader.conf file.
$loaderconf .= <<<EOD
kernel="kernel"
bootfile="kernel"
kernel_options=""
kern.hz="100"
EOD;
if (false === file_put_contents("{$mntpath}/boot/loader.conf", $loaderconf)) {
write_console("Error: Failed to create '/boot/loader.conf'.\n");
return 1;
}
// Set the release type.
$platform .= <<<EOD
{$g['arch']}-full
EOD;
if (false === file_put_contents("{$mntpath}/etc/platform", $platform)) {
write_console("Error: Failed to create '/etc/platform'.\n");
return 1;
}
// Decompress kernel.
mwexec("/usr/bin/gzip -d -f {$mntpath}/boot/kernel/kernel.gz");
// Generate /etc/fstab.
$fstab .= <<<EOD
# Device Mountpoint FStype Options Dump Pass#
/dev/{$harddrive}s1a / ufs rw 1 1
EOD;
if (false === file_put_contents("{$mntpath}/etc/fstab", $fstab)) {
write_console("Error: Failed to create '/etc/fstab'.\n");
return 1;
}
// Generating the /etc/cfdevice (this file is linked in /var/etc at bootup)
// This file is used by the firmware and mount check and is normally
// generated with 'liveCD' and 'embedded' during startup, but need to be
// created during install of 'full'.
$cfdevice .= <<<EOD
{$harddrive}s1a
EOD;
if (false === file_put_contents("{$mntpath}/etc/cfdevice", $cfdevice)) {
write_console("Error: Failed to create '/etc/cfdevice'.\n");
return 1;
}
// Unmount disk containing OS
mwexec("/sbin/umount {$mntpath}");
mwexec("/bin/rm -f -r {$mntpath}");
return 0;
}
// Upgrade a FreeNAS 'full' release on the destination harddrive.
// Return 0 if successful, otherwise 1
function install_upgrade_full($harddrive)
{
global $g;
$mntpath = "/mnt/inst_{$harddrive}";
$sysbackup = "/tmp/inst_{$harddrive}_bak";
// Mount disk used to install OS
mwexec("/bin/mkdir -p {$mntpath}");
if (1 == mwexec("/sbin/mount /dev/{$harddrive}s1a {$mntpath}")) {
write_console("Error: Failed to mount '/dev/{$harddrive}s1a'!\n");
return 1;
}
// Backup system files
write_console("Backup system configuration.\n");
mwexec("/bin/mkdir -p {$sysbackup}");
mwexec("/bin/cp -p {$mntpath}/boot/loader.conf {$sysbackup}");
mwexec("/bin/cp -p {$mntpath}/etc/platform {$sysbackup}");
mwexec("/bin/cp -p {$mntpath}/etc/fstab {$sysbackup}");
mwexec("/bin/cp -p {$mntpath}/etc/cfdevice {$sysbackup}");
// Start upgrade script to remove obsolete files. This should be done
// before system is updated because it may happen that some files
// may be reintroduced in the system.
write_console("Remove obsolete files.\n");
mwexec("/etc/install/upgrade.sh clean {$mntpath}");
// Install system files
write_console("Installing system files on device {$harddrive}s1.\n");
mwexec("/usr/bin/tar --exclude mnt/ --exclude dev/ --exclude var/ --exclude tmp/ --exclude {$g['cf_path']}/ -c -f - -C / . | tar -xvpf - -C {$mntpath}");
mwexec("/bin/mkdir -p {$mntpath}/var");
mwexec("/bin/mkdir -p {$mntpath}/dev");
mwexec("/bin/mkdir -p {$mntpath}/mnt");
mwexec("/bin/mkdir -p {$mntpath}/tmp");
mwexec("/bin/mkdir -p {$mntpath}/boot/defaults");
mwexec("/bin/cp -v /mnt/cdrom/boot/* {$mntpath}/boot");
mwexec("/bin/cp -v /mnt/cdrom/boot/defaults/* {$mntpath}/boot/defaults");
mwexec("/bin/cp -v /mnt/cdrom/boot/kernel/* {$mntpath}/boot/kernel");
// Decompress kernel
mwexec("/usr/bin/gzip -d -f {$mntpath}/boot/kernel/kernel.gz");
// Restore system files
write_console("Restore system configuration.\n");
mwexec("/bin/cp -p {$sysbackup}/loader.conf {$mntpath}/boot");
mwexec("/bin/cp -p {$sysbackup}/platform {$mntpath}/etc");
mwexec("/bin/cp -p {$sysbackup}/fstab {$mntpath}/etc");
mwexec("/bin/cp -p {$sysbackup}/cfdevice {$mntpath}/etc");
// Cleanup system backup files
mwexec("/bin/rm -f -r {$sysbackup}");
// Unmount disk containing OS
mwexec("/sbin/umount {$mntpath}");
mwexec("/bin/rm -f -r {$mntpath}");
return 0;
}
// Backup the configuration file on the hard drive/CF and unmount this drive.
// Return 0 if successful, otherwise 1
function install_backup_config($disk)
{
global $g;
$tmp_cf_bak = "/tmp/cf_bak";
$mnt_cf_disk = "/mnt/inst_{$disk}";
write_console("Backup configuration.\n");
/* Creating temporary directory for the configuration file. */
if (1 == mwexec("/bin/mkdir -p {$tmp_cf_bak}")) {
write_console("Error: Failed to create '{$tmp_cf_bak}'!\n");
return 1;
}
/* Creating temporary directory for the disk containing the configuration file. */
if (1 == mwexec("/bin/mkdir -p {$mnt_cf_disk}")) {
write_console("Error: Failed to create '{$mnt_cf_disk}'!\n");
return 1;
}
/* Mount the drive to be upgraded (readonly mode). */
if (1 == mwexec("/sbin/mount -r /dev/{$disk} {$mnt_cf_disk}")) {
write_console("Error: Failed to mount device '{$disk}'!\n");
return 1;
}
/* Backup the configuration file */
if (1 == mwexec("/bin/cp -p {$mnt_cf_disk}/conf/config.xml {$tmp_cf_bak}")) {
write_console("Error: Failed to backup the configuration file!\n");
return 1;
}
/* Unmount the drive to be upgraded */
if (1 == mwexec("/sbin/umount {$mnt_cf_disk}")) {
write_console("Error: Failed to unmount '{$mnt_cf_disk}'!\n");
return 1;
}
return 0;
}
function install_restore_config($disk)
{
global $g;
$tmp_cf_bak = "/tmp/cf_bak";
$mnt_cf_disk = "/mnt/inst_{$disk}";
write_console("Restore configuration.\n");
/* Mount the drive to be upgraded */
if (1 == mwexec("/sbin/mount /dev/{$disk} {$mnt_cf_disk}")) {
write_console("Error: Failed to mount device '{$disk}'!\n");
return 1;
}
/* Restore the configuration file */
if (1 == mwexec("/bin/cp -p {$tmp_cf_bak}/config.xml {$mnt_cf_disk}/conf")) {
write_console("Error: Failed to restore the configuration file!\n");
return 1;
}
/* Unmount the upgraded drive */
if (1 == mwexec("/sbin/umount {$mnt_cf_disk}")) {
write_console("Error: Failed to unmount '{$mnt_cf_disk}'!\n");
return 1;
}
/* Cleanup */
if (1 == mwexec("/bin/rm -f -r {$mnt_cf_disk} {$tmp_cf_bak}")) {
write_console("Error: Failed to remove temporary backup data!\n");
return 1;
}
return 0;
}
// Create two partitions on the destination hard drive.
// $disk - Disk name (e.g. ad0)
// $partlayout - The layout description of the disk, e.g.
// array(
// array("slice" => 1, "fstype" => "4.2BSD", "size" => 64, "volname" => "root"),
// array("slice" => 2, "fstype" => "4.2BSD", "size" => "*", "volname" => "data"),
// array("slice" => 3, "fstype" => "swap", "size" => 200, "volname" => "")
// array("slice" => 4, "fstype" => "unused", "size" => 0, "volname" => "")
// )
// Return 0 if successful, otherwise 1
function install_init_disk($disk, $partlayout)
{
// Initialize the destination using fdisk.
write_console("Create slice/partition table on device {$disk}.\n");
$slicesize = array(0, 0, 0, 0);
foreach ($partlayout as $partlayoutv) {
$slicesize[$partlayoutv['slice'] - 1] = $partlayoutv['size'];
}
if (install_fdisk($disk, $slicesize)) {
write_console("Error: Failed to create slice/partition table!\n");
return 1;
}
// Create disk labels.
write_console("Create BSD label on slice/partition:\n");
foreach ($partlayout as $partlayoutv) {
$diskdevicename = "{$disk}s{$partlayoutv['slice']}";
write_console(" {$diskdevicename}\n");
if (install_bsdlabel($diskdevicename, $partlayoutv['fstype'])) {
write_console("\nError: Failed to create BSD label on {$diskdevicename}!\n");
return 1;
}
}
// Create file systems and set volume label.
write_console("Create file system on slice/partition:\n");
foreach ($partlayout as $partlayoutv) {
// Do not create a file system for swap/unused partitions.
if ("swap" === $partlayoutv['fstype'] || "unused" === $partlayoutv['fstype'])
continue;
$diskdevicename = "{$disk}s{$partlayoutv['slice']}";
write_console(" {$diskdevicename}\n");
if (install_newfs($diskdevicename, $partlayoutv['volname'])) {
write_console("\nError: Failed to create file system on {$diskdevicename}!\n");
return 1;
}
}
return 0;
}
// Initialize harddrive. It creates the root (p1), data (p2) and swap (p3) partitions.
// $disk - Disk name (e.g. ad0)
// $slicesize - Array containing the size of each slice in MB, e.g. array(64, 100, *, 0)
// Return 0 if successful, otherwise 1
function install_fdisk($disk, $slicesize)
{
global $g;
$slicetable = array(
array("slice" => 1, "type" => 0, "start" => 0, "length" => 0),
array("slice" => 2, "type" => 0, "start" => 0, "length" => 0),
array("slice" => 3, "type" => 0, "start" => 0, "length" => 0),
array("slice" => 4, "type" => 0, "start" => 0, "length" => 0)
);
$activeslice = 0;
// Get disk informations
$diskinfo = disks_get_diskinfo($disk);
// Get valid CHS information. 'diskinfo' gets values from harddisk firmware that
// are maybe unusable by 'fdisk' (e.g. sectors = 255, fdisk requires <= 63).
disk_sanitize_bios_geom($diskinfo);
// Calculate the size of the slices
$usedsectors = 0;
foreach ($slicesize as $slicesizek => $slicesizev) {
if (is_numeric($slicesizev)) {
$bytes = $slicesizev * 1024 * 1024;
$slicetable[$slicesizek]['length'] = $bytes / $diskinfo['sectorsize'];
// Sum total used sectors
$usedsectors += $slicetable[$slicesizek]['length'];
}
}
foreach ($slicesize as $slicesizek => $slicesizev) {
if ("*" === $slicesizev) { // Use the rest of the disk space for this slice.
$slicetable[$slicesizek]['length'] = $diskinfo['mediasize_sectors'] - $usedsectors;
// Stop processing here (only one '*' is accepted and processed)
break;
}
}
// Check if partition layout fit disk size
if ($diskinfo['mediasize_sectors'] <= $usedsectors) {
write_console("Error: Partition layout does not fit on disk {$diskinfo['name']}.\n");
write_console(" Requested sectors: {$usedsectors}\n");
write_console(" Available sectors: {$diskinfo['mediasize_sectors']}\n");
write_console(" Layout: ");
foreach ($slicesize as $slicesizev)
write_console(" {$slicesizev}");
write_console("\n");
return 1;
}
// Calculate slice start sectors and type
$startsector = 1;
foreach ($slicesize as $slicesizek => $slicesizev) {
if ( 0 >= $slicetable[$slicesizek]['length'])
continue;
// Set the first found slice with length > 0 as active
if (0 == $activeslice)
$activeslice = $slicesizek + 1;
$slicetable[$slicesizek]['type'] = 165;
$slicetable[$slicesizek]['start'] = $startsector;
// Calculate start of next slice
$startsector += $slicetable[$slicesizek]['length'] + 1;
}
// Create fdisk config file (fdisk.conf)
$fdiskconf = "g c{$diskinfo['cylinders']} h{$diskinfo['heads']} s{$diskinfo['sectors']}\n";
for ($i = 0; $i < 4; ++$i) {
$fdiskconf .= "p {$slicetable[$i]['slice']} {$slicetable[$i]['type']} {$slicetable[$i]['start']} {$slicetable[$i]['length']}\n";
}
$fdiskconf .= "a {$activeslice}\n";
if (false === file_put_contents("{$g['tmp_path']}/fdisk.conf", $fdiskconf)) {
write_console("Error: Failed to create '{$g['tmp_path']}/fdisk.conf'.\n");
return 1;
}
// Delete MBR and partition table.
if (mwexec("/bin/dd if=/dev/zero of=/dev/{$disk} bs={$diskinfo['sectorsize']} count=1")) {
write_console("Error: Failed to delete MBR and partition table on disk {$disk}.\n");
return 1;
}
// Delete GEOM metadata.
$oseek = $diskinfo['mediasize_sectors'] - 1;
if (mwexec("/bin/dd if=/dev/zero of=/dev/{$disk} bs={$diskinfo['sectorsize']} oseek={$oseek} count=1")) {
write_console("Error: Failed to delete GEOM metadata on disk {$disk}.\n");
return 1;
}
// Set slice values
if (mwexec("/sbin/fdisk -f {$g['tmp_path']}/fdisk.conf {$disk}")) {
write_console("Error: Failed to write slice table on disk {$disk}.\n");
return 1;
}
// Reinitialize the boot code contained in sector 0 of the disk
if (mwexec("(/bin/echo y; /bin/echo y) | /sbin/fdisk -B -b /boot/mbr {$disk}")) {
write_console("Error: Failed to reinitialize the boot code on disk {$disk}!\n");
return 1;
}
return 0;
}
// Create disk label.
// $disk - Disk name (e.g. ad0s1)
// $fstype - The file system type (e.g. 4.2BSD or swap)
// Return 0 if successful, otherwise 1
function install_bsdlabel($disk, $fstype)
{
global $g;
// Completely wipe any prior information on the partition
if (mwexec("/bin/dd if=/dev/zero of=/dev/{$disk} bs=512 count=32")) {
write_console("\nError: Failed to wipe any prior information on {$disk}.\n");
return 1;
}
// Do an automatic label first
if (mwexec("/sbin/bsdlabel -w -B {$disk}")) {
write_console("\nError: Failed to write standard label.\n");
return 1;
}
// Read the label to manipulate it
exec("/sbin/bsdlabel {$disk} > {$g['tmp_path']}/label.tmp", $output, $result);
if ($result) {
write_console("\nError: Failed to backup label.\n");
return 1;
}
// Put the file conent into an array
$label = file("{$g['tmp_path']}/label.tmp");
// Open this file in add mode
$fd = fopen("{$g['tmp_path']}/label.tmp", 'a');
while(list(,$val) = each($label)) {
if (ereg ("unused",$val)) {
$val = ereg_replace("c:", "a:", $val);
// Replace 'unused' with '$fstype'
$val = ereg_replace("unused", $fstype, $val);
// Adding this line add the end of the file
fwrite($fd, $val);
}
}
// Closing file
fclose($fd);
// Injecting modified label
if (mwexec("/sbin/bsdlabel -R -B {$disk} {$g['tmp_path']}/label.tmp")) {
write_console("\nError: Failed to write modified label.\n");
return 1;
}
// Cleanup
unlink("{$g['tmp_path']}/label.tmp");
return 0;
}
// Create new file system on disk.
// $disk - Disk name (e.g. ad0s1)
// $volname - The new volume label (e.g. 'data')
// Return 0 if successful, otherwise 1
function install_newfs($disk, $volname) {
$param = "-U -b 16384 -f 2048";
if (!empty($volname))
$param .= " -L \"{$volname}\"";
if (mwexec("/sbin/newfs {$param} /dev/{$disk}")) {
return 1;
}
return 0;
}
// Detect device where the OS is installed.
// $mode [in] - Search for 'embedded' or 'full' installation.
// $devinfo [out] - Array (
// [device] => ad0
// [partition] => s1
// )
// Return 0 if successful, otherwise 1
function install_detect_installation($mode, &$devinfo) {
global $g;
$result = 1;
$devinfo = array();
echo "Try to detect installation...\n";
// Create temporary mountpoint
$mnt_cf_disk = "/mnt/inst_cf_tmp";
mwexec("/bin/mkdir -p {$mnt_cf_disk}");
// Scan all disks detected from kernel
$adisk = explode(" ", trim(preg_replace("/kern.disks: /", "", exec("/sbin/sysctl kern.disks"))));
foreach ($adisk as $diskv) {
// Skip mfs-mounted filesystems
if (0 < ereg("md[0-9]*", $diskv))
continue;
// Search on slice 1 for OS installed on disk with install script
if (0 == mwexec("/sbin/mount -r /dev/{$diskv}s1 {$mnt_cf_disk}")) {
// Check if configuration file exists
if (file_exists("{$mnt_cf_disk}/conf/config.xml")) {
$devinfo['device'] = $diskv;
$devinfo['partition'] = "s1";
// Check install mode
switch ($mode) {
case 'embedded':
if (file_exists("{$mnt_cf_disk}/mfsroot.gz")) {
$result = 0;
}
break;
case 'full':
$platform = chop(@file_get_contents("{$mnt_cf_disk}{$g['etc_path']}/platform"));
if (FALSE !== ereg("^.*-full", $platform)) {
$result = 0;
}
break;
}
}
// Unmount device
mwexec("/sbin/umount {$mnt_cf_disk}");
if (0 == $result)
break;
}
// Search on partition 'a' for OS IMG image written on disk
if (mwexec("/sbin/mount -r /dev/{$diskv}a {$mnt_cf_disk}") == 0) {
// Check if configuration file exists
if (file_exists("{$mnt_cf_disk}/conf/config.xml")) {
$devinfo['device'] = $diskv;
$devinfo['partition'] = "a";
// Check install mode
switch ($mode) {
case 'embedded':
if (file_exists("{$mnt_cf_disk}/mfsroot.gz")) {
$result = 0;
}
break;
case 'full':
$platform = chop(@file_get_contents("{$mnt_cf_disk}{$g['etc_path']}/platform"));
if (FALSE !== ereg("^.*-full", $platform)) {
$result = 0;
}
break;
}
}
// Unmount device
mwexec("/sbin/umount {$mnt_cf_disk}");
if (0 == $result)
break;
}
}
// Cleanup
mwexec("/bin/rm -f -r {$mnt_cf_disk}");
return $result;
}
?>
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.