xref: /freebsd/sys/contrib/openzfs/copy-builtin (revision 1f474190)
1#!/usr/bin/env bash
2
3set -e
4
5usage()
6{
7	echo "usage: $0 <kernel source tree>" >&2
8	exit 1
9}
10
11[ "$#" -eq 1 ] || usage
12KERNEL_DIR="$(readlink --canonicalize-existing "$1")"
13
14if ! [ -e 'zfs_config.h' ]
15then
16	echo >&2
17	echo "    $0: you did not run configure, or you're not in the ZFS source directory." >&2
18	echo "    $0: run configure with --with-linux=$KERNEL_DIR and --enable-linux-builtin." >&2
19	echo >&2
20	exit 1
21fi
22
23make clean || true
24make gitrev
25
26rm -rf "$KERNEL_DIR/include/zfs" "$KERNEL_DIR/fs/zfs"
27cp --recursive include "$KERNEL_DIR/include/zfs"
28cp --recursive module "$KERNEL_DIR/fs/zfs"
29cp zfs_config.h "$KERNEL_DIR/include/zfs/"
30
31cat > "$KERNEL_DIR/fs/zfs/Kconfig" <<"EOF"
32config ZFS
33	tristate "ZFS filesystem support"
34	depends on EFI_PARTITION
35	select ZLIB_INFLATE
36	select ZLIB_DEFLATE
37	help
38	  This is the ZFS filesystem from the OpenZFS project.
39
40	  See https://github.com/openzfs/zfs
41
42	  To compile this file system support as a module, choose M here.
43
44	  If unsure, say N.
45EOF
46
47add_after()
48{
49	local FILE="$1"
50	local MARKER="$2"
51	local NEW="$3"
52	local LINE
53
54	while IFS='' read -r LINE
55	do
56		echo "$LINE"
57
58		if [ -n "$MARKER" -a "$LINE" = "$MARKER" ]
59		then
60			echo "$NEW"
61			MARKER=''
62			if IFS='' read -r LINE
63			then
64				[ "$LINE" != "$NEW" ] && echo "$LINE"
65			fi
66		fi
67	done < "$FILE" > "$FILE.new"
68
69	mv "$FILE.new" "$FILE"
70}
71
72add_after "$KERNEL_DIR/fs/Kconfig" 'if BLOCK' 'source "fs/zfs/Kconfig"'
73add_after "$KERNEL_DIR/fs/Makefile" 'endif' 'obj-$(CONFIG_ZFS) += zfs/'
74
75echo >&2
76echo "    $0: done." >&2
77echo "    $0: now you can build the kernel with ZFS support." >&2
78echo "    $0: make sure you enable ZFS support (CONFIG_ZFS) before building." >&2
79echo >&2
80