Project

General

Profile

Download (14.8 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
// Handle other type of file systems
34
if($_REQUEST['fstype']) 
35
	$fstype = strtoupper($_REQUEST['fstype']);
36
else 
37
	$fstype = "UFS+S";
38

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

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

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

    
65
installMode=fresh
66
installInteractive=yes
67
installType=FreeBSD
68
installMedium=LiveCD
69

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

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

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

    
87
packageType=cpdup
88

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

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

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

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

    
129
function update_installer_status() {
130
	global $g, $fstype;
131
	// Ensure status files exist
132
	if(!file_exists("/tmp/installer_installer_running"))
133
		touch("/tmp/installer_installer_running");
134
	$status = `cat /tmp/.pc-sysinstall/pc-sysinstall.log`;
135
	$status = str_replace("\n", "\\n", $status);
136
	$status = str_replace("\n", "\\r", $status);
137
	echo "this.document.forms[0].installeroutput.value='$status';\n";
138
	echo "this.document.forms[0].installeroutput.scrollTop = this.document.forms[0].installeroutput.scrollHeight;\n";	
139
	// Find out installer progress
140
	$progress = "5";
141
	if(strstr($status, "Running: dd")) 
142
		$progress = "6";
143
	if(strstr($status, "Running: gpart create -s GPT")) 
144
		$progress = "7";
145
	if(strstr($status, "Running: gpart bootcode")) 
146
		$progress = "7";
147
	if(strstr($status, "Running: newfs -U")) 
148
		$progress = "8";
149
	if(strstr($status, "Running: sync")) 
150
		$progress = "9";
151
	if(strstr($status, "/boot /mnt/boot")) 
152
		$progress = "10";
153
	if(strstr($status, "/COPYRIGHT /mnt/COPYRIGHT"))
154
		$progress = "11";
155
	if(strstr($status, "/bin /mnt/bin"))
156
		$progress = "12";
157
	if(strstr($status, "/conf /mnt/conf"))
158
		$progress = "15";
159
	if(strstr($status, "/conf.default /mnt/conf.default"))
160
		$progress = "20";
161
	if(strstr($status, "/dev /mnt/dev"))
162
		$progress = "25";
163
	if(strstr($status, "/etc /mnt/etc"))
164
		$progress = "30";
165
	if(strstr($status, "/home /mnt/home"))
166
		$progress = "35";
167
	if(strstr($status, "/kernels /mnt/kernels"))
168
		$progress = "40";
169
	if(strstr($status, "/libexec /mnt/libexec"))
170
		$progress = "50";
171
	if(strstr($status, "/lib /mnt/lib"))
172
		$progress = "60";
173
	if(strstr($status, "/root /mnt/root"))
174
		$progress = "70";
175
	if(strstr($status, "/sbin /mnt/sbin"))
176
		$progress = "75";
177
	if(strstr($status, "/sys /mnt/sys"))
178
		$progress = "80";
179
	if(strstr($status, "/usr /mnt/usr"))
180
		$progress = "95";
181
	if(strstr($status, "/usr /mnt/usr"))
182
		$progress = "90";
183
	if(strstr($status, "/var /mnt/var"))
184
		$progress = "95";
185
	if(strstr($status, "cap_mkdb /etc/login.conf"))
186
		$progress = "96";
187
	if(strstr($status, "Setting hostname"))
188
		$progress = "97";
189
	if(strstr($status, "umount -f /mnt"))
190
		$progress = "98";
191
	if(strstr($status, "umount -f /mnt"))
192
		$progress = "99";
193
	if(strstr($status, "Installation finished"))
194
		$progress = "100";
195
	// Check for error and bail if we see one.
196
	if(stristr($status, "error")) {
197
		$error = true;
198
		echo "\$('installerrunning').innerHTML='<img class=\"infoboxnpimg\" src=\"/themes/{$g['theme']}/images/icons/icon_exclam.gif\"> <font size=\"2\"><b>An error occurred.  Aborting installation.'; ";
199
		echo "\$('progressbar').style.width='100%';\n";
200
		unlink("/tmp/install_complete");
201
		return;
202
	}
203
	$running_old = trim(file_get_contents("/tmp/installer_installer_running"));
204
	if($installer_running <> "running") {
205
		$ps_running = exec("ps awwwux | grep -v grep | grep 'sh /tmp/installer.sh'");
206
		if($ps_running)	{
207
			$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>'; ";
208
			if($running_old <> $running) {
209
				echo $running;
210
				file_put_contents("/tmp/installer_installer_running", "$running");			
211
			}
212
		}
213
	}
214
	if($progress) 
215
		echo "\$('progressbar').style.width='{$progress}%';\n";
216
	if(file_exists("/tmp/install_complete")) {
217
		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";
218
		unlink_if_exists("/tmp/installer.sh");
219
		file_put_contents("/tmp/installer_installer_running", "finished");
220
	}
221
}
222

    
223
function update_installer_status_win($status) {
224
	global $g, $fstype;
225
	echo "<script type=\"text/javascript\">\n";
226
	echo "	\$('installeroutput').value = '" . str_replace(htmlentities($status), "\n", "") . "';\n";
227
	echo "</script>";
228
}
229

    
230
function begin_quick_easy_install() {
231
	global $g, $fstype;
232
	if(file_exists("/tmp/install_complete"))
233
		return;
234
	unlink_if_exists("/tmp/install_complete");
235
	$disk = installer_find_first_disk();
236
	if(!$disk) {
237
		// XXX: hide progress bar
238
		$savemsg = gettext("Could not find a suitable disk for installation");
239
		update_installer_status_win(gettext("Could not find a suitable disk for installation."));
240
		return;
241
	}
242
	write_out_pc_sysinstaller_config($disk);
243
	update_installer_status_win(sprintf(gettext("Beginning installation on disk %s."),$disk));
244
	start_installation();
245
}
246

    
247
function head_html() {
248
	global $g, $fstype;
249
	echo <<<EOF
250
<html>
251
	<head>
252
		<style type='text/css'>
253
			a:link { 
254
				color: #000000;
255
				text-decoration:underline;
256
				font-size:14;
257
			}
258
			a:visited { 
259
				color: #000000;
260
				text-decoration:underline;
261
				font-size:14;
262
			}
263
			a:hover { 
264
				color: #FFFF00;
265
				text-decoration: none;
266
				font-size:14;
267
			}
268
			a:active { 
269
				color: #FFFF00;
270
				text-decoration:underline;
271
				font-size:14;
272
			}
273
		</style>
274
	</head>
275
EOF;
276

    
277
}
278

    
279
function body_html() {
280
	global $g, $fstype;
281
	$pfSversion = str_replace("\n", "", file_get_contents("/etc/version"));
282
	if(strstr($pfSversion, "1.2"))
283
		$one_two = true;
284
	$pgtitle = "{$g['product_name']}: " . gettext("Installer");
285
	include("head.inc");
286
	echo <<<EOF
287
	<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
288
	<script src="/javascript/scriptaculous/prototype.js" type="text/javascript"></script>
289
	<script type="text/javascript">
290
		function getinstallerprogress() {
291
			url = 'installer.php';
292
			pars = 'state=update_installer_status';
293
			callajax(url, pars, installcallback);
294
		}
295
		function callajax(url, pars, activitycallback) {
296
			var myAjax = new Ajax.Request(
297
				url,
298
				{
299
					method: 'post',
300
					parameters: pars,
301
					onComplete: activitycallback
302
				});
303
		}
304
		function installcallback(transport) {
305
			setTimeout('getinstallerprogress()', 2000);
306
			eval(transport.responseText);
307
		}
308
	</script>
309
EOF;
310

    
311
	if($one_two)
312
		echo "<p class=\"pgtitle\">{$pgtitle}</font></p>";
313

    
314
	if ($savemsg) print_info_box($savemsg); 
315
}
316

    
317
function end_html() {
318
	global $g, $fstype;
319
	echo "</form>";
320
	echo "</body>";
321
	echo "</html>";
322
}
323

    
324
function template() {
325
	global $g, $fstype;
326
	head_html();
327
	body_html();
328
	echo <<<EOF
329
	<div id="mainlevel">
330
		<table width="100%" border="0" cellpadding="0" cellspacing="0">
331
	 		<tr>
332
	    		<td>
333
					<div id="mainarea">
334
						<table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0">
335
							<tr>
336
	     						<td class="tabcont" >
337
	      							<form action="installer.php" method="post">
338
									<div id="pfsensetemplate">
339

    
340

    
341
									</div>
342
	     						</td>
343
							</tr>
344
						</table>
345
					</div>
346
				</td>
347
			</tr>
348
		</table>
349
	</div>
350
EOF;
351
	end_html();
352
}
353

    
354
function quickeasyinstall_gui() {
355
	global $g, $fstype;
356
	head_html();
357
	body_html();
358
	echo "<form action=\"installer.php\" method=\"post\" state=\"step1_post\">";
359
	page_table_start();
360
	echo <<<EOF
361
	<center>
362
		<table width="100%">
363
		<tr><td>
364
			<div id="mainlevel">
365
				<table width="100%" border="0" cellpadding="0" cellspacing="0">
366
			 		<tr>
367
			    		<td>
368
							<div id="mainarea">
369
								<table width="100%" border="0" cellpadding="0" cellspacing="0">
370
									<tr>
371
			     						<td>
372
											<div id="pfsenseinstaller" width="100%">
373
												<div id='installerrunning' width='100%' style="padding:8px; border:1px dashed #000000">
374
													<table>
375
														<tr>
376
															<td valign="middle">
377
																<img src="/themes/{$g['theme']}/images/misc/loader.gif">
378
															</td>
379
															<td valign="middle">
380
																&nbsp;<font size="2"><b>Starting Installer...  Please wait...
381
															</td>
382
														</tr>
383
													</table>
384
												</div>
385
												<br/>
386
												<center>
387
												<table height='15' width='640' border='0' colspacing='0' cellpadding='0' cellspacing='0'>
388
													<tr>
389
														<td background="./themes/the_wall/images/misc/bar_left.gif" height='15' width='5'>
390
														</td>
391
														<td>
392
															<table id="progholder" name="progholder" height='15' width='630' border='0' colspacing='0' cellpadding='0' cellspacing='0'>
393
																<td background="./themes/the_wall/images/misc/bar_gray.gif" valign="top" align="left">
394
																	<img src='./themes/the_wall/images/misc/bar_blue.gif' width='0' height='15' name='progressbar' id='progressbar'>
395
																</td>
396
															</table>
397
														</td>
398
														<td background="./themes/the_wall/images/misc/bar_right.gif" height='15' width='5'>
399
														</td>
400
													</tr>
401
												</table>
402
												<br/>
403
												<textarea name='installeroutput' id='installeroutput' rows="31" cols="90">
404
												</textarea>
405
											</div>
406
			     						</td>
407
									</tr>
408
								</table>
409
							</div>
410
						</td>
411
					</tr>
412
				</table>
413
			</div>
414
		</td></tr>
415
		</table>
416
	</center>
417
	<script type="text/javascript">setTimeout('getinstallerprogress()', 250);</script>
418

    
419
EOF;
420
	page_table_end();
421
	end_html();
422
	begin_quick_easy_install();
423
}
424

    
425
function page_table_start() {
426
	global $g, $fstype;
427
	echo <<<EOF
428
	<center>
429
		<img border="0" src="./themes/{$g['theme']}/images/logo.gif"></a><br/>
430
		<table cellpadding="6" cellspacing="0" width="640" height="480" style="border:1px solid #000000">
431
		<tr height="10" bgcolor="#990000">
432
			<td style="border-bottom:1px solid #000000">
433
				<font color='white'>
434
					<b>
435
						{$g['product_name']} installer
436
					</b>
437
				</font>
438
			</td>
439
		</tr>
440
		<tr>
441
			<td>
442

    
443
EOF;
444

    
445
}
446

    
447
function page_table_end() {
448
	global $g, $fstype;
449
	echo <<<EOF
450
			</td>
451
		</tr>
452
		</table>
453
	</center>
454

    
455
EOF;
456
	
457
}
458

    
459
function installer_main() {
460
	global $g, $fstype;
461
	if(file_exists("/tmp/.pc-sysinstall/pc-sysinstall.log")) 
462
		unlink("/tmp/.pc-sysinstall/pc-sysinstall.log");
463
	head_html();
464
	body_html();
465
	// Only enable ZFS if this exists.  The install will fail otherwise.
466
	if(file_exists("/boot/gptzfsboot")) 
467
		$zfs_enabled = "or <a href=\"installer.php?state=quickeasyinstall&fstype=ZFS\">ZFS</a> ";
468
	$disk = installer_find_first_disk();
469
	if(!$disk) 
470
		echo gettext("WARNING: Could not find any suitable disks for installation.");
471
	page_table_start();
472
	echo <<<EOF
473
		<form action="installer.php" method="post" state="step1_post">
474
			<div id="mainlevel">
475
				<center>
476
				<b><font face="arial" size="+2">Welcome to the {$g['product_name']} PCSysInstaller!</b></font><p/>
477
				<font face="arial" size="+1">This utility will install {$g['product_name']} to a hard disk, flash drive, etc.</font>
478
				<table width="100%" border="0" cellpadding="5" cellspacing="0">
479
			 		<tr>
480
			    		<td>
481
							<center>
482
							<div id="mainarea">
483
								<br/>
484
								<center>
485
								Please select an installer option to begin:
486
								<table width="100%" border="0" cellpadding="5" cellspacing="5">
487
									<tr>
488
			     						<td>
489
											<div id="pfsenseinstaller">
490
												<center>
491
												Rescue config.xml<p/>
492
												Install {$g['product_name']} using the <a href="installer.php?state=quickeasyinstall">UFS</a>
493
												 {$zfs_enabled}
494
												filesystem.
495
												</p>
496
											</div>
497
			     						</td>
498
									</tr>
499
								</table>
500
							</div>
501
						</td>
502
					</tr>
503
				</table>
504
			</div>
505
EOF;
506
	page_table_end();
507
	end_html();
508
}
509

    
510
?>
(81-81/222)