Project

General

Profile

Download (3.82 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 db4b4576 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 db4b4576 Renato Botelho
	local _dev="${1}"
25 ebadf180 jim-p
26 db4b4576 Renato Botelho
	if [ -z "${_dev}" ]; then
27
		return
28
	fi
29
30 829083b9 Brad Davis
	ID_PARTS=$(/sbin/dumpfs "/dev/${_dev}" | \
31 db4b4576 Renato Botelho
		/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 829083b9 Brad Davis
	ID_PART1=$(echo "${ID_PARTS}" | /usr/bin/awk '{print $1}')
37 ebadf180 jim-p
	# "51110eb0"
38 829083b9 Brad Davis
	ID_PART2=$(echo "${ID_PARTS}" | /usr/bin/awk '{print $2}')
39 ebadf180 jim-p
	# "f288b35d"
40
41 13691f23 Brad Davis
	if [ -z "${ID_PART1}" ] || [ -z "${ID_PART2}" ]; then
42 ebadf180 jim-p
		echo "Invalid ufsid on ${1} (${ID_PARTS}), cannot continue"
43 c4c2ecd8 Brad Davis
		exit 1
44 ebadf180 jim-p
	fi
45
46 8fa5038b Marcos Mendoza
	# Safety check to avoid https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=156908
47 db4b4576 Renato Botelho
	string_length "${ID_PART1}"
48 829083b9 Brad Davis
	if [ "${LEN}" -ne 8 ]; then
49 db4b4576 Renato Botelho
		ID_PART1=$(printf "%08s" "${ID_PART1}")
50 ebadf180 jim-p
	fi
51 db4b4576 Renato Botelho
	string_length "${ID_PART2}"
52 829083b9 Brad Davis
	if [ "${LEN}" -ne 8 ]; then
53 db4b4576 Renato Botelho
		ID_PART2=$(printf "%08s" "${ID_PART2}")
54 ebadf180 jim-p
	fi
55 db4b4576 Renato Botelho
	UFSID="${ID_PART1}${ID_PART2}"
56 ebadf180 jim-p
}
57
58 db4b4576 Renato Botelho
find_fs_device() {
59 ebadf180 jim-p
	unset DEV
60 829083b9 Brad Davis
	DEV=$(/usr/bin/grep -e "[[:blank:]]*${1}[[:blank:]]" "${FSTAB}" | /usr/bin/awk '{print $1}')
61 ebadf180 jim-p
	DEV=${DEV##/dev/}
62
}
63
64 829083b9 Brad Davis
FSTAB="${DESTDIR}/etc/fstab"
65 db4b4576 Renato Botelho
unset NEED_CHANGES
66 829083b9 Brad Davis
cp "${FSTAB}" "${FSTAB}.tmp"
67 ebadf180 jim-p
68 829083b9 Brad Davis
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 db4b4576 Renato Botelho
	find_fs_device "${FS}"
73 ebadf180 jim-p
	if [ "${DEV}" != "" ]; then
74 829083b9 Brad Davis
		get_ufsid "${DEV}"
75 db4b4576 Renato Botelho
		string_length "${UFSID}"
76 829083b9 Brad Davis
		if [ "${LEN}" -ne 16 ]; then
77 ebadf180 jim-p
			echo "Invalid UFS ID for FS ${FS} ($UFSID), skipping"
78
		else
79 829083b9 Brad Davis
			/usr/bin/sed -i '' -e "s/${DEV}/ufsid\/${UFSID}/g" "${FSTAB}.tmp"
80 db4b4576 Renato Botelho
			NEED_CHANGES=1
81 ebadf180 jim-p
		fi
82
	else
83
		echo "Unable to find device for ${FS}"
84 c4c2ecd8 Brad Davis
		exit 1
85 ebadf180 jim-p
	fi
86
	echo "FS: ${FS} on device ${DEV} with ufsid ${UFSID}"
87
done
88
89 829083b9 Brad Davis
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 829083b9 Brad Davis
			/usr/bin/sed -i '' -e "s/${SWAPDEV}/label\/swap${SWAPNUM}/g" "${FSTAB}.tmp"
99 db4b4576 Renato Botelho
			NEED_CHANGES=1
100 336e899a jim-p
		fi
101 db4b4576 Renato Botelho
		SWAPNUM=$((SWAPNUM+1))
102 336e899a jim-p
	fi
103
done
104 ebadf180 jim-p
105 db4b4576 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 db4b4576 Renato Botelho
	exit 0
108 ebadf180 jim-p
fi
109
110
echo "===================="
111
echo "Current fstab:"
112 829083b9 Brad Davis
cat "${FSTAB}"
113 ebadf180 jim-p
echo "===================="
114
echo "New fstab:"
115 829083b9 Brad Davis
cat "${FSTAB}.tmp"
116 ebadf180 jim-p
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 13691f23 Brad Davis
if [ "${COMMIT}" = "y" ] || [ "${COMMIT}" = "Y" ]; then
126 336e899a jim-p
127 db4b4576 Renato Botelho
	echo "Applying label to swap partition"
128 336e899a jim-p
	SWAPNUM=0
129
	for SFS in ${ALL_SWAPFS}
130
	do
131 db4b4576 Renato Botelho
		find_fs_device "${SFS}"
132 336e899a jim-p
		if [ "${DEV}" != "" ]; then
133
			SWAPDEV=${DEV}
134 db4b4576 Renato Botelho
			if [ -n "${SWAPDEV}" ]; then
135
				echo "Disabling swap ${SWAPDEV} to apply label"
136 829083b9 Brad Davis
				/sbin/swapoff "/dev/${SWAPDEV}"
137
				/sbin/glabel label "swap${SWAPNUM}" "/dev/${SWAPDEV}"
138 336e899a jim-p
			fi
139 db4b4576 Renato Botelho
			SWAPNUM=$((SWAPNUM+1))
140 336e899a jim-p
		fi
141
	done
142 ebadf180 jim-p
143
	echo "Activating new fstab"
144 829083b9 Brad Davis
	/bin/mv -f "${FSTAB}" "${FSTAB}.old"
145
	/bin/mv -f "${FSTAB}.tmp" "${FSTAB}"
146 ebadf180 jim-p
147
	echo "Re-enabling swap"
148
	/sbin/swapon -a 2>/dev/null >/dev/null
149
fi