Project

General

Profile

Download (9.42 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	diag_nanobsd.php
4
	Copyright (C) 2009 Scott Ullrich <sullrich@gmail.com>
5
	All rights reserved.
6

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

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

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

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

    
29
##|+PRIV
30
##|*IDENT=page-diagnostics-nanobsd
31
##|*NAME=Diagnostics: NanoBSD
32
##|*DESCR=Allow access to the 'Diagnostics: NanoBSD' page.
33
##|*MATCH=diag_nanobsd.php*
34
##|-PRIV
35

    
36
ini_set('zlib.output_compression', 0);
37
ini_set('implicit_flush', 1);
38
ini_set('max_input_time', '9999');
39

    
40
require_once("guiconfig.inc");
41
require_once("config.inc");
42

    
43
$pgtitle = array("Diagnostics","NanoBSD");
44
include("head.inc");
45

    
46
function detect_slice_info() {
47
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
48
	global $GLABEL_SLIZE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
49
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE;
50

    
51
	$BOOT_DEVICE=trim(`/sbin/mount | /usr/bin/grep pfsense | /usr/bin/cut -d'/' -f4 | /usr/bin/cut -d' ' -f1`);
52
	$REAL_BOOT_DEVICE=trim(`/sbin/glabel list | /usr/bin/grep -B2 ufs/{$BOOT_DEVICE} | /usr/bin/head -n 1 | /usr/bin/cut -f3 -d' '`);
53
	$BOOT_DRIVE=trim(`/sbin/glabel list | /usr/bin/grep -B2 ufs/pfsense | /usr/bin/head -n 1 | /usr/bin/cut -f3 -d' ' | /usr/bin/cut -d's' -f1`);
54

    
55
	// Detect which slice is active and set information.
56
	if(strstr($REAL_BOOT_DEVICE, "s1")) {
57
		$SLICE="2";
58
		$OLDSLICE="1";
59
		$TOFLASH="{$BOOT_DRIVE}s{$SLICE}";
60
		$COMPLETE_PATH="{$BOOT_DRIVE}s{$SLICE}a";
61
		$COMPLETE_BOOT_PATH="{$BOOT_DRIVE}s{$OLDSLICE}";	
62
		$GLABEL_SLICE="pfsense1";
63
		$UFS_ID="1";
64
		$OLD_UFS_ID="0";
65
		$BOOTFLASH="{$BOOT_DRIVE}s{$OLDSLICE}";
66

    
67
	} else {
68
		$SLICE="1";
69
		$OLDSLICE="2";
70
		$TOFLASH="{$BOOT_DRIVE}s{$SLICE}";
71
		$COMPLETE_PATH="{$BOOT_DRIVE}s{$SLICE}a";
72
		$COMPLETE_BOOT_PATH="{$BOOT_DRIVE}s{$OLDSLICE}";
73
		$GLABEL_SLICE="pfsense0";
74
		$UFS_ID="0";
75
		$OLD_UFS_ID="1";
76
		$BOOTFLASH="{$BOOT_DRIVE}s{$OLDSLICE}";
77
	}
78
}
79

    
80
// Survey slice info
81
detect_slice_info();
82

    
83
?>
84

    
85
<body link="#0000CC" vlink="#0000CC" alink="#0000CC" onload="<?=$jsevents["body"]["onload"];?>">
86
<script src="/javascript/scriptaculous/prototype.js" type="text/javascript"></script>
87

    
88
<?php include("fbegin.inc"); ?>
89

    
90
<?php
91

    
92
if($_POST['bootslice']) {
93
	echo <<<EOF
94
	 	<div id="loading">
95
			<img src="/themes/metallic/images/misc/loader.gif"> 
96
			Setting slice information, please wait...
97
			<p/>&nbsp;
98
		</div>
99
EOF;
100
	for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
101
	ob_implicit_flush(1);
102
	if(strstr($_POST['bootslice'], "s2")) {
103
		$ASLICE="2";
104
		$AOLDSLICE="1";
105
		$ATOFLASH="{$BOOT_DRIVE}s{$ASLICE}";
106
		$ACOMPLETE_PATH="{$BOOT_DRIVE}s{$ASLICE}a";
107
		$AGLABEL_SLICE="pfsense1";
108
		$AUFS_ID="1";
109
		$AOLD_UFS_ID="0";
110
		$ABOOTFLASH="{$BOOT_DRIVE}s{$AOLDSLICE}";
111
	} else {
112
		$ASLICE="1";
113
		$AOLDSLICE="2";
114
		$ATOFLASH="{$BOOT_DRIVE}s{$ASLICE}";
115
		$ACOMPLETE_PATH="{$BOOT_DRIVE}s{$ASLICE}a";
116
		$AGLABEL_SLICE="pfsense0";
117
		$AUFS_ID="0";
118
		$AOLD_UFS_ID="1";
119
		$ABOOTFLASH="{$BOOT_DRIVE}s{$AOLDSLICE}";
120
	}
121
	conf_mount_rw();
122
	exec("sysctl kern.geom.debugflags=16");	
123
	exec("gpart set -a active -i {$ASLICE} {$BOOT_DRIVE}");
124
	exec("/usr/sbin/boot0cfg -s {$ASLICE} -v /dev/{$BOOT_DRIVE}");
125
	exec("/bin/mkdir /tmp/{$AGLABEL_SLICE}");
126
	exec("/sbin/fsck_ufs -y /dev/{$ACOMPLETE_PATH}");
127
	exec("/sbin/mount /dev/ufs/{$AGLABEL_SLICE} /tmp/{$AGLABEL_SLICE}");
128
	$fstab = <<<EOF
129
/dev/ufs/{$AGLABEL_SLICE} / ufs ro 1 1
130
/dev/ufs/cf /cf ufs ro 1 1	
131
EOF;
132
	file_put_contents("/tmp/{$AGLABEL_SLICE}/etc/fstab", $fstab);
133
	exec("/sbin/umount /tmp/{$AGLABEL_SLICE}");
134
	exec("sysctl kern.geom.debugflags=0");
135
	conf_mount_ro();
136
	$savemsg = "The boot slice has been set to {$BOOT_DRIVE} {$AGLABEL_SLICE}";
137
	// Survey slice info
138
	detect_slice_info();
139

    
140
}
141

    
142
if($_POST['destslice']) {
143

    
144
echo <<<EOF
145
 	<div id="loading">
146
		<img src="/themes/metallic/images/misc/loader.gif">
147
		Duplicating slice.  Please wait, this will take a moment...
148
		<p/>&nbsp;
149
	</div>
150
EOF;
151
	for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
152
	ob_implicit_flush(1);
153
	exec("sysctl kern.geom.debugflags=16");
154
	exec("dd if=/dev/zero of=/dev/{$TOFLASH} bs=1m count=1");
155
	exec("/bin/dd if=/dev/{$BOOTFLASH} of=/dev/{$TOFLASH} bs=64k");
156
	exec("/sbin/tunefs -L {$GLABEL_SLICE} /dev/{$COMPLETE_PATH}");
157
	exec("/bin/mkdir /tmp/{$GLABEL_SLICE}");
158
	exec("/sbin/fsck_ufs -y /dev/{$COMPLETE_PATH}");
159
	exec("/sbin/mount /dev/ufs/{$GLABEL_SLICE} /tmp/{$GLABEL_SLICE}");
160
	exec("/bin/cp /etc/fstab /tmp/{$GLABEL_SLICE}/etc/fstab");
161
	$status = exec("sed -i \"\" \"s/pfsense{$OLD_UFS_ID}/pfsense{$UFS_ID}/g\" /tmp/{$GLABEL_SLICE}/etc/fstab");
162
	if($status) {
163
		exec("/sbin/umount /tmp/{$GLABEL_SLICE}");
164
		$savemsg = "There was an error while duplicating the slice.  Operation aborted.";
165
	} else {
166
		$savemsg = "The slice has been duplicated.<p/>If you would like to boot from this newly duplicated slice please set it using the bootup information area.";
167
		exec("/sbin/umount /tmp/{$GLABEL_SLICE}");
168
	}
169
	exec("sysctl kern.geom.debugflags=0");
170
	// Re-Survey slice info
171
	detect_slice_info();
172
}
173

    
174
if ($savemsg)
175
	print_info_box($savemsg)
176

    
177
?>
178
<table width="100%" border="0" cellpadding="0" cellspacing="0">
179
	<tr>
180
		<td>
181
			<!-- tabs here if you want them -->
182
		</td>
183
	</tr>
184
	<tr>
185
		<td id="mainarea">
186
			<div class="tabcont">
187
				<span class="vexpl">
188
					<span class="red">
189
						<strong>NOTE:&nbsp</strong>
190
					</span>
191
					The options on this page are intended for use by advanced users only.
192
					<br/>&nbsp;
193
				</span>
194
				<p/>
195
				<table width="100%" border="0" cellpadding="6" cellspacing="0">
196
					<tr>
197
						<td colspan="2" valign="top" class="listtopic">Bootup information</td>
198
					</tr>
199
					<tr>
200
						<td width="22%" valign="top" class="vncell">Bootup</td>
201
						<td width="78%" class="vtable">
202
							<form action="diag_nanobsd.php" method="post" name="iform">
203
								Bootup slice:
204
								<select name='bootslice'>
205
									<option value='<?php echo $BOOTFLASH; ?>'>
206
										<?php echo $BOOTFLASH; ?>
207
									</option>
208
									<option value='<?php echo $TOFLASH; ?>'>
209
										<?php echo "{$TOFLASH}"; ?>
210
									</option>
211
								</select>
212
								<br/>
213
								This will set the bootup slice.
214
						</td>
215
					</tr>
216
					<tr>
217
						<td valign="top" class="">&nbsp;</td><td><br/><input type='submit' value='Set bootup'></form></td>
218
					</tr>
219
					<tr>
220
						<td colspan="2" valign="top" class="">&nbsp;</td>
221
					</tr>					
222
					<tr>
223
						<td colspan="2" valign="top" class="listtopic">Duplicate bootup slice to alternate</td>
224
					</tr>
225
					<tr>
226
						<td width="22%" valign="top" class="vncell">Duplicate bootup slice</td>
227
						<td width="78%" class="vtable">
228
							<form action="diag_nanobsd.php" method="post" name="iform">						
229
								Destination slice:							
230
								<select name='destslice'>
231
									<option value='<?php echo $COMPLETE_PATH; ?>'>
232
										<?php echo "{$COMPLETE_BOOT_PATH} -> {$TOFLASH}"; ?>
233
									</option>
234
								</select>
235
								<br/>
236
								This will duplicate the bootup slice to the alternate slice.  Use this if you would like to duplicate the known good working boot partition to the alternate.
237
						</td>
238
					</tr>
239
					<tr>
240
						<td valign="top" class="">&nbsp;</td><td><br/><input type='submit' value='Duplicate slice'></form></td>
241
					</tr>
242
<?php if(file_exists("/conf/upgrade_log.txt")): ?>
243
					<tr>
244
						<td colspan="2" valign="top" class="">&nbsp;</td>
245
					</tr>					
246
					<tr>
247
						<td colspan="2" valign="top" class="listtopic">View upgrade log</td>
248
					</tr>
249
					<tr>
250
						<td width="22%" valign="top" class="vncell">View previous upgrade log</td>
251
						<td width="78%" class="vtable">
252
						<?php
253
							if($_POST['viewupgradelog']) {
254
								echo "<textarea name='log' cols='80' rows='40'>";
255
								echo file_get_contents("/conf/upgrade_log.txt");
256
								echo "\nFile list:\n";
257
								echo file_get_contents("/conf/file_upgrade_log.txt");
258
								echo "\nMisc log:\n";
259
								echo file_get_contents("/conf/firmware_update_misc.log");
260
								echo "\nfdisk/bsdlabel log:\n";
261
								echo file_get_contents("/conf/fdisk_upgrade_log.txt");
262
								echo "</textarea>";
263
							} else {
264
								echo "<form action='diag_nanobsd.php' method='post' name='iform'>";
265
								echo "<input type='submit' name='viewupgradelog' value='View upgrade log'>";
266
							}
267
						?>
268
						</td>
269
					</tr>
270
<?php endif; ?>
271
					<tr>
272
						<td colspan="2" valign="top" class="">&nbsp;</td>
273
					</tr>					
274
				</table>
275
			</div>
276
		</td>
277
	</tr>
278
</table>
279
<?php require("fend.inc"); ?>
280
</body>
281
</html>
282

    
283
<?php
284

    
285
// Clear the loading indicator
286
echo "<script type=\"text/javascript\">";
287
echo "$('loading').innerHTML = '';";
288
echo "</script>";	
289

    
290
?>
(26-26/217)