aboutsummaryrefslogtreecommitdiffstats
path: root/main/openrc/modloop.initd
blob: cf46c98df09d080e76ff4192a3c9fc3f9a41d507 (plain)
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
#!/sbin/runscript

# script that will mount image with modules

depend() {
	need dev
	before checkfs fsck hwdrivers modules hwclock
	keyword novserver
}

# read kernel options
init_KOPT() {
	eval set -- $(cat /proc/cmdline 2>/dev/null)
	while [ $# -gt 0 ]; do
	        case "$1" in
                	*=*)    eval "KOPT_${1%%=*}='${1#*=}'" ;;
	                *)      eval "KOPT_$(echo $1 | sed 's: :_:g')=yes" ;;
        	esac
        	shift
	done
}

find_mnt() {
	local dev="$1"
	local fsfile="$2"
	awk "\$ == \"$dev\" {print \$2}\"" "$fsfile" 2>/dev/null
}

# initialies: alpine_dev, alpine_mnt, alpine_fs, alpine_mounted
find_media() {
	init_KOPT
	alpine_mounted=
	alpine_dev=${KOPT_alpine_dev%%:*}
	alpine_fs=${KOPT_alpine_dev#*:}
	[ "$alpine_fs" = "$KOPT_alpine_dev" ] && unset alpine_fs
	# first we check if alpine_dev is mounted and use this
	alpine_mnt=$(find_mnt /dev/$alpine_dev /proc/mounts)
	if [ -z "$alpine_mnt" ]; then
		# then we check fstab
		alpine_mnt=$(find_mnt /dev/$alpine_dev /etc/fstab)
	else
		alpine_mounted=yes
	fi
	# finally we fallback to /media/<devicename>
	[ -z "$alpine_mnt" ] && alpine_mnt=/media/$alpine_dev
}

start() {
	local modloop mount_opts
	find_media
	if [ -z "$alpine_dev" ] ; then
		ebegin "Skipping mount module loopback (specify with alpine_dev)"
		eend 0
		return 0
	fi

        modloop=${KOPT_modloop:-$KOPT_BOOT_IMAGE.cmg}
	[ -n "$alpine_fs" ] && mount_opts="-t $alpine_fs"

	ebegin "Mounting loopback device for kernel modules"
	if [ -z "$alpine_mounted" ]; then
		mount $mount_opts /dev/$alpine_dev $alpine_mnt 2>/dev/null
	fi
	mkdir -p /.modloop /lib

	mount -o loop,ro -t cramfs  $alpine_mnt/$modloop /.modloop &&\
		rm -rf /lib/modules &&\
		ln -sf /.modloop/modules /lib/
	eend $? || return 1

	# copy firmware if there are any
	if [ -d $alpine_mnt/firmware ]; then
	        ebegin "Copying firmware from $alpine_mnt/firmware"
	        cp -R -a $alpine_mnt/firmware /lib/
	        eend $?
	fi
}

stop() {
	local rc=0
	find_media
	[ -z "$alpine_dev" ] && return 0
	ebegin "Unmounting loopback device for kernel modules"
	if mountinfo --quiet /.modloop; then
		umount -d /.modloop || rc=1
	fi
	if mountinfo --quiet $alpine_mnt; then
		umount $alpine_mnt || rc=1
	fi
	eend $rc 
}