Project

General

Profile

Download (10.1 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 = "Diagnostics: NanoBSD";
44
include("head.inc");
45

    
46
$BOOT_DEVICE=trim(`/sbin/mount | /usr/bin/grep pfsense | /usr/bin/cut -d'/' -f4 | /usr/bin/cut -d' ' -f1`);
47
$REAL_BOOT_DEVICE=trim(`/sbin/glabel list | /usr/bin/grep -B2 ufs/{$BOOT_DEVICE} | /usr/bin/head -n 1 | /usr/bin/cut -f3 -d' '`);
48
$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`);
49

    
50
function detect_slice_info() {
51
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
52
	global $GLABEL_SLIZE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
53
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE;
54
	
55
	$BOOT_DEVICE=trim(`/sbin/mount | /usr/bin/grep pfsense | /usr/bin/cut -d'/' -f4 | /usr/bin/cut -d' ' -f1`);
56
	$REAL_BOOT_DEVICE=trim(`/sbin/glabel list | /usr/bin/grep -B2 ufs/{$BOOT_DEVICE} | /usr/bin/head -n 1 | /usr/bin/cut -f3 -d' '`);
57
	$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`);
58

    
59
	// Detect which slice is active and set information.
60
	if(strstr($REAL_BOOT_DEVICE, "s1")) {
61
		$SLICE="2";
62
		$OLDSLICE="1";
63
		$TOFLASH="{$BOOT_DRIVE}s{$SLICE}";
64
		$COMPLETE_PATH="{$BOOT_DRIVE}s{$SLICE}a";
65
		$COMPLETE_BOOT_PATH="{$BOOT_DRIVE}s{$OLDSLICE}";	
66
		$GLABEL_SLICE="pfsense1";
67
		$UFS_ID="1";
68
		$OLD_UFS_ID="0";
69
		$BOOTFLASH="{$BOOT_DRIVE}s{$OLDSLICE}";
70
	} else {
71
		$SLICE="1";
72
		$OLDSLICE="2";
73
		$TOFLASH="{$BOOT_DRIVE}s{$SLICE}";
74
		$COMPLETE_PATH="{$BOOT_DRIVE}s{$SLICE}a";
75
		$COMPLETE_BOOT_PATH="{$BOOT_DRIVE}s{$OLDSLICE}";
76
		$GLABEL_SLICE="pfsense0";
77
		$UFS_ID="0";
78
		$OLD_UFS_ID="1";
79
		$BOOTFLASH="{$BOOT_DRIVE}s{$OLDSLICE}";
80
	}
