Fix several problems in linux_find_root_device
[kboot-utils.git] / kboot-mkconfig
1 #! /bin/sh
2 # Generate kboot.conf by inspecting /boot contents.
3 # Copyright (C) 2011  Antonio Ospite <ospite@studenti.unina.it>
4 #
5 # Inspired by kboot-mkconfig
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This progra, is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this software.  If not, see <http://www.gnu.org/licenses/>.
19
20 set -e
21
22 PACKAGE_NAME=kboot-mkconfig
23 PACKAGE_VERSION=0.1a
24
25 prefix=/usr
26 exec_prefix=${prefix}
27 sbindir=${exec_prefix}/sbin
28 bindir=${exec_prefix}/bin
29 libdir=${exec_prefix}/lib
30 sysconfdir=/etc
31 host_os=linux-gnu
32 datarootdir=${prefix}/share
33 datadir=${datarootdir}
34 pkgdatadir=${datadir}/kboot
35 kboot_conf=""
36
37 self=`basename $0`
38
39 # Usage: usage
40 # Print the usage.
41 usage () {
42     cat <<EOF
43 Usage: $self [OPTION]
44 Generate a kboot config file
45
46   -o, --output=FILE       output generated config to FILE [default=stdout]
47   -h, --help              print this message and exit
48   -v, --version           print the version information and exit
49
50 Report bugs to <bug-kboot@gnu.org>.
51 EOF
52 }
53
54 argument () {
55   opt=$1
56   shift
57
58   if test $# -eq 0; then
59       echo "$0: option requires an argument -- '$opt'" 1>&2
60       exit 1
61   fi
62   echo $1
63 }
64
65 # Check the arguments.
66 while test $# -gt 0
67 do
68     option=$1
69     shift
70
71     case "$option" in
72     -h | --help)
73         usage
74         exit 0 ;;
75     -v | --version)
76         echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
77         exit 0 ;;
78     -o | --output)
79         kboot_conf=`argument $option "$@"`; shift;;
80     --output=*)
81         kboot_conf=`echo "$option" | sed 's/--output=//'`
82         ;;
83     -*)
84         echo "Unrecognized option \`$option'" 1>&2
85         usage
86         exit 1
87         ;;
88     # Explicitly ignore non-option arguments, for compatibility.
89     esac
90 done
91
92 . ${libdir}/kboot/kboot-mkconfig_lib
93
94 if [ "x$EUID" = "x" ] ; then
95   EUID=`id -u`
96 fi
97
98 if [ "$EUID" != 0 ] ; then
99   root=f
100   case "`uname 2>/dev/null`" in
101     CYGWIN*)
102       # Cygwin: Assume root if member of admin group
103       for g in `id -G 2>/dev/null` ; do
104         case $g in
105           0|544) root=t ;;
106         esac
107       done ;;
108   esac
109   if [ $root != t ] ; then
110     echo "$self: You must run this as root" >&2
111     exit 1
112   fi
113 fi
114
115
116 if test -f ${sysconfdir}/default/kboot ; then
117   . ${sysconfdir}/default/kboot
118 fi
119
120 # Default values
121 if [ "x${KBOOT_TIMEOUT}" = "x" ] ; then KBOOT_TIMEOUT=5 ; fi
122
123 if [ "x${KBOOT_DISTRIBUTOR}" = "x" ] ; then
124   OS=GNU/Linux
125 else
126   OS="${KBOOT_DISTRIBUTOR} GNU/Linux"
127 fi
128
129 # These are optional, user-defined variables.
130 export KBOOT_TIMEOUT \
131   KBOOT_DISTRIBUTOR \
132   KBOOT_CMDLINE_LINUX \
133   KBOOT_CMDLINE_LINUX_DEFAULT \
134   KBOOT_DISABLE_RECOVERY
135
136 if test "x${kboot_conf}" != "x"; then
137   rm -f ${kboot_conf}.new
138   exec > ${kboot_conf}.new
139 fi
140 echo "Generating kboot.conf ..." >&2
141
142 cat << EOF
143 #
144 # DO NOT EDIT THIS FILE
145 #
146 # It is automatically generated by $self
147 # with settings from ${sysconfdir}/default/kboot
148 #
149
150 timeout=${KBOOT_TIMEOUT}
151
152 EOF
153
154 echo "### BEGIN list of kernels ###"
155 list_kernels
156 echo "### END list of kernels ###"
157
158 if test "x${kboot_conf}" != "x" ; then
159    # none of the children aborted with error, install the new kboot.cfg
160    mv -f ${kboot_conf}.new ${kboot_conf}
161 fi
162
163 echo "done" >&2