summaryrefslogtreecommitdiffstats
path: root/scripts/lib/ptxd_make_image_fix_permissions.sh
blob: eae2ab3fe237fbd77a1a81622f495364ae38934c (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
#!/bin/bash
#
# Copyright (C) 2008, 2009 by Marc Kleine-Budde <mkl@pengutronix.de>
#
# See CREDITS for details about who has contributed to this project.
#
# For further information about the PTXdist project and license conditions
# see the README file.
#

ptxd_make_image_fix_permissions_generate() {
    case "${kind}" in
	n)
	    # erase existing nodes
	    cat >&3 <<EOF
	    echo "creating device node: ${file##${PTXDIST_WORKSPACE}/}"
	    rm -rf -- "${file}" &&
	    mknod  -- "${file}" ${type} ${major_should} ${minor_should} &&
	    chown  -- ${uid_should}\:${gid_should} "${file}" &&
	    chmod  -- ${prm_should}		   "${file}" || exit

EOF
	    ;;
	l)
	    ;;
    esac
}
export -f ptxd_make_image_fix_permissions_generate



ptxd_make_image_fix_permissions_check() {
    local workdir="${1}"
    local ifs_orig="${IFS}"
    IFS=":"

    # just care about dev-nodes, for now
    egrep -h "^[n]:" "${ptxd_reply_perm_files[@]}" |
    while read kind file uid_should gid_should prm_should type major_should minor_should; do
	local fixup=false
	file="${workdir}/${file#/}"

	if [ \! -e "${file}" ]; then
	    fixup=true
	else
	    local uid_is gid_is prm_is
	    eval $(stat -c"uid_is=%u gid_is=%g prm_is=%a" "${file}")

	    # check user/group and permissions
	    if [ \
		 ${uid_is} -ne	${uid_should} -o \
		 ${gid_is} -ne	${gid_should} -o \
		0${prm_is} -ne 0${prm_should} ]; then
		fixup=true

	    # for dev-nodes and pipes
	    elif [ "${kind}" = "n" ]; then
		if [ \
		    "${type}" = "p" -a ! -p "${file}" -o \
		    "${type}" = "c" -a ! -c "${file}" -o \
		    "${type}" = "b" -a ! -b "${file}" ]; then
		    fixup=true

		# for dev-nodes check major/minor
		elif [ "${type}" != "p" ]; then
		    local major_is minor_is
		    eval $(stat -c"major_is=0x%t minor_is=0x%T" "${file}")

		    # convert from hex to dec
		    major_is=$(( major_is ))
		    minor_is=$(( minor_is ))

		    if [ \
			${major_is} -ne ${major_should} -o \
			${minor_is} -ne ${minor_should} ]; then
			fixup=true
		    fi
		fi
	    fi
	fi

	if [ "${fixup}" = "true" ]; then
	    ptxd_make_image_fix_permissions_generate
	fi
    done

    IFS="${ifs_orig}"
}
export -f ptxd_make_image_fix_permissions_check


#
# ptxd_make_image_fix_permissions - create device nodes in nfsroots
#
ptxd_make_image_fix_permissions() {
    ptxd_make_image_init &&

    local fixscript="${PTXDIST_TEMPDIR}/${FUNCNAME}" &&
    touch "${fixscript}" &&
    chmod +x "${fixscript}" &&

    # get permission files
    local -a ptxd_reply_ipkg_file ptxd_reply_perm_files &&
    ptxd_get_ipkg_files ${image_pkgs_selected_target} || return

    set -- "${ptx_nfsroot}" "${ptx_nfsroot_dbg}"

    exec 3> "${fixscript}"
    while [ ${#} -ne 0 ]; do
	ptxd_make_image_fix_permissions_check "${1}" || return
	shift
    done
    exec 3>&-

    # nothing to do!
    if [ \! -s "${fixscript}" ]; then
	return
    fi

cat <<EOF



-------------------------------------------------------------------
For a proper NFS-root environment, some device nodes are essential.
In order to create them root privileges are required.
-------------------------------------------------------------------

EOF
    read -t 5 -p "(Please press enter to start 'sudo' to gain root privileges.)"
    if [ ${?} -ne 0 ]; then
	cat >&2 <<EOF



WARNING: NFS-root might not be working correctly!


EOF
	return
    fi

    if ! sudo "${fixscript}"; then
	cat <<EOF

error: creation of device node(s) failed.

EOF
	return 1
    fi
    echo
}
export -f ptxd_make_image_fix_permissions