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