Project

General

Profile

Download (3.73 KB) Statistics
| Branch: | Tag: | Revision:
1 ebadf180 jim-p
#!/bin/sh
2
############################################
3
#
4
# Change fstab to use ufsid and geom labels to avoid relying on device numbers directly.
5
#
6
############################################
7
8
# : cat /etc/fstab
9
# # Device                Mountpoint      FStype  Options         Dump    Pass#
10
# /dev/ad0s1a             /               ufs     rw              1       1
11
# /dev/ad0s1b             none            swap    sw              0       0
12
13
string_length() {
14
	unset LEN
15 8c392069 Renato Botelho
	LEN=$(echo -n "${1}" | /usr/bin/wc -m)
16 ebadf180 jim-p
}
17
18
get_ufsid() {
19
	# $1 = device
20
	# : /sbin/dumpfs /dev/ad0s1a | /usr/bin/head -n 2 | /usr/bin/tail -n 1 | /usr/bin/cut -f2 -d'[' | /usr/bin/cut -f1 -d ']' | /usr/bin/sed -e 's/[[:blank:]]//g'
21
	# 51928c99a471c440
22
23
	unset UFSID
24 8c392069 Renato Botelho
	local _dev="${1}"
25 ebadf180 jim-p
26 8c392069 Renato Botelho
	if [ -z "${_dev}" ]; then
27
		return
28
	fi
29
30
	ID_PARTS=$(/sbin/dumpfs /dev/${_dev} | \
31
		/usr/bin/head -n 2 | \
32
		/usr/bin/tail -n 1 | \
33
		/usr/bin/cut -f2 -d'[' | \
34
		/usr/bin/cut -f1 -d ']')
35 ebadf180 jim-p
	# " 51110eb0 f288b35d " (note it has more spaces than we need/want)
36 8c392069 Renato Botelho
	ID_PART1=$(echo ${ID_PARTS} | awk '{print $1}')
37 ebadf180 jim-p
	# "51110eb0"
38 8c392069 Renato Botelho
	ID_PART2=$(echo ${ID_PARTS} | awk '{print $2}')
39 ebadf180 jim-p
	# "f288b35d"
40
41 8c392069 Renato Botelho
	if [ -z "${ID_PART1}" -o -z  "${ID_PART2}" ]; then
42 ebadf180 jim-p
		echo "Invalid ufsid on ${1} (${ID_PARTS}), cannot continue"
43 8c392069 Renato Botelho
		exit -1
44 ebadf180 jim-p
	fi
45
46
	# Safety check to avoid http://www.freebsd.org/cgi/query-pr.cgi?pr=156908
47 8c392069 Renato Botelho
	string_length "${ID_PART1}"
48 ebadf180 jim-p
	if [ ${LEN} -ne 8 ]; then
49 8c392069 Renato Botelho
		ID_PART1=$(printf "%08s" "${ID_PART1}")
50 ebadf180 jim-p
	fi
51 8c392069 Renato Botelho
	string_length "${ID_PART2}"
52 ebadf180 jim-p
	if [ ${LEN} -ne 8 ]; then
53 8c392069 Renato Botelho
		ID_PART2=$(printf "%08s" "${ID_PART2}")
54 ebadf180 jim-p
	fi
55 8c392069 Renato Botelho
	UFSID="${ID_PART1}${ID_PART2}"
56 ebadf180 jim-p
}
57
58 8c392069 Renato Botelho
find_fs_device() {
59 ebadf180 jim-p
	unset DEV
60 8c392069 Renato Botelho
	DEV=$(/usr/bin/grep -e "[[:blank:]]*${1}[[:blank:]]" ${FSTAB} | awk '{print $1}')
61 ebadf180 jim-p
	DEV=${DEV##/dev/}
62
}
63
64 04c8360c Renato Botelho
FSTAB=${DESTDIR}/etc/fstab
65 8c392069 Renato Botelho
unset NEED_CHANGES
66 ebadf180 jim-p
cp ${FSTAB} ${FSTAB}.tmp
67
68 04c8360c Renato Botelho
ALL_FILESYSTEMS=$(/usr/bin/awk '/ufs/ && !(/dev\/mirror\// || /dev\/ufsid\// || /dev\/label\// || /dev\/geom\//) {print $2}' ${DESTDIR}/etc/fstab)
69 ebadf180 jim-p
70
for FS in ${ALL_FILESYSTEMS}
71
do
72 8c392069 Renato Botelho
	find_fs_device "${FS}"
73 ebadf180 jim-p
	if [ "${DEV}" != "" ]; then
74
		get_ufsid ${DEV}
75 8c392069 Renato Botelho
		string_length "${UFSID}"
76 ebadf180 jim-p
		if [ ${LEN} -ne 16 ]; then
77
			echo "Invalid UFS ID for FS ${FS} ($UFSID), skipping"
78
		else
79 802956d6 Renato Botelho
			/usr/bin/sed -i '' -e "s/${DEV}/ufsid\/${UFSID}/g" ${FSTAB}.tmp
80 8c392069 Renato Botelho
			NEED_CHANGES=1
81 ebadf180 jim-p
		fi
82
	else
83
		echo "Unable to find device for ${FS}"
84 8c392069 Renato Botelho
		exit -1
85 ebadf180 jim-p
	fi
86
	echo "FS: ${FS} on device ${DEV} with ufsid ${UFSID}"
87
done
88
89 04c8360c Renato Botelho
ALL_SWAPFS=$(/usr/bin/awk '/swap/ && !(/dev\/mirror\// || /dev\/ufsid\// || /dev\/label\// || /dev\/geom\//) {print $1}' ${DESTDIR}/etc/fstab)
90 336e899a jim-p
SWAPNUM=0
91
for SFS in ${ALL_SWAPFS}
92
do
93
	DEV=${SFS##/dev/}
94
	if [ "${DEV}" != "" ]; then
95
		SWAPDEV=${DEV}
96
		echo "FS: Swap slice ${SWAPNUM} on device ${SWAPDEV}"
97
		if [ "${SWAPDEV}" != "" ]; then
98 802956d6 Renato Botelho
			/usr/bin/sed -i '' -e "s/${SWAPDEV}/label\/swap${SWAPNUM}/g" ${FSTAB}.tmp
99 8c392069 Renato Botelho
			NEED_CHANGES=1
100 336e899a jim-p
		fi
101 8c392069 Renato Botelho
		SWAPNUM=$((SWAPNUM+1))
102 336e899a jim-p
	fi
103
done
104 ebadf180 jim-p
105 8c392069 Renato Botelho
if [ -z "${NEED_CHANGES}" ]; then
106 336e899a jim-p
	echo Nothing to do, all filesystems and swap already use some form of device-independent labels
107 8c392069 Renato Botelho
	exit 0
108 ebadf180 jim-p
fi
109
110
echo "===================="
111
echo "Current fstab:"
112
cat ${FSTAB}
113
echo "===================="
114
echo "New fstab:"
115
cat ${FSTAB}.tmp
116
117 735ab503 jim-p
if [ "${1}" = "commit" ]; then
118 ebadf180 jim-p
	COMMIT=y
119
else
120
	echo "Commit changes? (y/n):"
121
	read COMMIT
122
fi
123
124
# Commit changes
125 8c392069 Renato Botelho
if [ "${COMMIT}" = "y" -o "${COMMIT}" = "Y" ]; then
126 336e899a jim-p
127 8c392069 Renato Botelho
	echo "Applying label to swap partition"
128 336e899a jim-p
	SWAPNUM=0
129
	for SFS in ${ALL_SWAPFS}
130
	do
131 8c392069 Renato Botelho
		find_fs_device "${SFS}"
132 336e899a jim-p
		if [ "${DEV}" != "" ]; then
133
			SWAPDEV=${DEV}
134 8c392069 Renato Botelho
			if [ -n "${SWAPDEV}" ]; then
135
				echo "Disabling swap ${SWAPDEV} to apply label"
136
				/sbin/swapoff /dev/${SWAPDEV}
137 336e899a jim-p
				/sbin/glabel label swap${SWAPNUM} /dev/${SWAPDEV}
138
			fi
139 8c392069 Renato Botelho
			SWAPNUM=$((SWAPNUM+1))
140 336e899a jim-p
		fi
141
	done
142 ebadf180 jim-p
143
	echo "Activating new fstab"
144
	/bin/mv -f ${FSTAB} ${FSTAB}.old
145
	/bin/mv -f ${FSTAB}.tmp ${FSTAB}
146
147
	echo "Re-enabling swap"
148
	/sbin/swapon -a 2>/dev/null >/dev/null
149
fi