]> TLD Linux GIT Repositories - rc-scripts.git/blob - hwprofile
- disable lock checks (happens on LUKS2 and fails during system boot)
[rc-scripts.git] / hwprofile
1 #!/bin/sh
2 # (C) 2001 Arkadiusz Miƛkiewicz <misiek@pld.ORG.PL>
3 # Hardware Profiles for rc-scripts TOTALNEW.
4
5 PROFDIR="/etc/sysconfig/hwprofiles"
6 QUIET=0
7
8 . /etc/rc.d/init.d/functions
9 . /etc/sysconfig/hwprof
10
11 [ -z "${EDITOR}" ] && EDITOR=vi
12
13 if [ "$(id -u)" != "0" ]; then
14         echo "Need superuser privileges. Can't continue!"
15         exit 1
16 fi
17
18 checkprofdir()
19 {
20         if [ ! -d ${PROFDIR} -o ! -d ${PROFDIR}/data ]; then
21                 echo "${PROFDIR} or ${PROFDIR}/data not found. Can't continue!"
22                 echo "Check /etc/sysconfig/hwprof and hwprofile -h."
23                 exit 1
24         fi
25 }
26
27 myecho()
28 {
29         if [ $QUIET -eq 0 ]; then
30                 echo $@
31         fi
32 }
33
34 showhelp()
35 {
36 echo '
37 no opts      - show help
38 -a           - add profile
39 -d           - delete profile
40 -r foo       - run specified profile or "default" if such exist
41 -s           - save/update running profile
42 -f           - find proper profile and run it
43 -l           - list configured profiles
44 -q           - be quiet (used only in few modes)
45 '
46 }
47
48 # This function probably must be enhanced.
49 md5sumgen()
50 {
51         ( \
52                 grep -v "MHz" /proc/cpuinfo 2> /dev/null \
53                 grep -v "Latency" /proc/pci 2> /dev/null \
54         ) | md5sum | awk ' { gsub(/ .*$/,NIL); print $0 } '
55 }
56
57 while getopts adfhlsqr: opt "$@"; do
58         case "$opt" in
59           a)
60                 checkprofdir
61
62                 echo "Adding new profile..."
63
64                 if [ -f /var/run/hwprofile ]; then
65                         echo -n "Current profile is: "
66                         cat /var/run/hwprofile
67                 fi
68
69                 PROFILE=
70                 while [ -z "${PROFILE}" ]; do
71                         echo -n "Enter profile name: "
72                         read PROFILE
73
74                         if [ -f ${PROFDIR}/${PROFILE}.md5 ]; then
75                                 echo "Profile ${PROFILE} exist. Try other name."
76                                 PROFILE=
77                         fi
78                 done
79
80                 rm -rf ${PROFDIR}/${PROFILE}.* ${PROFDIR}/data/${PROFILE}/
81                 md5sumgen > ${PROFDIR}/${PROFILE}.md5
82                 LANG=C LC_ALL=C date > ${PROFDIR}/${PROFILE}.date
83                 mkdir -p ${PROFDIR}/data/${PROFILE}/
84                 chmod 700 ${PROFDIR}/data/${PROFILE}/
85
86                 echo -n "Enter profile description (PROFDIR/${PROFILE}.desc): "
87                 read DESCRIPTION
88                 [ -z "${DESCRIPTION}" ] && DESCRIPTION="${PROFILE}"
89                 echo "${DESCRIPTION}" > ${PROFDIR}/${PROFILE}.desc
90                 echo "Now, editor will be started and you need to enter list of files"
91                 echo "for this profile (later as ${EDITOR} ${PROFDIR}/${PROFILE}.files)."
92                 echo "Press Enter."
93                 read
94
95                 if [ -f ${PROFDIR}/files -o ! -f ${PROFDIR}/${PROFILE}.files ]; then
96                         cat ${PROFDIR}/files > ${PROFDIR}/${PROFILE}.files;
97                 fi
98                 ${EDITOR} ${PROFDIR}/${PROFILE}.files
99
100                 FILES=
101                 echo "Validating and copying valid files for this profile:"
102                 for file in $(grep -v "^#" ${PROFDIR}/${PROFILE}.files); do
103                         if [ ! -f ${file} -o "${file}" = "/etc/sysconfig/hwprof" ]; then
104                                 echo "${file}: invalid, skipping"
105                                 continue
106                         fi
107
108                         bfile=$(basename ${file})
109                         echo "Copying ${file} to ${PROFDIR}/data/${PROFILE}/${file}..."
110                         cp -dp --parents ${file} ${PROFDIR}/data/${PROFILE}/
111                         FILES="${file}\n${FILES}"
112                 done
113
114                 echo -e "${FILES}" > ${PROFDIR}/${PROFILE}.files
115                 rm -f ${PROFDIR}/${PROFILE}.*~
116
117                 echo "New profile ready."
118                 break
119                 ;;
120           d)
121                 checkprofdir
122
123                 echo "Deleting existing profile..."
124
125                 PROFILE=
126                 while [ -z ${PROFILE} ]; do
127                         echo -n "Enter profile name: "
128                         read PROFILE
129
130                         if [ ! -f ${PROFDIR}/${PROFILE}.md5 ]; then
131                                 echo "Profile ${PROFILE} doesn't exist. Try other name."
132                                 PROFILE=
133                         fi
134                 done
135
136                 echo -n "Are you sure? [y/N]: "
137                 read YN
138                 case "${YN}" in
139                   y | Y | t | T )
140                         # ok
141                         ;;
142                   *)
143                         echo "Exiting then."
144                         exit 1
145                         ;;
146                 esac
147
148                 rm -rf ${PROFDIR}/${PROFILE}.* ${PROFDIR}/data/${PROFILE}/
149                 echo "Done."
150                 break
151                 ;;
152           r)
153
154                 checkprofdir
155
156                 PROFILE="${OPTARG}"
157
158                 if [ -z "${PROFILE}" -o ! -f ${PROFDIR}/${PROFILE}.files ]; then
159                         echo "Can't setup ${PROFILE}. No required data."
160                         exit 1
161                 fi
162
163                 echo -n "Setting up profile \"${PROFILE}\" "
164                 myecho "";
165
166                 for file in $(grep -v "^#" ${PROFDIR}/${PROFILE}.files); do
167                         if [ ! -f ${file} -o "${file}" = "/etc/sysconfig/hwprof" ]; then
168                                 myecho "${file}: invalid, skipping"
169                                 continue
170                         fi
171                         echo -n ".";
172
173                         myecho "Copying ${PROFDIR}/data/${PROFILE}/${file} to ${file}..."
174                         cp -dp ${PROFDIR}/data/${PROFILE}/${file} ${file} 2>/dev/null
175                 done
176
177                 echo ${PROFILE} > /var/run/hwprofile
178                 echo " Done.";
179                 myecho "Profile \"${PROFILE}\" running."
180                 break
181                 ;;
182           f)
183                 checkprofdir
184
185                 PROFILE=
186                 PROFILEMD5=$(md5sumgen)
187
188                 myecho "Trying to find proper profile..."
189                 for prof in $(ls -1 ${PROFDIR}/*.md5 2> /dev/null); do
190                         prof=$(basename ${prof})
191                         CURRENTMD5="$(cat ${PROFDIR}/${prof} 2> /dev/null)"
192                         # echo "$prof ${CURRENTMD5} ${PROFILEMD5}";
193                         [ -z "${CURRENTMD5}" ] && continue
194
195                         if [ "${CURRENTMD5}" = "${PROFILEMD5}" ]; then
196                                 PROFILE=$(echo ${prof} | awk ' { gsub(/\.md5/,NIL); print $0 } ')
197                                 break;
198                         fi
199                 done
200
201                 if [ -z "${PROFILE}" ]; then
202                         if is_yes "${ASKFORPROFILE}"; then
203                                 echo "Available profiles:";
204                                 PS3='Enter profile number: ';
205                                 LISTA=$(ls ${PROFDIR}/*.desc 2> /dev/null);
206                                 for i in ${LISTA}; do
207                                         a=$(basename $i .desc);
208                                         LISTA1="${a} ${LISTA1}";
209                                 done
210                                 LISTA1="${LISTA1} Default";
211
212                                 select PROFILE in $LISTA1; do
213                                         if [ -n "${PROFILE}" ]; then
214                                                 break
215                                         fi
216                                 done
217
218                                 if [ "${PROFILE}" = "Default" ]; then
219                                         PROFILE=
220                                 fi
221                         fi
222                 fi
223
224                 if [ -z "${PROFILE}" ]; then
225                         if [ ! -f ${PROFDIR}/default.md5 ]; then
226                                 echo "Valid profile not found. Starting with current files."
227                                 exit 0
228                         else
229                                 echo "Valid profile not found. Starting with \"default\" profile."
230                                 PROFILE=default
231                         fi
232                 fi
233
234                 if [ ! -z "${PROFILE}" ]; then
235                         myecho "Found profile \"${PROFILE}\" with md5sum \"${PROFILEMD5}\"."
236                         if [ $QUIET -eq 0 ]; then
237                                 $0 -r ${PROFILE};
238                         else
239                                 $0 -qr ${PROFILE};
240                         fi
241                 fi
242                 break
243                 ;;
244           s)
245                 checkprofdir
246
247                 PROFILE=$(cat /var/run/hwprofile 2> /dev/null)
248
249                 if [ -z "${PROFILE}" ]; then
250                         echo "No profile currenty running."
251                         exit 1
252                 fi
253
254                 if [ ! -f ${PROFDIR}/${PROFILE}.files ]; then
255                         echo "${PROFILE}.files is missing. Can't continue."
256                         exit 1
257                 fi
258
259                 echo -n "Saving/updating current [${PROFILE}] profile "
260                 md5sumgen > ${PROFDIR}/${PROFILE}.md5
261                 myecho "";
262                 for file in $(grep -v "^#" ${PROFDIR}/${PROFILE}.files); do
263                         echo -n ".";
264
265                         if [ ! -f ${file} -o "${file}" = "/etc/sysconfig/hwprof" ]; then
266                                 myecho "${file}: invalid, skipping"
267                                 continue
268                         fi
269
270                         myecho "Copying ${file} to ${PROFDIR}/data/${PROFILE}/${file}"
271                         cp -dp --parents ${file} ${PROFDIR}/data/${PROFILE}/
272                 done
273                 echo " Done."
274                 break
275                 ;;
276           l)
277                 echo "List of configured profiles:"
278
279                 for prof in $(ls -1 ${PROFDIR}/*.md5 2> /dev/null); do
280                         prof=$(basename ${prof})
281                         profname=$(echo ${prof} | awk ' { gsub(/\.md5/,NIL); print $0 } ')
282                         echo    "Name    : ${profname}"
283                         echo -n "Desc    : "
284                         if [ -f ${PROFDIR}/${profname}.md5 ]; then
285                                 cat ${PROFDIR}/${profname}.desc
286                         else
287                                 echo "MISSING"
288                         fi
289                         echo -n "Created : "
290                         if [ -f ${PROFDIR}/${profname}.date ]; then
291                                 cat ${PROFDIR}/${profname}.date
292                         else
293                                 echo "MISSING"
294                         fi
295                         echo -n "md5sum  : "
296                         if [ -f ${PROFDIR}/${profname}.md5 ]; then
297                                 cat ${PROFDIR}/${profname}.md5
298                         else
299                                 echo "MISSING"
300                         fi
301                         echo -n "Files   : "
302                         if [ -f ${PROFDIR}/${profname}.files ]; then
303                                 cat ${PROFDIR}/${profname}.files | xargs
304                         else
305                                 echo "MISSING"
306                         fi
307                         echo
308                 done
309                 echo "End."
310                 break
311                 ;;
312           q)
313                 QUIET=1; export QUIET;
314                 ;;
315           *)
316                 showhelp
317                 exit 1
318                 break
319                 ;;
320         esac
321 done
322
323 if [ "$#" -le "0" -o "$OPTIND" -eq 1 ]; then
324         showhelp
325         exit 1
326 fi
327
328 exit 0