aboutsummarylogtreecommitdiffstats
path: root/overlayroot-runtime.hooks
blob: b8728e5ca680aa7e06c066ec32de6ac741bb1006 (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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/sh

# Copyright 2022 Tim Hildering

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

MSG_MODIFIED="original line modified by overlayroot"
MSG_DEACTIVATED="line deactivated by overlayroot"
OVLROOT_FS_ROONLY="msdos,fat,vfat,iso9660,udf,ntfs-3g,ntfs3"
OVLROOT_FS_RAMONLY="tmpfs,devpts,devtmpfs,tracefs,debugfs,\
configfs,securityfs,proc,sysfs,swap,ramfs"
OVLROOT_CFGFILE="/etc/overlayroot.conf"
OVLROOT_FSTAB="/tmp/overlayroot.fstab"
OVLROOT_MAINDIR="/.overlay"
OVLROOT_LOWERDIR_NAME="ro"
OVLROOT_UPPERDIR_NAME="rw"
OVLROOT_WORKDIR_NAME="work"
FSTAB_SYSTEM="/etc/fstab"
ROOTMNT="/new_root"

add_comments() {
	while [ ${#} -gt 0 ]
	do
   		 echo "# ${1}"; shift 1
	done
}

list_contains() {
	local list="${1}" item="${2}"

	for _item in ${list}
	do
		[ "${_item}" = "${item}" ] && return 0	
	done

	return 1
}

fsopts_add_ro() {
	local fsopts=${1}, fsopts_ro="ro"
	local opt=""

	for opt in ${fsopts//,/ }
	do
		case "${opt}" in
			none | rw)
				;;
			ro)
				fsopts_ro="${fsopts}"
				break
				;;
			*)
				fsopts_ro="${fsopts_ro},${opt}"
				;;
		esac
	done

	printf "%s" "${fsopts_ro}"
}

run_latehook() {
	local line="" options="" noswap="" mnts_overlay="" mnts_rdonly=""
	local ovlroot_lowerdir="" ovlroot_upperdir="" ovlroot_workdir=""
	local fsopts_root="ro"

	[ ${#overlayroot} -eq 0 ] && return

	for opt in ${overlayroot//:/ }
	do
		case "${opt}" in
			tmpfs=*)
				mnts_overlay="${opt#tmpfs=}"
				;;
			ro=*)
				mnts_rdonly="${opt#ro=}"
				;;
			opts=*)
				options="${opt#options=}"

				for opt in ${options//,/ }
				do
					case "${opt}" in
						noswap)
							noswap="y"
							;;
					esac
				done
				;;	
		esac
	done

	[ -f "${OVLROOT_CFGFILE}" ] && . "${OVLROOT_CFGFILE}"

	ovlroot_lowerdir="${OVLROOT_MAINDIR}/${OVLROOT_LOWERDIR_NAME}"
	ovlroot_upperdir="${OVLROOT_MAINDIR}/${OVLROOT_UPPERDIR_NAME}"
	ovlroot_workdir="${OVLROOT_MAINDIR}/${OVLROOT_WORKDIR_NAME}"

	mkdir -p "${OVLROOT_MAINDIR}"
	mount -t "tmpfs" "overlayroot-tmpfs" "${OVLROOT_MAINDIR}"
	
	mkdir -p "${ovlroot_lowerdir}"
	mkdir -p "${ovlroot_upperdir}/root"
	mkdir -p "${ovlroot_workdir}/root"

	mount -o "move" "${ROOTMNT}" "${ovlroot_lowerdir}"
	mount -t "overlay" -o "lowerdir=${ovlroot_lowerdir},\
upperdir=${ovlroot_upperdir}/root,workdir=${ovlroot_workdir}/root" \
"overlayroot" "${ROOTMNT}"

	mkdir -p "${ROOTMNT}/${OVLROOT_MAINDIR}"
	mount -o "move" "${OVLROOT_MAINDIR}" "${ROOTMNT}/${OVLROOT_MAINDIR}"

	while IFS= read -r line
	do
		local fs="" fsdir="" fstype="" fsopts="" fsdump="" fspass=""
		local error="" fsclass="" fsopts_ro=""
		local _line="${line%%#*}"

		[ ${#_line} -eq 0 ] && { echo "${line}"; continue; }

		read -r fs fsdir fstype fsopts fsdump fspass error <<-EOF
		${_line}
		EOF

		if [ ${#fs} -gt 0 ]
		then
			if [ ${#fsopts} -eq 0 ] || [ ${#error} -ne 0 ]
			then
				add_comments "${MSG_DEACTIVATED}" "${line}"
				continue
			fi
		else
			echo; continue
		fi

		fsopts_ro="$(fsopts_add_ro "${fsopts}")"

		if [ "${fstype}" = "swap" ]
		then
			if [ "${noswap}" = "y" ]
			then
				add_comments "${MSG_DEACTIVATED}" "${line}"
				continue
			fi

			fsclass="ram_only"
		else
			if   list_contains "${OVLROOT_FS_ROONLY//,/ }" "${fstype}"
			then
				fsclass="ro_only"
			elif list_contains "${OVLROOT_FS_RAMONLY//,/ }" "${fstype}"
			then
				fsclass="ram_only"
			else
				fsclass="overlay"
			fi
		fi

		if [ "${fsdir}" = "/" ]
		then
			[ "${fsclass}" != "overlay" ] && return

			fsopts_root="${fsopts_ro}"
			add_comments "${MSG_DEACTIVATED}" "${line}"
			continue
		fi

		if [ "${fsclass}" != "ram_only" ]
		then
			if [ "${mnts_overlay}" = "all" ] || \
			   list_contains "${mnts_overlay//,/ }" "${fsdir}"
			then
				if [ "${fsclass}" != "ro_only" ]
				then
					add_comments "${MSG_MODIFIED}" "${line}"
					fstype="overlayroot.${fstype}"
					fsopts="${fsopts_ro}"
				fi
			elif [ "${mnts_rdonly}" = "all" ] ||
				 list_contains "${mnts_rdonly//,/ }" "${fsdir}"
			then
				add_comments "${MSG_MODIFIED}" "${line}"
				fsopts="${fsopts_ro}"
				fspass=0
			fi
		fi

		printf "%s\t%s\t%s\t%s\t%s\t%s\n" "${fs}" "${fsdir}" \
		"${fstype}" "${fsopts}" "${fsdump}" "${fspass}"
	done <"${ROOTMNT}/${FSTAB_SYSTEM}" >"${OVLROOT_FSTAB}"

	mount -o "remount,${fsopts_root}" "${ROOTMNT}/${ovlroot_lowerdir}"
	mv "${OVLROOT_FSTAB}" "${ROOTMNT}/${FSTAB_SYSTEM}"
}