Project

General

Profile

Download (17.2 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	installer.php
4
	part of pfSense (http://www.pfsense.com/)
5
	Copyright (C) 2010 Scott Ullrich <sullrich@gmail.com>
6
	All rights reserved.
7

    
8
	Redistribution and use in source and binary forms, with or without
9
	modification, are permitted provided that the following conditions are met:
10

    
11
	1. Redistributions of source code must retain the above copyright notice,
12
	   this list of conditions and the following disclaimer.
13

    
14
	2. Redistributions in binary form must reproduce the above copyright
15
	   notice, this list of conditions and the following disclaimer in the
16
	   documentation and/or other materials provided with the distribution.
17

    
18
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
22
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
	POSSIBILITY OF SUCH DAMAGE.
28
*/
29

    
30
require("globals.inc");
31
require("guiconfig.inc");
32

    
33
define('PC_SYSINSTALL', '/PCBSD/pc-sysinstall/pc-sysinstall');
34

    
35
// Handle other type of file systems
36
if($_REQUEST['fstype']) 
37
	$fstype = strtoupper($_REQUEST['fstype']);
38
else 
39
	$fstype = "UFS+S";
40

    
41
if($g['platform'] == "pfSense" or $g['platform'] == "nanobsd") {
42
	Header("Location: /index.php");
43
	exit;
44
}
45

    
46
// Main switch dispatcher
47
switch ($_REQUEST['state']) {
48
	case "quickeasyinstall":
49
		quickeasyinstall_gui();
50
		break;
51
	case "update_installer_status":
52
		update_installer_status();
53
		exit;
54
	default:
55
		installer_main();	
56
}
57

    
58
function write_out_pc_sysinstaller_config($disk) {
59
	global $fstype;
60
	$fd = fopen("/PCBSD/pc-sysinstall/examples/pfSense-install.cfg", "w");
61
	if(!$fd) {
62
		return true;
63
	}
64
	$config = <<<EOF
65
# Sample configuration file for an installation using pc-sysinstall
66

    
67
installMode=fresh
68
installInteractive=yes
69
installType=FreeBSD
70
installMedium=LiveCD
71

    
72
# Set the disk parameters
73
disk0={$disk}
74
partition=all
75
bootManager=bsd
76
commitDiskPart
77

    
78
# Setup the disk label
79
# All sizes are expressed in MB
80
# Avail FS Types, UFS, UFS+S, UFS+J, ZFS, SWAP
81
# Size 0 means use the rest of the slice size
82
disk0-part={$fstype} 0 / 
83
# Do it now!
84
commitDiskLabel
85

    
86
# Set if we are installing via optical, USB, or FTP
87
installType=FreeBSD
88

    
89
packageType=cpdup
90

    
91
# Optional Components
92
cpdupPaths=boot,COPYRIGHT,bin,conf,conf.default,dev,etc,home,kernels,libexec,lib,root,sbin,sys,usr,var
93

    
94
# runExtCommand=chmod a+rx /usr/local/bin/after_installation_routines.sh && cd / && /usr/local/bin/after_installation_routines.sh
95
EOF;
96
	fwrite($fd, $config);
97
	fclose($fd);
98
	return;
99
}
100

    
101
function start_installation() {
102
	global $g, $fstype;
103
	if(file_exists("/tmp/install_complete"))
104
		return;
105
	$ps_running = exec("ps awwwux | grep -v grep | grep 'sh /tmp/installer.sh'");
106
	if($ps_running)	
107
		return;
108
	$fd = fopen("/tmp/installer.sh", "w");
109
	if(!$fd) {
110
		die(gettext("Could not open /tmp/installer.sh for writing"));
111
		exit;
112
	}
113
	fwrite($fd, "rm /tmp/.pc-sysinstall/pc-sysinstall.log 2>/dev/null\n");
114
	fwrite($fd, "/PCBSD/pc-sysinstall/pc-sysinstall -c /PCBSD/pc-sysinstall/examples/pfSense-install.cfg \n");
115
	fwrite($fd, "chmod a+rx /usr/local/bin/after_installation_routines.sh\n");
116
	fwrite($fd, "cd / && /usr/local/bin/after_installation_routines.sh\n");
117
	fwrite($fd, "mkdir /mnt/tmp\n");
118
	fwrite($fd, "umount /mnt\n");
119
	fwrite($fd, "touch /tmp/install_complete\n");
120
	fclose($fd);
121
	exec("chmod a+rx /tmp/installer.sh");
122
	mwexec_bg("sh /tmp/installer.sh");
123
}
124

    
125
function installer_find_first_disk() {
126
	global $g, $fstype;
127
	$disk = `/PCBSD/pc-sysinstall/pc-sysinstall disk-list | head -n1 | cut -d':' -f1`;
128
	return $disk;
129
}
130

    
131
// Return an array with all disks information.
132
function installer_find_all_disks() {
133
	global $g, $fstype;
134
	$disk = split("\n", `/PCBSD/pc-sysinstall/pc-sysinstall disk-list`);
135
	$disks_array = array();
136
	foreach($disk as $d) {
137
		$disks_info = split(":", $d);
138
		$tmp_array = array();
139
		$disk_info = split("\n", `/PCBSD/pc-sysinstall/pc-sysinstall disk-info {$disks_info[0]}`);
140
		foreach($disk_info as $di) { 
141
			$di_s = split("=", $di);
142
			if($di_s[0])
143
				$tmp_array[$di_s[0]] = $di_s[1];
144
		}
145
		$tmp_array['disk'] = trim($disks_info[0]);
146
		$tmp_array['desc'] = trim(htmlentities($disks_info[1]));
147
		$disks_array[] = $tmp_array;
148
	}
149
	return $disks_array;
150
}
151

    
152
function update_installer_status() {
153
	global $g, $fstype;
154
	// Ensure status files exist
155
	if(!file_exists("/tmp/installer_installer_running"))
156
		touch("/tmp/installer_installer_running");
157
	$status = `cat /tmp/.pc-sysinstall/pc-sysinstall.log`;
158
	$status = str_replace("\n", "\\n", $status);
159
	$status = str_replace("\n", "\\r", $status);
160
	echo "this.document.forms[0].installeroutput.value='$status';\n";
161
	echo "this.document.forms[0].installeroutput.scrollTop = this.document.forms[0].installeroutput.scrollHeight;\n";	
162
	// Find out installer progress
163
	$progress = "5";
164
	if(strstr($status, "Running: dd")) 
165
		$progress = "6";
166
	if(strstr($status, "Running: gpart create -s GPT")) 
167
		$progress = "7";
168
	if(strstr($status, "Running: gpart bootcode")) 
169
		$progress = "7";
170
	if(strstr($status, "Running: newfs -U")) 
171
		$progress = "8";
172
	if(strstr($status, "Running: sync")) 
173
		$progress = "9";
174
	if(strstr($status, "/boot /mnt/boot")) 
175
		$progress = "10";
176
	if(strstr($status, "/COPYRIGHT /mnt/COPYRIGHT"))
177
		$progress = "11";
178
	if(strstr($status, "/bin /mnt/bin"))
179
		$progress = "12";
180
	if(strstr($status, "/conf /mnt/conf"))
181
		$progress = "15";
182
	if(strstr($status, "/conf.default /mnt/conf.default"))
183
		$progress = "20";
184
	if(strstr($status, "/dev /mnt/dev"))
185
		$progress = "25";
186
	if(strstr($status, "/etc /mnt/etc"))
187
		$progress = "30";
188
	if(strstr($status, "/home /mnt/home"))
189
		$progress = "35";
190
	if(strstr($status, "/kernels /mnt/kernels"))
191
		$progress = "40";
192
	if(strstr($status, "/libexec /mnt/libexec"))
193
		$progress = "50";
194
	if(strstr($status, "/lib /mnt/lib"))
195
		$progress = "60";
196
	if(strstr($status, "/root /mnt/root"))
197
		$progress = "70";
198
	if(strstr($status, "/sbin /mnt/sbin"))
199
		$progress = "75";
200
	if(strstr($status, "/sys /mnt/sys"))
201
		$progress = "80";
202
	if(strstr($status, "/usr /mnt/usr"))
203
		$progress = "95";
204
	if(strstr($status, "/usr /mnt/usr"))
205
		$progress = "90";
206
	if(strstr($status, "/var /mnt/var"))
207
		$progress = "95";
208
	if(strstr($status, "cap_mkdb /etc/login.conf"))
209
		$progress = "96";
210
	if(strstr($status, "Setting hostname"))
211
		$progress = "97";
212
	if(strstr($status, "umount -f /mnt"))
213
		$progress = "98";
214
	if(strstr($status, "umount -f /mnt"))
215
		$progress = "99";
216
	if(strstr($status, "Installation finished"))
217
		$progress = "100";
218
	// Check for error and bail if we see one.
219
	if(stristr($status, "error")) {
220
		$error = true;
221
		echo "\$('installerrunning').innerHTML='<img class=\"infoboxnpimg\" src=\"/themes/{$g['theme']}/images/icons/icon_exclam.gif\"> <font size=\"2\"><b>An error occurred.  Aborting installation.'; ";
222
		echo "\$('progressbar').style.width='100%';\n";
223
		unlink("/tmp/install_complete");
224
		return;
225
	}
226
	$running_old = trim(file_get_contents("/tmp/installer_installer_running"));
227
	if($installer_running <> "running") {
228
		$ps_running = exec("ps awwwux | grep -v grep | grep 'sh /tmp/installer.sh'");
229
		if($ps_running)	{
230
			$running = "\$('installerrunning').innerHTML='<table><tr><td valign=\"middle\"><img src=\"/themes/{$g['theme']}/images/misc/loader.gif\"></td><td valign=\"middle\">&nbsp;<font size=\"2\"><b>Installer running ({$progress}% completed)...</td></tr></table>'; ";
231
			if($running_old <> $running) {
232
				echo $running;
233
				file_put_contents("/tmp/installer_installer_running", "$running");			
234
			}
235
		}
236
	}
237
	if($progress) 
238
		echo "\$('progressbar').style.width='{$progress}%';\n";
239
	if(file_exists("/tmp/install_complete")) {
240
		echo "\$('installerrunning').innerHTML='<img class=\"infoboxnpimg\" src=\"/themes/{$g['theme']}/images/icons/icon_exclam.gif\"> <font size=\"+1\">Installation completed.  Please <a href=\"reboot.php\">reboot</a> to continue';\n";
241
		unlink_if_exists("/tmp/installer.sh");
242
		file_put_contents("/tmp/installer_installer_running", "finished");
243
	}
244
}
245

    
246
function update_installer_status_win($status) {
247
	global $g, $fstype;
248
	echo "<script type=\"text/javascript\">\n";
249
	echo "	\$('installeroutput').value = '" . str_replace(htmlentities($status), "\n", "") . "';\n";
250
	echo "</script>";
251
}
252

    
253
function begin_quick_easy_install() {
254
	global $g, $fstype;
255
	if(file_exists("/tmp/install_complete"))
256
		return;
257
	unlink_if_exists("/tmp/install_complete");
258
	$disk = installer_find_first_disk();
259
	if(!$disk) {
260
		// XXX: hide progress bar
261
		$savemsg = gettext("Could not find a suitable disk for installation");
262
		update_installer_status_win(gettext("Could not find a suitable disk for installation."));
263
		return;
264
	}
265
	write_out_pc_sysinstaller_config($disk);
266
	update_installer_status_win(sprintf(gettext("Beginning installation on disk %s."),$disk));
267
	start_installation();
268
}
269

    
270
function head_html() {
271
	global $g, $fstype;
272
	echo <<<EOF
273
<html>
274
	<head>
275
		<style type='text/css'>
276
			a:link { 
277
				color: #000000;
278
				text-decoration:underline;
279
				font-size:14;
280
			}
281
			a:visited { 
282
				color: #000000;
283
				text-decoration:underline;
284
				font-size:14;
285
			}
286
			a:hover { 
287
				color: #FFFF00;
288
				text-decoration: none;
289
				font-size:14;
290
			}
291
			a:active { 
292
				color: #FFFF00;
293
				text-decoration:underline;
294
				font-size:14;
295
			}
296
		</style>
297
	</head>
298
EOF;
299

    
300
}
301

    
302
function body_html() {
303
	global $g, $fstype;
304
	$pfSversion = str_replace("\n", "", file_get_contents("/etc/version"));
305
	if(strstr($pfSversion, "1.2"))
306
		$one_two = true;
307
	$pgtitle = "{$g['product_name']}: " . gettext("Installer");
308
	include("head.inc");
309
	echo <<<EOF
310
	<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
311
	<script src="/javascript/scriptaculous/prototype.js" type="text/javascript"></script>
312
	<script type="text/javascript">
313
		function getinstallerprogress() {
314
			url = 'installer.php';
315
			pars = 'state=update_installer_status';
316
			callajax(url, pars, installcallback);
317
		}
318
		function callajax(url, pars, activitycallback) {
319
			var myAjax = new Ajax.Request(
320
				url,
321
				{
322
					method: 'post',
323
					parameters: pars,
324
					onComplete: activitycallback
325
				});
326
		}
327
		function installcallback(transport) {
328
			setTimeout('getinstallerprogress()', 2000);
329
			eval(transport.responseText);
330
		}
331
	</script>
332
EOF;
333

    
334
	if($one_two)
335
		echo "<p class=\"pgtitle\">{$pgtitle}</font></p>";
336

    
337
	if ($savemsg) print_info_box($savemsg); 
338
}
339

    
340
function end_html() {
341
	global $g, $fstype;
342
	echo "</form>";
343
	echo "</body>";
344
	echo "</html>";
345
}
346

    
347
function template() {
348
	global $g, $fstype;
349
	head_html();
350
	body_html();
351
	echo <<<EOF
352
	<div id="mainlevel">
353
		<table width="100%" border="0" cellpadding="0" cellspacing="0">
354
	 		<tr>
355
	    		<td>
356
					<div id="mainarea">
357
						<table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0">
358
							<tr>
359
	     						<td class="tabcont" >
360
	      							<form action="installer.php" method="post">
361
									<div id="pfsensetemplate">
362

    
363

    
364
									</div>
365
	     						</td>
366
							</tr>
367
						</table>
368
					</div>
369
				</td>
370
			</tr>
371
		</table>
372
	</div>
373
EOF;
374
	end_html();
375
}
376

    
377
function quickeasyinstall_gui() {
378
	global $g, $fstype;
379
	head_html();
380
	body_html();
381
	echo "<form action=\"installer.php\" method=\"post\" state=\"step1_post\">";
382
	page_table_start();
383
	echo <<<EOF
384
	<center>
385
		<table width="100%">
386
		<tr><td>
387
			<div id="mainlevel">
388
				<table width="100%" border="0" cellpadding="0" cellspacing="0">
389
			 		<tr>
390
			    		<td>
391
							<div id="mainarea">
392
								<table width="100%" border="0" cellpadding="0" cellspacing="0">
393
									<tr>
394
			     						<td>
395
											<div id="pfsenseinstaller" width="100%">
396
												<div id='installerrunning' width='100%' style="padding:8px; border:1px dashed #000000">
397
													<table>
398
														<tr>
399
															<td valign="middle">
400
																<img src="/themes/{$g['theme']}/images/misc/loader.gif">
401
															</td>
402
															<td valign="middle">
403
																&nbsp;<font size="2"><b>Starting Installer...  Please wait...
404
															</td>
405
														</tr>
406
													</table>
407
												</div>
408
												<br/>
409
												<center>
410
												<table height='15' width='640' border='0' colspacing='0' cellpadding='0' cellspacing='0'>
411
													<tr>
412
														<td background="./themes/the_wall/images/misc/bar_left.gif" height='15' width='5'>
413
														</td>
414
														<td>
415
															<table id="progholder" name="progholder" height='15' width='630' border='0' colspacing='0' cellpadding='0' cellspacing='0'>
416
																<td background="./themes/the_wall/images/misc/bar_gray.gif" valign="top" align="left">
417
																	<img src='./themes/the_wall/images/misc/bar_blue.gif' width='0' height='15' name='progressbar' id='progressbar'>
418
																</td>
419
															</table>
420
														</td>
421
														<td background="./themes/the_wall/images/misc/bar_right.gif" height='15' width='5'>
422
														</td>
423
													</tr>
424
												</table>
425
												<br/>
426
												<textarea name='installeroutput' id='installeroutput' rows="31" cols="90">
427
												</textarea>
428
											</div>
429
			     						</td>
430
									</tr>
431
								</table>
432
							</div>
433
						</td>
434
					</tr>
435
				</table>
436
			</div>
437
		</td></tr>
438
		</table>
439
	</center>
440
	<script type="text/javascript">setTimeout('getinstallerprogress()', 250);</script>
441

    
442
EOF;
443
	page_table_end();
444
	end_html();
445
	begin_quick_easy_install();
446
}
447

    
448
function page_table_start() {
449
	global $g, $fstype;
450
	echo <<<EOF
451
	<center>
452
		<img border="0" src="./themes/{$g['theme']}/images/logo.gif"></a><br/>
453
		<table cellpadding="6" cellspacing="0" width="640" height="480" style="border:1px solid #000000">
454
		<tr height="10" bgcolor="#990000">
455
			<td style="border-bottom:1px solid #000000">
456
				<font color='white'>
457
					<b>
458
						{$g['product_name']} installer
459
					</b>
460
				</font>
461
			</td>
462
		</tr>
463
		<tr>
464
			<td>
465

    
466
EOF;
467

    
468
}
469

    
470
function page_table_end() {
471
	global $g, $fstype;
472
	echo <<<EOF
473
			</td>
474
		</tr>
475
		</table>
476
	</center>
477

    
478
EOF;
479
	
480
}
481

    
482
function installer_custom() {
483
	global $g, $fstype;
484
	if(file_exists("/tmp/.pc-sysinstall/pc-sysinstall.log")) 
485
		unlink("/tmp/.pc-sysinstall/pc-sysinstall.log");
486
	head_html();
487
	body_html();
488
	// Only enable ZFS if this exists.  The install will fail otherwise.
489
	if(file_exists("/boot/gptzfsboot")) 
490
		$zfs_enabled = "or <a href=\"installer.php?state=quickeasyinstall&fstype=ZFS\">ZFS</a> ";
491
	$disk = installer_find_first_disk();
492
	if(!$disk) 
493
		echo gettext("WARNING: Could not find any suitable disks for installation.");
494
	page_table_start();
495
	echo <<<EOF
496
		<form action="installer.php" method="post" state="step1_post">
497
			<div id="mainlevel">
498
				<center>
499
				<b><font face="arial" size="+2">Welcome to the {$g['product_name']} PCSysInstaller!</b></font><p/>
500
				<font face="arial" size="+1">This utility will install {$g['product_name']} to a hard disk, flash drive, etc.</font>
501
				<table width="100%" border="0" cellpadding="5" cellspacing="0">
502
			 		<tr>
503
			    		<td>
504
							<center>
505
							<div id="mainarea">
506
								<br/>
507
								<center>
508
								Please select an installer option to begin:
509
								<table width="100%" border="0" cellpadding="5" cellspacing="5">
510
									<tr>
511
			     						<td>
512
											<div id="pfsenseinstaller">
513
												<center>
514
												Rescue config.xml<p/>
515
												Install {$g['product_name']} using the <a href="installer.php?state=quickeasyinstall">UFS</a>
516
												 {$zfs_enabled}
517
												filesystem.
518
												</p>
519
											</div>
520
			     						</td>
521
									</tr>
522
								</table>
523
							</div>
524
						</td>
525
					</tr>
526
				</table>
527
			</div>
528
EOF;
529
	page_table_end();
530
	end_html();
531
}
532

    
533
function installer_main() {
534
	global $g, $fstype;
535
	if(file_exists("/tmp/.pc-sysinstall/pc-sysinstall.log")) 
536
		unlink("/tmp/.pc-sysinstall/pc-sysinstall.log");
537
	head_html();
538
	body_html();
539
	// Only enable ZFS if this exists.  The install will fail otherwise.
540
	if(file_exists("/boot/gptzfsboot")) 
541
		$zfs_enabled = "or <a href=\"installer.php?state=quickeasyinstall&fstype=ZFS\">ZFS</a> ";
542
	$disk = installer_find_first_disk();
543
	if(!$disk) 
544
		echo gettext("WARNING: Could not find any suitable disks for installation.");
545
	page_table_start();
546
	echo <<<EOF
547
		<form action="installer.php" method="post" state="step1_post">
548
			<div id="mainlevel">
549
				<center>
550
				<b><font face="arial" size="+2">Welcome to the {$g['product_name']} PCSysInstaller!</b></font><p/>
551
				<font face="arial" size="+1">This utility will install {$g['product_name']} to a hard disk, flash drive, etc.</font>
552
				<table width="100%" border="0" cellpadding="5" cellspacing="0">
553
			 		<tr>
554
			    		<td>
555
							<center>
556
							<div id="mainarea">
557
								<br/>
558
								<center>
559
								Please select an installer option to begin:
560
								<table width="100%" border="0" cellpadding="5" cellspacing="5">
561
									<tr>
562
			     						<td>
563
											<div id="pfsenseinstaller">
564
												<center>
565
												Rescue config.xml<p/>
566
												Install {$g['product_name']} using the <a href="installer.php?state=quickeasyinstall">UFS</a>
567
												 {$zfs_enabled}
568
												filesystem.
569
												</p>
570
											</div>
571
			     						</td>
572
									</tr>
573
								</table>
574
							</div>
575
						</td>
576
					</tr>
577
				</table>
578
			</div>
579
EOF;
580
	page_table_end();
581
	end_html();
582
}
583

    
584
echo "<pre>";
585
print_r(installer_find_all_disks());
586
echo "</pre>";
587

    
588
?>
(81-81/222)