Project

General

Profile

« Previous | Next » 

Revision 08fd5444

Added by Jim Pingle over 15 years ago

Relocate some NanoBSD functions to a central place. Change NanoBSD diagnostics and system info widget to use the new code. Ticket #533 and some prerequisite parts of Ticket #534.

View differences:

etc/inc/pfsense-utils.inc
1914 1914
		return -1;
1915 1915
	}
1916 1916
}
1917
function get_real_slice_from_glabel($label) {
1918
	$label = escapeshellarg($label);
1919
	return trim(`/sbin/glabel list | /usr/bin/grep -B2 ufs/{$label} | /usr/bin/head -n 1 | /usr/bin/cut -f3 -d' '`);
1920
}
1921
function nanobsd_get_boot_slice() {
1922
	return trim(`/sbin/mount | /usr/bin/grep pfsense | /usr/bin/cut -d'/' -f4 | /usr/bin/cut -d' ' -f1`);
1923
}
1924
function nanobsd_get_boot_drive() {
1925
	return 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`);
1926
}
1927
function nanobsd_get_active_slice() {
1928
	$boot_drive = nanobsd_get_boot_drive();
1929
	$active = trim(`gpart show $boot_drive | grep '\[active\]' | awk '{print $3;}'`);
1930

  
1931
	return "{$boot_drive}s{$active}";
1932
}
1933
function nanobsd_get_size() {
1934
	return strtoupper(file_get_contents("/etc/nanosize.txt"));
1935
}
1936
function nanobsd_set_boot_slice($slice) {
1937
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
1938
	global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
1939
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
1940
	nanobsd_detect_slice_info();
1941

  
1942
	for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
1943
	ob_implicit_flush(1);
1944
	if(strstr($slice, "s2")) {
1945
		$ASLICE="2";
1946
		$AOLDSLICE="1";
1947
		$AGLABEL_SLICE="pfsense1";
1948
		$AUFS_ID="1";
1949
		$AOLD_UFS_ID="0";
1950
	} else {
1951
		$ASLICE="1";
1952
		$AOLDSLICE="2";
1953
		$AGLABEL_SLICE="pfsense0";
1954
		$AUFS_ID="0";
1955
		$AOLD_UFS_ID="1";
1956
	}
1957
	$ATOFLASH="{$BOOT_DRIVE}s{$ASLICE}";
1958
	$ACOMPLETE_PATH="{$BOOT_DRIVE}s{$ASLICE}a";
1959
	$ABOOTFLASH="{$BOOT_DRIVE}s{$AOLDSLICE}";
1960
	conf_mount_rw();
1961
	exec("sysctl kern.geom.debugflags=16");
1962
	exec("gpart set -a active -i {$ASLICE} {$BOOT_DRIVE}");
1963
	exec("/usr/sbin/boot0cfg -s {$ASLICE} -v /dev/{$BOOT_DRIVE}");
1964
	exec("/sbin/tunefs -L ${AGLABEL_SLICE} /dev/$ACOMPLETE_PATH");
1965
	exec("/bin/mkdir /tmp/{$AGLABEL_SLICE}");
1966
	exec("/sbin/fsck_ufs -y /dev/{$ACOMPLETE_PATH}");
1967
	exec("/sbin/mount /dev/ufs/{$AGLABEL_SLICE} /tmp/{$AGLABEL_SLICE}");
1968
	$fstab = <<<EOF
1969
/dev/ufs/{$AGLABEL_SLICE} / ufs ro 1 1
1970
/dev/ufs/cf /cf ufs ro 1 1
1971
EOF;
1972
	file_put_contents("/tmp/{$AGLABEL_SLICE}/etc/fstab", $fstab);
1973
	exec("/sbin/umount /tmp/{$AGLABEL_SLICE}");
1974
	exec("/sbin/sysctl kern.geom.debugflags=0");
1975
	conf_mount_ro();
1976
}
1977
function nanobsd_clone_slice($dstslice) {
1978
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
1979
	global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
1980
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
1981
	nanobsd_detect_slice_info();
1982

  
1983
	for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
1984
	ob_implicit_flush(1);
1985
	exec("/sbin/sysctl kern.geom.debugflags=16");
1986
	exec("/bin/dd if=/dev/zero of=/dev/{$TOFLASH} bs=1m count=1");
1987
	exec("/bin/dd if=/dev/{$BOOTFLASH} of=/dev/{$TOFLASH} bs=64k");
1988
	exec("/sbin/tunefs -L {$GLABEL_SLICE} /dev/{$COMPLETE_PATH}");
1989
	exec("/bin/mkdir /tmp/{$GLABEL_SLICE}");
1990
	exec("/sbin/fsck_ufs -y /dev/{$COMPLETE_PATH}");
1991
	exec("/sbin/mount /dev/ufs/{$GLABEL_SLICE} /tmp/{$GLABEL_SLICE}");
1992
	exec("/bin/cp /etc/fstab /tmp/{$GLABEL_SLICE}/etc/fstab");
1993
	$status = exec("sed -i \"\" \"s/pfsense{$OLD_UFS_ID}/pfsense{$UFS_ID}/g\" /tmp/{$GLABEL_SLICE}/etc/fstab");
1994
	exec("/sbin/umount /tmp/{$GLABEL_SLICE}");
1995
	exec("/sbin/sysctl kern.geom.debugflags=0");
1996
	if($status) {
1997
		return false;
1998
	} else {
1999
		return true;
2000
	}
2001
}
2002
function nanobsd_detect_slice_info() {
2003
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
2004
	global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
2005
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
2006

  
2007
	$BOOT_DEVICE=nanobsd_get_boot_slice();
2008
	$REAL_BOOT_DEVICE=get_real_slice_from_glabel($BOOT_DEVICE);
2009
	$BOOT_DRIVE=nanobsd_get_boot_drive();
2010
	$ACTIVE_SLICE=nanobsd_get_active_slice();
2011

  
2012
	// Detect which slice is active and set information.
2013
	if(strstr($REAL_BOOT_DEVICE, "s1")) {
2014
		$SLICE="2";
2015
		$OLDSLICE="1";
2016
		$GLABEL_SLICE="pfsense1";
2017
		$UFS_ID="1";
2018
		$OLD_UFS_ID="0";
1917 2019

  
2020
	} else {
2021
		$SLICE="1";
2022
		$OLDSLICE="2";
2023
		$GLABEL_SLICE="pfsense0";
2024
		$UFS_ID="0";
2025
		$OLD_UFS_ID="1";
2026
	}
2027
	$TOFLASH="{$BOOT_DRIVE}s{$SLICE}";
2028
	$COMPLETE_PATH="{$BOOT_DRIVE}s{$SLICE}a";
2029
	$COMPLETE_BOOT_PATH="{$BOOT_DRIVE}s{$OLDSLICE}";
2030
	$BOOTFLASH="{$BOOT_DRIVE}s{$OLDSLICE}";
2031
}
1918 2032
?>
usr/local/www/diag_nanobsd.php
49 49
$pgtitle = array("Diagnostics","NanoBSD");
50 50
include("head.inc");
51 51

  
52
function detect_slice_info() {
53
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
54
	global $GLABEL_SLIZE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
55
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE;
56

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

  
61
	// Detect which slice is active and set information.
62
	if(strstr($REAL_BOOT_DEVICE, "s1")) {
63
		$SLICE="2";
64
		$OLDSLICE="1";
65
		$TOFLASH="{$BOOT_DRIVE}s{$SLICE}";
66
		$COMPLETE_PATH="{$BOOT_DRIVE}s{$SLICE}a";
67
		$COMPLETE_BOOT_PATH="{$BOOT_DRIVE}s{$OLDSLICE}";	
68
		$GLABEL_SLICE="pfsense1";
69
		$UFS_ID="1";
70
		$OLD_UFS_ID="0";
71
		$BOOTFLASH="{$BOOT_DRIVE}s{$OLDSLICE}";
72

  
73
	} else {
74
		$SLICE="1";
75
		$OLDSLICE="2";
76
		$TOFLASH="{$BOOT_DRIVE}s{$SLICE}";
77
		$COMPLETE_PATH="{$BOOT_DRIVE}s{$SLICE}a";
78
		$COMPLETE_BOOT_PATH="{$BOOT_DRIVE}s{$OLDSLICE}";
79
		$GLABEL_SLICE="pfsense0";
80
		$UFS_ID="0";
81
		$OLD_UFS_ID="1";
82
		$BOOTFLASH="{$BOOT_DRIVE}s{$OLDSLICE}";
83
	}
84
}
85

  
86 52
// Survey slice info
87
detect_slice_info();
53
global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
54
global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
55
global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
56
nanobsd_detect_slice_info();
88 57

  
89 58
?>
90 59

  
......
95 64

  
96 65
<?php
97 66

  
98
$NANOBSD_SIZE = strtoupper(file_get_contents("/etc/nanosize.txt"));
67
$NANOBSD_SIZE = nanobsd_get_size();
99 68

  
100 69
if($_POST['bootslice']) {
101 70
	echo <<<EOF
......
105 74
			<p/>&nbsp;
106 75
		</div>
107 76
EOF;
108
	for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
109
	ob_implicit_flush(1);
110
	if(strstr($_POST['bootslice'], "s2")) {
111
		$ASLICE="2";
112
		$AOLDSLICE="1";
113
		$ATOFLASH="{$BOOT_DRIVE}s{$ASLICE}";
114
		$ACOMPLETE_PATH="{$BOOT_DRIVE}s{$ASLICE}a";
115
		$AGLABEL_SLICE="pfsense1";
116
		$AUFS_ID="1";
117
		$AOLD_UFS_ID="0";
118
		$ABOOTFLASH="{$BOOT_DRIVE}s{$AOLDSLICE}";
119
	} else {
120
		$ASLICE="1";
121
		$AOLDSLICE="2";
122
		$ATOFLASH="{$BOOT_DRIVE}s{$ASLICE}";
123
		$ACOMPLETE_PATH="{$BOOT_DRIVE}s{$ASLICE}a";
124
		$AGLABEL_SLICE="pfsense0";
125
		$AUFS_ID="0";
126
		$AOLD_UFS_ID="1";
127
		$ABOOTFLASH="{$BOOT_DRIVE}s{$AOLDSLICE}";
128
	}
129
	conf_mount_rw();
130
	exec("sysctl kern.geom.debugflags=16");	
131
	exec("gpart set -a active -i {$ASLICE} {$BOOT_DRIVE}");
132
	exec("/usr/sbin/boot0cfg -s {$ASLICE} -v /dev/{$BOOT_DRIVE}");
133
	exec("/sbin/tunefs -L ${AGLABEL_SLICE} /dev/$ACOMPLETE_PATH");
134
	exec("/bin/mkdir /tmp/{$AGLABEL_SLICE}");
135
	exec("/sbin/fsck_ufs -y /dev/{$ACOMPLETE_PATH}");
136
	exec("/sbin/mount /dev/ufs/{$AGLABEL_SLICE} /tmp/{$AGLABEL_SLICE}");
137
	$fstab = <<<EOF
138
/dev/ufs/{$AGLABEL_SLICE} / ufs ro 1 1
139
/dev/ufs/cf /cf ufs ro 1 1	
140
EOF;
141
	file_put_contents("/tmp/{$AGLABEL_SLICE}/etc/fstab", $fstab);
142
	exec("/sbin/umount /tmp/{$AGLABEL_SLICE}");
143
	exec("/sbin/sysctl kern.geom.debugflags=0");
144
	conf_mount_ro();
145
	$savemsg = "The boot slice has been set to {$BOOT_DRIVE} {$AGLABEL_SLICE}";
77
	nanobsd_set_boot_slice($_POST['bootslice']);
78
	$savemsg = "The boot slice has been set to " . nanobsd_get_active_slice();
146 79
	// Survey slice info
147
	detect_slice_info();
80
	nanobsd_detect_slice_info();
148 81

  
149 82
}
150 83

  
151 84
if($_POST['destslice']) {
152

  
153 85
echo <<<EOF
154 86
 	<div id="loading">
155 87
		<img src="/themes/metallic/images/misc/loader.gif">
......
157 89
		<p/>&nbsp;
158 90
	</div>
159 91
EOF;
160
	for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
161
	ob_implicit_flush(1);
162
	exec("/sbin/sysctl kern.geom.debugflags=16");
163
	exec("/bin/dd if=/dev/zero of=/dev/{$TOFLASH} bs=1m count=1");
164
	exec("/bin/dd if=/dev/{$BOOTFLASH} of=/dev/{$TOFLASH} bs=64k");
165
	exec("/sbin/tunefs -L {$GLABEL_SLICE} /dev/{$COMPLETE_PATH}");
166
	exec("/bin/mkdir /tmp/{$GLABEL_SLICE}");
167
	exec("/sbin/fsck_ufs -y /dev/{$COMPLETE_PATH}");
168
	exec("/sbin/mount /dev/ufs/{$GLABEL_SLICE} /tmp/{$GLABEL_SLICE}");
169
	exec("/bin/cp /etc/fstab /tmp/{$GLABEL_SLICE}/etc/fstab");
170
	$status = exec("sed -i \"\" \"s/pfsense{$OLD_UFS_ID}/pfsense{$UFS_ID}/g\" /tmp/{$GLABEL_SLICE}/etc/fstab");
171
	if($status) {
172
		exec("/sbin/umount /tmp/{$GLABEL_SLICE}");
173
		$savemsg = "There was an error while duplicating the slice.  Operation aborted.";
174
	} else {
92
	if(nanobsd_clone_slice($_POST['destslice'])) {
175 93
		$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.";
176
		exec("/sbin/umount /tmp/{$GLABEL_SLICE}");
94
	} else {
95
		$savemsg = "There was an error while duplicating the slice.  Operation aborted.";
177 96
	}
178
	exec("/sbin/sysctl kern.geom.debugflags=0");
179 97
	// Re-Survey slice info
180
	detect_slice_info();
98
	nanobsd_detect_slice_info();
181 99
}
182 100

  
183 101
if ($savemsg)
......
217 135
							<form action="diag_nanobsd.php" method="post" name="iform">
218 136
								Bootup slice:
219 137
								<select name='bootslice'>
220
									<option value='<?php echo $BOOTFLASH; ?>'>
138
									<option value='<?php echo $BOOTFLASH; ?>'<?php if ($BOOTFLASH == $ACTIVE_SLICE) {?> selected<?php } ?>>
221 139
										<?php echo $BOOTFLASH; ?>
222 140
									</option>
223
									<option value='<?php echo $TOFLASH; ?>'>
224
										<?php echo "{$TOFLASH}"; ?>
141
									<option value='<?php echo $TOFLASH; ?>'<?php if ($TOFLASH == $ACTIVE_SLICE) {?> selected<?php } ?>>
142
										<?php echo $TOFLASH; ?>
225 143
									</option>
226 144
								</select>
227 145
								<br/>
usr/local/www/widgets/widgets/system_information.widget.php
95 95
		<?php endif; ?>
96 96
		<?php if ($g['platform'] == "nanobsd"): ?>
97 97
			<?
98
			$BOOT_DEVICE=trim(`/sbin/mount | /usr/bin/grep pfsense | /usr/bin/cut -d'/' -f4 | /usr/bin/cut -d' ' -f1`);
99
			$REAL_BOOT_DEVICE=trim(`/sbin/glabel list | /usr/bin/grep -B2 ufs/{$BOOT_DEVICE} | /usr/bin/head -n 1 | /usr/bin/cut -f3 -d' '`);
98
			global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
99
			global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
100
			global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
101
			nanobsd_detect_slice_info();
100 102
			?>
101 103
		<tr>
102 104
			<td width="25%" class="vncellt">NanoBSD Boot Slice</td>
103
			<td width="75%" class="listr"><?=htmlspecialchars($BOOT_DEVICE);?> / <?=htmlspecialchars($REAL_BOOT_DEVICE);?></td>
105
			<td width="75%" class="listr">
106
				<?=htmlspecialchars($BOOT_DEVICE);?> / <?=htmlspecialchars($BOOTFLASH);?>
107
				<?php if ($BOOTFLASH != $ACTIVE_SLICE): ?>
108
				<br/><br/>Next Boot:<br/>
109
				<?=htmlspecialchars($GLABEL_SLICE);?> / <?=htmlspecialchars($ACTIVE_SLICE);?>
110
				<?php endif; ?>
111
			</td>
104 112
		</tr>
105 113
		<?php endif; ?>
106 114
		<tr>

Also available in: Unified diff