81
}
82

    
83
// Survey slice info
84
detect_slice_info();
85

    
86
?>
87

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

    
91
<?php include("fbegin.inc"); ?>
92

    
93
<?php
94

    
95
if($_POST['bootslice']) {
96
	echo <<<EOF
97
	 	<div id="loading">
98
			<img src="/themes/metallic/images/misc/loader.gif"> 
99
			Setting slice information, please wait...
100
			<p/>&nbsp;
101
		</div>
102
EOF;
103
	for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
104
	ob_implicit_flush(1);
105
	if(strstr($_POST['bootslice'], "s2")) {
106
		$ASLICE="2";
107
		$AOLDSLICE="1";
108
		$ATOFLASH="{$BOOT_DRIVE}s{$ASLICE}";
109
		$ACOMPLETE_PATH="{$BOOT_DRIVE}s{$ASLICE}a";
110
		$AGLABEL_SLICE="pfsense1";
111
		$AUFS_ID="1";
112
		$AOLD_UFS_ID="0";
113
		$ABOOTFLASH="{$BOOT_DRIVE}s{$AOLDSLICE}";
114
	} else {
115
		$ASLICE="1";
116
		$AOLDSLICE="2";
117
		$ATOFLASH="{$BOOT_DRIVE}s{$ASLICE}";
118
		$ACOMPLETE_PATH="{$BOOT_DRIVE}s{$ASLICE}a";
119
		$AGLABEL_SLICE="pfsense0";
120
		$AUFS_ID="0";
121
		$AOLD_UFS_ID="1";
122
		$ABOOTFLASH="{$BOOT_DRIVE}s{$AOLDSLICE}";
123
	}
124

    
125
	conf_mount_rw();
126
	exec("sysctl kern.geom.debugflags=16");
127
	exec("gpart set -a active -i {$ASLICE} {$BOOT_DRIVE}");
128
	exec("/usr/sbin/boot0cfg -s {$ASLICE} -v /dev/{$BOOT_DRIVE}");
129
	exec("/sbin/tunefs -L ${AGLABEL_SLICE} /dev/$ACOMPLETE_PATH");
130
	exec("/bin/mkdir /tmp/{$AGLABEL_SLICE}");
131
	exec("/sbin/fsck_ufs -y /dev/{$ACOMPLETE_PATH}");
132
	exec("/sbin/mount /dev/ufs/{$AGLABEL_SLICE} /tmp/{$AGLABEL_SLICE}");
133
	$fstab = <<<EOF
134
/dev/ufs/{$AGLABEL_SLICE} / ufs ro 1 1
135
/dev/ufs/cf /cf ufs ro 1 1	
136
EOF;
137
	file_put_contents("/tmp/{$AGLABEL_SLICE}/etc/fstab", $fstab);
138
	exec("/sbin/umount /tmp/{$AGLABEL_SLICE}");
139
	exec("sysctl kern.geom.debugflags=0");
140
	conf_mount_ro();
141
	$savemsg = "The boot slice has been set to {$ABOOT_DRIVE} {$AGLABEL_SLICE}";
142
	// Survey slice info
143
	detect_slice_info();
144
}
145

    
146
$NANOBSD_SIZE = strtoupper(file_get_contents("/etc/nanosize.txt"));
147

    
148
if($_POST['destslice']) {
149

    
150
echo <<<EOF
151
 	<div id="loading">
152
		<img src="/themes/metallic/images/misc/loader.gif">
153
		Duplicating slice.  Please wait, this will take a moment...
154
		<p/>&nbsp;
155
	</div>
156
EOF;
157
	for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
158
	ob_implicit_flush(1);
159
	exec("sysctl kern.geom.debugflags=16");
160
	exec("dd if=/dev/zero of=/dev/{$TOFLASH} bs=1m count=1");
161
	exec("/bin/dd if=/dev/{$BOOTFLASH} of=/dev/{$TOFLASH} bs=64k");
162
	exec("/sbin/tunefs -L {$GLABEL_SLICE} /dev/{$COMPLETE_PATH}");
163
	exec("/bin/mkdir /tmp/{$GLABEL_SLICE}");
164
	exec("/sbin/fsck_ufs -y /dev/{$COMPLETE_PATH}");
165
	exec("/sbin/mount /dev/ufs/{$GLABEL_SLICE} /tmp/{$GLABEL_SLICE}");
166
	exec("/bin/cp /etc/fstab /tmp/{$GLABEL_SLICE}/etc/fstab");
167
	exec("sysctl kern.geom.debugflags=0");
168
	$status = exec("sed -i \"\" \"s/pfsense{$OLD_UFS_ID}/pfsense{$UFS_ID}/g\" /tmp/{$GLABEL_SLICE}/etc/fstab");
169
	if($status) {
170
		exec("/sbin/umount /tmp/{$GLABEL_SLICE}");
171
		$savemsg = "There was an error while duplicating the slice.  Operation aborted.";
172
	} else {
173
		$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.";
174
		exec("/sbin/umount /tmp/{$GLABEL_SLICE}");
175
	}
176
	// Survey slice info
177
	detect_slice_info();
178
}
179

    
180
if ($savemsg)
181
	print_info_box($savemsg)
182

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

    
296
<?php
297

    
298
// Clear the loading indicator
299
echo "<script type=\"text/javascript\">";
300
echo "$('loading').innerHTML = '';";
301
echo "</script>";	
302

    
303
?>
(25-25/176)