File: ltsp-common-functions

package info (click to toggle)
ltsp 5.18.12-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,016 kB
  • sloc: sh: 4,831; ansic: 1,590; perl: 280; python: 197; makefile: 173; lex: 152; yacc: 59
file content (213 lines) | stat: -rw-r--r-- 5,598 bytes parent folder | download | duplicates (3)
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
206
207
208
209
210
211
212
213
# Common functions shared by LTSP scripts

# Multiple inclusions shouldn't ever happen, but let's prevent them anyway
if [ -n "$sourced_ltsp_common_functions" ]; then
    echo "Warning: $0 tried to include ltsp-common-functions multiple times." >&2
    return 0
else
    sourced_ltsp_common_functions=true
fi

# All tools should support the standard --version option, see
# http://www.gnu.org/prep/standards/html_node/_002d_002dversion.html
ltsp_version()
{
    # Distributions should replace the version mentioned here with the actual
    # version mentioned in the changelog, using `sed` in their packaging code.
    echo "$ltsp_tool 1.0"
}

warn()
{
    printf "%s\n" "$*" >&2
}

die() {
    warn "$@"
    exit 1
}

log()
{
    if [ -x /usr/bin/logger ] && [ -e /dev/log ]; then
        logger -s -t "${0##/}" "$*"
    elif [ -w /dev/kmsg ]; then
        warn "${0##/}: $*" 2>>/dev/kmsg
        warn "${0##/}: $*"
    else
        warn "${0##/}: $*"
    fi
}

# To debug LTSP scripts, set ltsp.debug=<scriptname> in the kernel
# command line, or run debug=<scriptname> <scriptname>.
# $0 is automatically prefixed in debug messages.
# TODO: merge ltsp-build-client's debug with this one.
debug()
{
    local x

    if [ -z "$debug" ]; then
        if [ -f /proc/cmdline ]; then
            for x in $(cat /proc/cmdline); do
                case "$x" in
                    ltsp.debug=*)
                        debug=${x#ltsp.debug=}
                    ;;
                esac
            done
        fi
        debug=${debug:-false}
    fi
    if [ "$debug" = "${0##*/}" ]; then
        log "$@"
    fi
}

boolean_is_true(){
    case $1 in
       # match all cases of true|y|yes
       [Tt][Rr][Uu][Ee]|[Yy]|[Yy][Ee][Ss]) return 0 ;;
       *) return 1 ;;
    esac
}

run_parts()
# Parameters:
# $1: Directory with main scripts
# $2: Directory with override scripts, defaults to ''
# $3: Script matching regex, defaults to '[0-9]*'
{
    if [ ! -d "$1" ]; then
        warn "Not a directory: $1"
        return 1
    fi
    # We do want to source the scripts in a subshell, so that they affect each
    # other, but not the callee script.
    # ${2:+"$2"} means "preserve spaces, but don't pass a parameter if empty"
    find "$1" ${2:+"$2"} -maxdepth 1 -type f -name ${3:-'[0-9]*'} \
        -printf '%f\n' 2>/dev/null | sort | uniq | 
        while read script; do
            if [ -n "$2" ] && [ -f "$2/$script" ]; then
                . "$2/$script"
            else
                . "$1/$script"
            fi
        done
}

# list files in a directory consisting only of alphanumerics, hyphens and
# underscores
# $1 - directory to list
# $2 - optional prefix to limit which files are selected
run_parts_list() {
    test $# -ge 1 || die "ERROR: Usage: run_parts_list <dir>"
    if [ -d "$1" ]; then
        find -L "$1" -mindepth 1 -maxdepth 1 -type f -name "$2*" |
            sed -n '/.*\/[[:alnum:]_\-]\{1,\}$/p' | sort -n
    fi
}

detect_vendor() {
    if [ -e /etc/sysconfig/ltspdist ]; then
        . /etc/sysconfig/ltspdist
        echo "$VENDORDEF"
    else
        lsb_vendor=$(lsb_release --id --short | tr " " "_")
        if [ "$lsb_vendor" = LinuxMint ]; then
            if [ "$(lsb_release -s -c)" = "debian" ]; then
                echo "Debian"
            else
                echo "Ubuntu"
            fi
        else
            echo "$lsb_vendor"
        fi
    fi
}

# Distros may override this function to implement their own
# architecture detection.
detect_arch() {
    echo $(uname -m)
}

require_root()
{
    if [ ${UID:-$(id -u)} -ne 0 ]; then
        die "Superuser privileges are needed."
    fi
}

# Remember mounted dirs so that it's easier to unmount them with a single call
# to umount_marked. They'll be unmounted in reverse order.
# Use the normal mount syntax, e.g.
#   mark_mount -t proc proc "$ROOT/proc"
mark_mount() {
    local dir old_marked_mounts

    # The last parameter is the dir we need to remember to unmount
    dir=$(eval "echo \$$#")
    # If the user presses Ctrl+C while mount is still running, there's
    # a possibility that it will succeed but it won't go inside the if.
    # So mark the dir before mount.
    # Use newlines to separate dirs, in case they contain spaces
    old_marked_mounts="$MARKED_MOUNTS"
    if [ -z "$MARKED_MOUNTS" ]; then
        MARKED_MOUNTS="$dir"
    else
        MARKED_MOUNTS="$dir
$MARKED_MOUNTS"
    fi
    if ! mount "$@"; then
        MARKED_MOUNTS="$old_marked_mounts"
        die "Could not mount $dir."
    fi
}

umount_marked() {
    [ -z "$MARKED_MOUNTS" ] && return 0

    # Wait until all buffers are flushed, otherwise umount might fail
    sync
    echo "$MARKED_MOUNTS" | while read dir; do
        if ! umount "$dir"; then
            warn "Couldn't unmount $dir."
        fi
    done
    unset MARKED_MOUNTS
}

read_prompt() {
    # Shell agnostic "read -p" implementation

    # Only output if on an interactive terminal
    if [ -t 0 ]; then
        echo -n "$1"
    fi
    shift
    read "$@"
}

translator_credits() {
    # Display translation credits 
    eval_gettext "translator-credits"
}

debug "Started"

# Source tool-specific settings and functions, if they're provided
ltsp_tool=${0##*/}
if [ -f "/usr/share/ltsp/$ltsp_tool-functions" ]; then
    . "/usr/share/ltsp/$ltsp_tool-functions"
fi

# Source tool-specific configuration files
if [ -f "/etc/ltsp/$ltsp_tool.conf" ]; then
    . "/etc/ltsp/$ltsp_tool.conf"
fi
if [ -d "/etc/ltsp/$ltsp_tool.conf.d" ]; then
    for file in $(run_parts_list "/etc/ltsp/$ltsp_tool.conf.d") ; do
        . "$file"
    done
fi