summaryrefslogtreecommitdiffstats
path: root/remote/modules/nvidia_kernel/nvidia_kernel.build
blob: bf9c6ee6ef7d1bf7e217cbc5c36713fbaf69fec0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
fetch_source() {
	mkdir -p src 2>/dev/null
	cd src || perror "Could not change into src directory."
	download "$REQUIRED_URL"
}

build() {
	local KERNELSRCDIR="$MODULE_DIR/../kernel/ksrc"	# kernel sources
	local TEMPDIR="$MODULE_DIR/temp"			
	local ROOTLOWERDIR="/"
	local ROOTUPPERDIR="$TEMPDIR/rootupper"
	local ROOTBINDDIR="$TEMPDIR/rootbind"
	local ROOTMOUNTDIR="$TEMPDIR/rootmount"
	local BINDMOUNTS="/dev /proc /run /sys"
	local NVIDIA="$MODULE_DIR/src/$REQUIRED_NVIDIA"
	local NVIDIAEXTRACTDIR="$ROOTMOUNTDIR/NVIDIA"
	local NVEXTRACTDIR="/NVIDIA"				# This is relative to the chroot.

	make_dirs () {
		[ -d "$TEMPDIR" ] && rm -rf $TEMPDIR/*
		mkdir -p "$TEMPDIR"			|| perror "Could not create base directory for mount directories $TEMPDIR."
		for DIR in "$ROOTUPPERDIR" "$ROOTBINDDIR" "$ROOTMOUNTDIR"; do
			mkdir -p "$DIR"			|| perror "Could not create directory for mount directory $DIR."
		done
	}

	mount_dirs () {
		pinfo "Executing bind- and overlay mounts ..."
		mount -o bind "$ROOTLOWERDIR" "$ROOTBINDDIR"	|| perror "Could not mount (bind) $ROOTLOWERDIR to $ROOTBINDDIR."
		mount -o remount,ro "$ROOTBINDDIR"		|| perror "Could not remount $ROOTBINDDIR ro read-only."
		mount -t overlayfs overlayfs -o lowerdir="$ROOTBINDDIR",upperdir="$ROOTUPPERDIR" "$ROOTMOUNTDIR"  \
			|| perror "Could not mount (overlayfs) $ROOTLOWERDIR, $ROOTUPPERDIR to $BINDDIR."
		pinfo "Executing bind mounts ($BINDMOUNTS) for chroot root dir ..."
		for MOUNT in $BINDMOUNTS; do
        		mount -o bind "$MOUNT" "$ROOTMOUNTDIR/$MOUNT"	|| perror "Could not mount (bind) $MOUNTS into chroot root dir."
		done
	}

	# We inject a bashrc to be executed later within the chroot.
	gen_bashrc () {
		local COMMON_OPTIONS=' --no-nouveau-check --no-network --no-backup --no-rpms --no-runlevel-check --no-distro-scripts --no-cc-version-check --no-x-check --no-precompiled-interface --silent '

		cat >"$ROOTMOUNTDIR/$HOME/.bashrc"<<-EOF
		echo "chroot successful."
		alias ll='ls -alF'				# A little convenience for debugging purposes.
		PS1='\[\e[1;33m\]chroot@\h:\w\$ \[\e[1;32m\]'	# To recognize the chroot instantly when debugging (yellow on black).
		cd "$NVEXTRACTDIR"
		./nvidia-installer $COMMON_OPTIONS --kernel-source-path /"$KERNELSRCDIR"	# Do the work!
		exit						# Out-comment this for debugging: Then script stays in chroot.
EOF
	}

	unpack_nvidia () {
        	[ -d "$NVIDIAEXTRACTDIR" ] && rm -rf "$NVIDIAEXTRACTDIR"
	        pinfo "Unpacking NVidia archive ($NVIDIA) ..."
        	sh "$NVIDIA" --extract-only --target "$NVIDIAEXTRACTDIR" ||	perror "Could not extract $NVIDIA to $NVIDIAEXTRACTDIR."
	}

	umount_dirs () {
		# Let's tidy the place, or at least the mounts: Otherwise these would stack up, and we do not like that, don't we.
	        for MOUNT in $BINDMOUNTS; do
        	        umount "$ROOTMOUNTDIR/$MOUNT"	|| pwarning "Could not unmount $ROOTMOUNTDIR/$MOUNT!"
	        done
        	umount "$ROOTMOUNTDIR"			|| pwarning "Could not unmount $ROOTMOUNTDIR!"
	        umount "$ROOTBINDDIR"			|| pwarning "Could not unmount $ROOTBINDDIR!"
	}

	strip_modules () {
		strip -g "$ROOTUPPERDIR/$NVEXTRACTDIR/kernel/nvidia.ko"		|| \
			pwarning "Could not strip kernel module $ROOTUPPERDIR/$NVEXTRACTDIR/kernel/nvidia.ko."
		strip -g "$ROOTUPPERDIR/$NVEXTRACTDIR/kernel/uvm/nvidia-uvm.ko"	|| \
			pwarning "Could not strip kernel module $ROOTUPPERDIR/$NVEXTRACTDIR/kernel/uvm/nvidia-uvm.ko."
	}

	copy_modules () {
		local NVIDIA_MODULES="$MODULE_DIR/build/lib/modules/nvidia/"
		mkdir -p "$NVIDIA_MODULES"
		cp "$ROOTUPPERDIR/$NVEXTRACTDIR/kernel/nvidia.ko" "$NVIDIA_MODULES"		|| perror "Could not copy nvidia.ko!"
		cp "$ROOTUPPERDIR/$NVEXTRACTDIR/kernel/uvm/nvidia-uvm.ko" "$NVIDIA_MODULES"	|| perror "Could not copy nvidia-uvm.ko!"
	}

	clean_temp () {
		rm -rf "$TEMPDIR" || perror "Could not clean/delete temp directory $TEMPDIR."
	}

	# Main stuff
	pinfo "Generating temporary directories ..."
	make_dirs
	pinfo "Mounting directories ..."
	mount_dirs

	pinfo "Injecting .bashrc into later chroot ..."
	gen_bashrc

	pinfo "Unpacking NVidia-Installer ..."
	unpack_nvidia

	pinfo "Ready to chroot - compiling may take some time."
	pdebug "--- chroot ---------------------------------------------------------------------"
	pdebug "-                                                                              -"
	pdebug "-   Notice: This may take a while!                                             -"
	pdebug "-                                                                              -"
	pdebug "-      Please keep note the Nvidia installer _will_ complain about             -"
	pdebug "-      several warnings and errors. It will do this in any case.               -"
	pdebug "-                                                                              -"
	pdebug "-      This does _not_ mean the kernel module compilation was unsuccessful!    -"
	pdebug "-                                                                              -"
	pdebug "--------------------------------------------------------------------------------"
	chroot "$ROOTMOUNTDIR"
	pinfo "chroot terminated."

	pinfo "Unmount directories."
	umount_dirs

	pinfo "Stripping kernel modules."
	strip_modules

	pinfo "Copying kernel modules."
	copy_modules

	pinfo "Cleaning / deleting temp directories."
	clean_temp
}

post_copy() {
	:
}