summaryrefslogtreecommitdiffstats
path: root/usr/local/sbin
diff options
context:
space:
mode:
authorjim-p <jimp@pfsense.org>2013-05-15 15:37:09 -0400
committerjim-p <jimp@pfsense.org>2013-05-15 15:38:56 -0400
commitebadf1802219f8f45b106cbba73976f56cf44917 (patch)
treeca6c4c510e2f3c2a9fe927a5a5a9059f35ae3176 /usr/local/sbin
parent5996862b2930e2cd7a8ce7940dae8793c2f77cb0 (diff)
downloadpfsense-ebadf1802219f8f45b106cbba73976f56cf44917.zip
pfsense-ebadf1802219f8f45b106cbba73976f56cf44917.tar.gz
Add a script that can automatically convert a user's fstab over to ufs labels. Useful for full installs to avoid issues with device numbering (e.g. ad0 changes to ad4 or da0 to ad2, or in the future case, adX to adaX)
Diffstat (limited to 'usr/local/sbin')
-rw-r--r--usr/local/sbin/ufslabels.sh113
1 files changed, 113 insertions, 0 deletions
diff --git a/usr/local/sbin/ufslabels.sh b/usr/local/sbin/ufslabels.sh
new file mode 100644
index 0000000..4ced54f
--- /dev/null
+++ b/usr/local/sbin/ufslabels.sh
@@ -0,0 +1,113 @@
+#!/bin/sh
+############################################
+#
+# Change fstab to use ufsid and geom labels to avoid relying on device numbers directly.
+#
+############################################
+
+# : cat /etc/fstab
+# # Device Mountpoint FStype Options Dump Pass#
+# /dev/ad0s1a / ufs rw 1 1
+# /dev/ad0s1b none swap sw 0 0
+
+string_length() {
+ unset LEN
+ LEN=`echo -n ${1} | /usr/bin/wc -m | awk '{print $1}'`
+}
+
+get_ufsid() {
+ # $1 = device
+ # : /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'
+ # 51928c99a471c440
+
+ unset UFSID
+
+ ID_PARTS=`/sbin/dumpfs /dev/${1} | /usr/bin/head -n 2 | /usr/bin/tail -n 1 | /usr/bin/cut -f2 -d'[' | /usr/bin/cut -f1 -d ']'`
+ # " 51110eb0 f288b35d " (note it has more spaces than we need/want)
+ ID_PART1=`echo ${ID_PARTS} | awk '{print $1;}'`
+ # "51110eb0"
+ ID_PART2=`echo ${ID_PARTS} | awk '{print $2;}'`
+ # "f288b35d"
+
+ if [ "${ID_PART1}" = "" ] || [ "${ID_PART2}" = "" ]; then
+ echo "Invalid ufsid on ${1} (${ID_PARTS}), cannot continue"
+ exit.
+ fi
+
+ # Safety check to avoid http://www.freebsd.org/cgi/query-pr.cgi?pr=156908
+ string_length ${ID_PART1}
+ if [ ${LEN} -ne 8 ]; then
+ ID_PART1=`printf "%08s" "${ID_PART1}"`
+ fi
+ string_length ${ID_PART2}
+ if [ ${LEN} -ne 8 ]; then
+ ID_PART2=`printf "%08s" "${ID_PART2}"`
+ fi
+ UFSID=${ID_PART1}${ID_PART2}
+}
+
+find_fs_device(){
+ unset DEV
+ DEV=`/usr/bin/grep -e "[[:blank:]]${1}[[:blank:]]" ${FSTAB} | awk '{print $1;}'`
+ DEV=${DEV##/dev/}
+}
+
+FSTAB=/etc/fstab
+cp ${FSTAB} ${FSTAB}.tmp
+
+ALL_FILESYSTEMS=`/bin/cat /etc/fstab | /usr/bin/grep ufs | /usr/bin/awk '{print $2;}'`
+
+for FS in ${ALL_FILESYSTEMS}
+do
+ find_fs_device ${FS}
+ if [ "${DEV}" != "" ]; then
+ get_ufsid ${DEV}
+ string_length ${UFSID}
+ if [ ${LEN} -ne 16 ]; then
+ echo "Invalid UFS ID for FS ${FS} ($UFSID), skipping"
+ else
+ /usr/bin/sed -i'' -e "s/${DEV}/ufsid\/${UFSID}/g" ${FSTAB}.tmp
+ fi
+ else
+ echo "Unable to find device for ${FS}"
+ return -1
+ fi
+ echo "FS: ${FS} on device ${DEV} with ufsid ${UFSID}"
+done
+
+find_fs_device swap
+SWAPDEV=${DEV}
+echo "FS: Swap on device ${SWAPDEV}"
+
+if [ "${SWAPDEV}" != "" ]; then
+ /usr/bin/sed -i'' -e "s/${SWAPDEV}/label\/swap/g" ${FSTAB}.tmp
+fi
+
+echo "===================="
+echo "Current fstab:"
+cat ${FSTAB}
+echo "===================="
+echo "New fstab:"
+cat ${FSTAB}.tmp
+
+if [ ${1} = "commit" ]; then
+ COMMIT=y
+else
+ echo "Commit changes? (y/n):"
+ read COMMIT
+fi
+
+# Commit changes
+if [ "${COMMIT}" = "y" ] || [ "${COMMIT}" = "Y" ]; then
+ echo "Disabling swap to apply label"
+ /sbin/swapoff /dev/${SWAPDEV}
+ echo "Applying label to swap parition"
+ /sbin/glabel label swap /dev/${SWAPDEV}
+
+ echo "Activating new fstab"
+ /bin/mv -f ${FSTAB} ${FSTAB}.old
+ /bin/mv -f ${FSTAB}.tmp ${FSTAB}
+
+ echo "Re-enabling swap"
+ /sbin/swapon -a 2>/dev/null >/dev/null
+fi
OpenPOWER on IntegriCloud