File: bootcd-usage.lib

package info (click to toggle)
bootcd 6.9.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 520 kB
  • sloc: sh: 1,940; makefile: 49
file content (167 lines) | stat: -rw-r--r-- 4,858 bytes parent folder | download | duplicates (2)
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
# vim: set filetype=sh :
#        file: /usr/share/bootcd/bootcd-usage.lib
#   copyright: Bernd Schumacher <bernd.schumacher@hpe.com> (2001-2020)
#     license: GNU General Public License, version 3
# description: bootcd-usage.lib - functions needed by bootcd scripts

check_OBSOLETE()
{
  local i
  local c
  local OBSOLETE
  local problem

  OBSOLETE="BLANKING CDSCSI CDSPEED FLOPPY_CREATE_DEV ISO_ONLY NOT_TO_RAM FASTBOOT DEVFS"

  for c in $CONF; do
    problem="is set in $c, but is obsolete.
It will be ignored."
    for i in $OBSOLETE; do
      [ "$(set | grep ^$i=)" ] && warn "$i $problem"
    done
  done

  return 0
}

# check_commandline_function <functionname>
# check and enable a function defined from commandline
check_commandline_function()
{
  # 1. program myprog is called with commandline function myfun()
  #    myprog --myfun "myfun() {...}"
  #
  #    a) bootcdconf.lib:initopts_myprog()
  #       unset -f myfun
  #       myfun="" # contains myfun source code
  #    b) bootcdconf.lib:readopts_myprog()
  #       case "$1" in
  #        --myfun) ... myfun="$2"
  #    c) bootcd-usage.lib:checkopts_myprog()
  #        check_commandline_function myfun
  eval "\
  if [ \"\$$1\" ]; then
    if [ \"\$(echo \"\$$1\" | grep \"^\s*$1\s*()\s*$\")\" ]; then
      eval \"\$$1\"
    else
      ia_logerr \"$1 does not have a line <$1()> ($1=<\$$1>)\"; return 105
    fi
    [ \"\$(type \"$1\" | head -1 | grep \"function\")\" ] || \
      { ia_logerr \"function: $1 could not be created ($1=<\$$1>)\"; return 106; }
    unset $1 # delete variable with function source code
  fi\
  "
}

checkopts_bootcdmk2diskconf()
{
  MOUNTGREP='grep -e "^/dev/" -e "^LABEL=" -e "^UUID=" | \
    grep -v -e "\<noauto\>" -e "\<auto\>" -e "\<swap\>" -e \
    "\<devpts\>" -e "\<sysfs\>" -e "\<tmpfs\>" -e "\<proc\>"' # only grep filesystems from fstab
}

checkopts_bootcdwrite()
{
  check_OBSOLETE
  [ "$(whoami)" = "root" ] || { ia_logerr "Please run as root"; return 101; }
  SRCDI="$SRCDISK"; [ "$SRCDI" = "/" ] && SRCDI=""
  add_SRCDI_to_variables
  CHANGES=$VAR/changes
  [ ! "$(echo " $ia_opts " | grep " -s ")" ] || yes="-s"
  check_commandline_function "extra_changes"
}

checkopts_bootcd2disk()
{
  check_OBSOLETE
  [ "$(whoami)" = "root" ] || { ia_logerr "Please run as root"; return 103; }
  [ ! "$(echo " $ia_opts " | grep " -s ")" ] || yes="-s"
  [ ! "$only_copydata" ] || [ "$COPYDEST" ] || \
    { ia_logerr "with Option --only_copydata COPYDEST must not be empty"; return 109; }
  check_commandline_function "do_first"
  check_commandline_function "after_copy"
  check_commandline_function "do_last"
}

# print_only_if_exists <var>
print_only_if_exists()
{
  eval "[ ! \"\${$1+x}\" ] || echo \"$1=<\${$1}>\""
}

# print_bootcd_vars [<program> ...}
print_bootcd_vars()
{
  local programs all i j
  [ "$*" ] && programs="$*" || programs="bootcdwriteconf bootcd2diskconf bootcd2disk bootcdwrite"

  all="$(for i in $( for p in $programs; do echo $(list_$p); done ); do
    echo "$i"; done | sort -u)"

  for i in $all; do
    if [ "$(echo "$i" | grep "#$")" ]; then
      i="$(echo "$i" | sed "s/#$//")"
      for j in $(seq 0 99 | sed "s/^/$i/"); do
        print_only_if_exists "$j"
      done
    elif [ "$(echo "$i" | grep "()$")" ]; then
      i="$(echo "$i" | sed "s/()$//")"
      [ "$(typefun $i)" ] && type $i
    else
      print_only_if_exists "$i"
    fi
  done
  return 0
}

# Set config variables from different sources.
# The later overwrites the previous.
# The sources and order is:
#   1. defaults resulting from command name $0
#   2. values from config files
#   3. options from command line
read_opts()
{
  local i
  local cfg
  local help
  local unknown_opt
  local missing_opt

  CONF=""
  CMD="$(basename $0)"
  i="bootcdwrite bootcdflopcp bootcd2disk bootcdmk2diskconf"
  [ "$(echo " $i " | grep " $CMD ")" ] || { ia_logerr "Please do not run as $CMD (try $i)"; return 102; }

  initopts_$CMD # ignore env
  if [ "$(echo " bootcdwrite " | grep " $CMD ")" ]; then
    defaults_bootcdwrite
  elif [ "$(echo " bootcd2disk " | grep " $CMD ")" ]; then
    defaults_bootcd2disk
  elif [ "$(echo " bootcdmk2diskconf " | grep " $CMD ")" ]; then
    defaults_bootcdmk2diskconf
  fi

  readopts_$CMD "$@" # cmdline may set CONF
  if [ "$help" ]; then
    ia_logerr "$(usage_$CMD)"; return 100
  elif [ "$unknown_opt" ]; then
    ia_logerr "Unknown option: $unknown_opt" ; return 104
  elif [ "$missing_opt" ]; then
    ia_logerr "Missing argument for option: $missing_opt"; return 107
  fi

  for i in ${CONF:-}; do
    cfg="$(find_bootcd_file $i)"
    if [ "$cfg" ]; then
      dbg "read_opts: sourcing cfg=<$cfg>"
      . "$cfg"
    else
      ia_logerr "No CONF=<$i> found."; return 108
    fi
  done
  readopts_$CMD "$@" || return # cmdline over conf
  checkopts_$CMD || return

  dbglog "read_opts: <$(print_bootcd_vars)>"
}