File: ocs-run-boot-param

package info (click to toggle)
clonezilla 5.14.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,168 kB
  • sloc: sh: 41,203; perl: 193; python: 59; makefile: 26
file content (136 lines) | stat: -rwxr-xr-x 4,253 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
#!/bin/bash
# License: GPL 
# Author: Steven Shiau <steven _at_ clonezilla org>
# Description: Program to run commands assigned in boot parameter, like: ocs_prerun*, ocs_postrun*, ocs_savedisk_prerun*...

DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"
. $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
. /etc/drbl/drbl-ocs.conf
. $DRBL_SCRIPT_PATH/sbin/ocs-functions

#
cmdl_file_def="/proc/cmdline"
force_to_run="no"

# Functions
USAGE() {
    echo "$ocs - Run commands assigned in boot parameter"
    echo "Usage:"
    echo "To run $ocs PARAM:"
    echo "$ocs [OPTION]"
    echo "Options:"
    echo "-b, --batch-mode     Run this program in batch mode"
    echo "-c, --cmdline-file   Assign the kernel boot parameter file. If not assigned, \"$cmdl_file_def\" will be used."
    echo "-f, --force          Force to run $ocs, no matter it has been run successfully or not."
    echo "PARAM is one of the following:"
    echo "ocs_prerun*, ocs_postrun*, ocs_savedisk_prerun*..."
    echo "E.g. To run the commands assigned by ocs_prerun* (e.g. ocs_prerun, ocs_prerun1, ocs_prerun2...) in the kernel boot parameter file 'my-cmdline', run:"
    echo "     $ocs -c my-cmdline ocs_prerun"
    echo "Multiple PARAMs in boot param are available, i.e. ocs_prerun*, just append a number after that. E.g."
    echo "ocs_prerun=... ocs_prerun1=... ocs_prerun2=..."
    echo "//NOTE// use \"ocs_prerun\" for this command only, while putting multiple ocs_prerun* in the boot parameter." 
} # end of USAGE

#################
##### MAIN ######
#################

ocs_file="$0"
ocs=`basename $ocs_file`
#
while [ $# -gt 0 ]; do
 case "$1" in
   -b|--batch-mode)
      ocs_batch_mode="on"
      shift;;
   -c|--cmdline-file)
      shift
      if [ -z "$(echo $1 |grep ^-.)" ]; then
        # skip the -xx option, in case 
        cmdl_file="$1"
        shift
      fi
      [ -z "$cmdl_file" ] && echo "-c is used, but no cmdl_file assigned." && exit 1
      ;;
   -f|--force) force_to_run="yes"; shift;;
   -*)     echo "${0}: ${1}: invalid option" >&2
           USAGE >& 2
           exit 2 ;;
   *)      break ;;
 esac
done

ocs_param="$1"

check_if_root
ask_and_load_lang_set

[ -z "$cmdl_file" ] && cmdl_file="$cmdl_file_def"
if [ ! -e "$cmdl_file" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "Kernel cmdline file ($cmdl_file) does _NOT_ exist!"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "$msg_program_stop"
  exit 1
fi

ocs_param_list="$(grep -Ewo "${ocs_param}[[:digit:]]*" $cmdl_file | uniq | sort -V)"
ocs_param_list="$(echo $ocs_param_list)"  # in one line

if [ -z "$ocs_param_list" ]; then
  exit 1
else
  echo "Found ${ocs_param}* parameter in boot parameters..."
  echo "The order to run: $ocs_param_list"
fi

parse_cmdline_option -c $cmdl_file "echo_${ocs_param}"

eval echo_ocs_param="\$echo_$ocs_param"

# Now start parsing the paramaters listed in $ocs_param_list
RETVAL=0
for i in $ocs_param_list; do
  parse_cmdline_option -c $cmdl_file "$i"
  eval irun=\$$i
  if [ -n "$irun" ]; then
    # Only when not force to run, we will check the tag file
    if [ "$force_to_run" = "no" ]; then
      # Checking if this program is already run
      if [ -e /var/lib/clonezilla/$i ]; then
        [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
	echo "This boot parameter has been successfully run before, so skip it: $i ($irun)"
        [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
        continue
      fi
    fi
    echo "**************************"
    # run it
    if [ "$echo_ocs_param" != "no" ]; then
      echo "Now run \"$i\": $irun..."
    fi
    # Since "$irun" might not be exe mode, so we test it if it's script, use . $irun, else, run it directly.
    if [ "$(LANG=C file -Ls "$irun" 2>/dev/null | grep -iE "shell script")" ]; then
      $irun
      rc=$?
    else
      eval $irun
      rc=$?
    fi
    RETVAL="$((RETVAL + $rc))"
    # Creating state file
    if [ "$rc" -eq 0 ]; then
      touch /var/lib/clonezilla/$i
    else
      [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
      echo "Failed to run: $irun"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      if [ "$ocs_batch_mode" != "on" ]; then
        echo -n "$msg_press_enter_to_continue..."
        read
      fi
    fi
  fi
done

exit $RETVAL