xref: /freebsd/sys/contrib/openzfs/scripts/zfs-helpers.sh (revision eda14cbc264d6969b02f2b1994cef11148e914f1)
1*eda14cbcSMatt Macy#!/bin/sh
2*eda14cbcSMatt Macy#
3*eda14cbcSMatt Macy# This script is designed to facilitate in-tree development and testing
4*eda14cbcSMatt Macy# by installing symlinks on your system which refer to in-tree helper
5*eda14cbcSMatt Macy# utilities.  These helper utilities must be installed to in order to
6*eda14cbcSMatt Macy# exercise all ZFS functionality.  By using symbolic links and keeping
7*eda14cbcSMatt Macy# the scripts in-tree during development they can be easily modified
8*eda14cbcSMatt Macy# and those changes tracked.
9*eda14cbcSMatt Macy#
10*eda14cbcSMatt Macy# Use the following configuration option to override the installation
11*eda14cbcSMatt Macy# paths for these scripts.  The correct path is automatically set for
12*eda14cbcSMatt Macy# most distributions but you can optionally set it for your environment.
13*eda14cbcSMatt Macy#
14*eda14cbcSMatt Macy#   --with-mounthelperdir=DIR  install mount.zfs in dir [/sbin]
15*eda14cbcSMatt Macy#   --with-udevdir=DIR         install udev helpers [default=check]
16*eda14cbcSMatt Macy#   --with-udevruledir=DIR     install udev rules [default=UDEVDIR/rules.d]
17*eda14cbcSMatt Macy#   --sysconfdir=DIR           install zfs configuration files [PREFIX/etc]
18*eda14cbcSMatt Macy#
19*eda14cbcSMatt Macy
20*eda14cbcSMatt MacyBASE_DIR=$(dirname "$0")
21*eda14cbcSMatt MacySCRIPT_COMMON=common.sh
22*eda14cbcSMatt Macyif [ -f "${BASE_DIR}/${SCRIPT_COMMON}" ]; then
23*eda14cbcSMatt Macy	. "${BASE_DIR}/${SCRIPT_COMMON}"
24*eda14cbcSMatt Macyelse
25*eda14cbcSMatt Macy	echo "Missing helper script ${SCRIPT_COMMON}" && exit 1
26*eda14cbcSMatt Macyfi
27*eda14cbcSMatt Macy
28*eda14cbcSMatt MacyPROG=zfs-helpers.sh
29*eda14cbcSMatt MacyDRYRUN="no"
30*eda14cbcSMatt MacyINSTALL="no"
31*eda14cbcSMatt MacyREMOVE="no"
32*eda14cbcSMatt MacyVERBOSE="no"
33*eda14cbcSMatt Macy
34*eda14cbcSMatt Macyfail() {
35*eda14cbcSMatt Macy	echo "${PROG}: $1" >&2
36*eda14cbcSMatt Macy	exit 1
37*eda14cbcSMatt Macy}
38*eda14cbcSMatt Macy
39*eda14cbcSMatt Macymsg() {
40*eda14cbcSMatt Macy	if [ "$VERBOSE" = "yes" ]; then
41*eda14cbcSMatt Macy		echo "$@"
42*eda14cbcSMatt Macy	fi
43*eda14cbcSMatt Macy}
44*eda14cbcSMatt Macy
45*eda14cbcSMatt Macyusage() {
46*eda14cbcSMatt Macycat << EOF
47*eda14cbcSMatt MacyUSAGE:
48*eda14cbcSMatt Macy$0 [dhirv]
49*eda14cbcSMatt Macy
50*eda14cbcSMatt MacyDESCRIPTION:
51*eda14cbcSMatt Macy	Install/remove the ZFS helper utilities.
52*eda14cbcSMatt Macy
53*eda14cbcSMatt MacyOPTIONS:
54*eda14cbcSMatt Macy	-d      Dry run
55*eda14cbcSMatt Macy	-h      Show this message
56*eda14cbcSMatt Macy	-i      Install the helper utilities
57*eda14cbcSMatt Macy	-r      Remove the helper utilities
58*eda14cbcSMatt Macy	-v      Verbose
59*eda14cbcSMatt Macy
60*eda14cbcSMatt Macy$0 -iv
61*eda14cbcSMatt Macy$0 -r
62*eda14cbcSMatt Macy
63*eda14cbcSMatt MacyEOF
64*eda14cbcSMatt Macy}
65*eda14cbcSMatt Macy
66*eda14cbcSMatt Macywhile getopts 'hdirv' OPTION; do
67*eda14cbcSMatt Macy	case $OPTION in
68*eda14cbcSMatt Macy	h)
69*eda14cbcSMatt Macy		usage
70*eda14cbcSMatt Macy		exit 1
71*eda14cbcSMatt Macy		;;
72*eda14cbcSMatt Macy	d)
73*eda14cbcSMatt Macy		DRYRUN="yes"
74*eda14cbcSMatt Macy		;;
75*eda14cbcSMatt Macy	i)
76*eda14cbcSMatt Macy		INSTALL="yes"
77*eda14cbcSMatt Macy		;;
78*eda14cbcSMatt Macy	r)
79*eda14cbcSMatt Macy		REMOVE="yes"
80*eda14cbcSMatt Macy		;;
81*eda14cbcSMatt Macy	v)
82*eda14cbcSMatt Macy		VERBOSE="yes"
83*eda14cbcSMatt Macy		;;
84*eda14cbcSMatt Macy	?)
85*eda14cbcSMatt Macy		usage
86*eda14cbcSMatt Macy		exit
87*eda14cbcSMatt Macy		;;
88*eda14cbcSMatt Macy	esac
89*eda14cbcSMatt Macydone
90*eda14cbcSMatt Macy
91*eda14cbcSMatt Macyif [ "$INSTALL" = "yes" ] && [ "$REMOVE" = "yes" ]; then
92*eda14cbcSMatt Macy	fail "Specify -i or -r but not both"
93*eda14cbcSMatt Macyfi
94*eda14cbcSMatt Macy
95*eda14cbcSMatt Macyif [ "$INSTALL" = "no" ] && [ "$REMOVE" = "no" ]; then
96*eda14cbcSMatt Macy	fail "Either -i or -r must be specified"
97*eda14cbcSMatt Macyfi
98*eda14cbcSMatt Macy
99*eda14cbcSMatt Macyif [ "$(id -u)" != "0" ]; then
100*eda14cbcSMatt Macy	fail "Must run as root"
101*eda14cbcSMatt Macyfi
102*eda14cbcSMatt Macy
103*eda14cbcSMatt Macyif [ "$INTREE" != "yes" ]; then
104*eda14cbcSMatt Macy	fail "Must be run in-tree"
105*eda14cbcSMatt Macyfi
106*eda14cbcSMatt Macy
107*eda14cbcSMatt Macyif [ "$VERBOSE" = "yes" ]; then
108*eda14cbcSMatt Macy	echo "--- Configuration ---"
109*eda14cbcSMatt Macy	echo "udevdir:          $INSTALL_UDEV_DIR"
110*eda14cbcSMatt Macy	echo "udevruledir:      $INSTALL_UDEV_RULE_DIR"
111*eda14cbcSMatt Macy	echo "mounthelperdir:   $INSTALL_MOUNT_HELPER_DIR"
112*eda14cbcSMatt Macy	echo "sysconfdir:       $INSTALL_SYSCONF_DIR"
113*eda14cbcSMatt Macy	echo "pythonsitedir:    $INSTALL_PYTHON_DIR"
114*eda14cbcSMatt Macy	echo "dryrun:           $DRYRUN"
115*eda14cbcSMatt Macy	echo
116*eda14cbcSMatt Macyfi
117*eda14cbcSMatt Macy
118*eda14cbcSMatt Macyinstall() {
119*eda14cbcSMatt Macy	src=$1
120*eda14cbcSMatt Macy	dst=$2
121*eda14cbcSMatt Macy
122*eda14cbcSMatt Macy	if [ -h "$dst" ]; then
123*eda14cbcSMatt Macy		echo "Symlink exists: $dst"
124*eda14cbcSMatt Macy	elif [ -e "$dst" ]; then
125*eda14cbcSMatt Macy		echo "File exists: $dst"
126*eda14cbcSMatt Macy	elif [ ! -e "$src" ]; then
127*eda14cbcSMatt Macy		echo "Source missing: $src"
128*eda14cbcSMatt Macy	else
129*eda14cbcSMatt Macy		msg "ln -s $src $dst"
130*eda14cbcSMatt Macy
131*eda14cbcSMatt Macy		if [ "$DRYRUN" = "no" ]; then
132*eda14cbcSMatt Macy			DIR=$(dirname "$dst")
133*eda14cbcSMatt Macy			mkdir -p "$DIR" >/dev/null 2>&1
134*eda14cbcSMatt Macy			ln -s "$src" "$dst"
135*eda14cbcSMatt Macy		fi
136*eda14cbcSMatt Macy	fi
137*eda14cbcSMatt Macy}
138*eda14cbcSMatt Macy
139*eda14cbcSMatt Macyremove() {
140*eda14cbcSMatt Macy	dst=$1
141*eda14cbcSMatt Macy
142*eda14cbcSMatt Macy	if [ -h "$dst" ]; then
143*eda14cbcSMatt Macy		msg "rm $dst"
144*eda14cbcSMatt Macy		rm "$dst"
145*eda14cbcSMatt Macy		DIR=$(dirname "$dst")
146*eda14cbcSMatt Macy		rmdir "$DIR" >/dev/null 2>&1
147*eda14cbcSMatt Macy	elif [ -e "$dst" ]; then
148*eda14cbcSMatt Macy		echo "Expected symlink: $dst"
149*eda14cbcSMatt Macy	fi
150*eda14cbcSMatt Macy}
151*eda14cbcSMatt Macy
152*eda14cbcSMatt Macyif [ "${INSTALL}" = "yes" ]; then
153*eda14cbcSMatt Macy	install "$CMD_DIR/mount_zfs/mount.zfs" \
154*eda14cbcSMatt Macy	    "$INSTALL_MOUNT_HELPER_DIR/mount.zfs"
155*eda14cbcSMatt Macy	install "$CMD_DIR/fsck_zfs/fsck.zfs" \
156*eda14cbcSMatt Macy	    "$INSTALL_MOUNT_HELPER_DIR/fsck.zfs"
157*eda14cbcSMatt Macy	install "$CMD_DIR/zvol_id/zvol_id" \
158*eda14cbcSMatt Macy	    "$INSTALL_UDEV_DIR/zvol_id"
159*eda14cbcSMatt Macy	install "$CMD_DIR/vdev_id/vdev_id" \
160*eda14cbcSMatt Macy	    "$INSTALL_UDEV_DIR/vdev_id"
161*eda14cbcSMatt Macy	install "$UDEV_RULE_DIR/60-zvol.rules" \
162*eda14cbcSMatt Macy	    "$INSTALL_UDEV_RULE_DIR/60-zvol.rules"
163*eda14cbcSMatt Macy	install "$UDEV_RULE_DIR/69-vdev.rules" \
164*eda14cbcSMatt Macy	    "$INSTALL_UDEV_RULE_DIR/69-vdev.rules"
165*eda14cbcSMatt Macy	install "$UDEV_RULE_DIR/90-zfs.rules" \
166*eda14cbcSMatt Macy	    "$INSTALL_UDEV_RULE_DIR/90-zfs.rules"
167*eda14cbcSMatt Macy	install "$CMD_DIR/zpool/zpool.d" \
168*eda14cbcSMatt Macy	    "$INSTALL_SYSCONF_DIR/zfs/zpool.d"
169*eda14cbcSMatt Macy	install "$CONTRIB_DIR/pyzfs/libzfs_core" \
170*eda14cbcSMatt Macy	    "$INSTALL_PYTHON_DIR/libzfs_core"
171*eda14cbcSMatt Macy	# Ideally we would install these in the configured ${libdir}, which is
172*eda14cbcSMatt Macy	# by default "/usr/local/lib and unfortunately not included in the
173*eda14cbcSMatt Macy	# dynamic linker search path.
174*eda14cbcSMatt Macy	install "$(find "$LIB_DIR/libzfs_core" -type f -name 'libzfs_core.so*')" \
175*eda14cbcSMatt Macy	    "/lib/libzfs_core.so"
176*eda14cbcSMatt Macy	install "$(find "$LIB_DIR/libnvpair" -type f -name 'libnvpair.so*')" \
177*eda14cbcSMatt Macy	    "/lib/libnvpair.so"
178*eda14cbcSMatt Macy	ldconfig
179*eda14cbcSMatt Macyelse
180*eda14cbcSMatt Macy	remove "$INSTALL_MOUNT_HELPER_DIR/mount.zfs"
181*eda14cbcSMatt Macy	remove "$INSTALL_MOUNT_HELPER_DIR/fsck.zfs"
182*eda14cbcSMatt Macy	remove "$INSTALL_UDEV_DIR/zvol_id"
183*eda14cbcSMatt Macy	remove "$INSTALL_UDEV_DIR/vdev_id"
184*eda14cbcSMatt Macy	remove "$INSTALL_UDEV_RULE_DIR/60-zvol.rules"
185*eda14cbcSMatt Macy	remove "$INSTALL_UDEV_RULE_DIR/69-vdev.rules"
186*eda14cbcSMatt Macy	remove "$INSTALL_UDEV_RULE_DIR/90-zfs.rules"
187*eda14cbcSMatt Macy	remove "$INSTALL_SYSCONF_DIR/zfs/zpool.d"
188*eda14cbcSMatt Macy	remove "$INSTALL_PYTHON_DIR/libzfs_core"
189*eda14cbcSMatt Macy	remove "/lib/libzfs_core.so"
190*eda14cbcSMatt Macy	remove "/lib/libnvpair.so"
191*eda14cbcSMatt Macy	ldconfig
192*eda14cbcSMatt Macyfi
193*eda14cbcSMatt Macy
194*eda14cbcSMatt Macyexit 0
195