60fe26cc7e7b9f87497ca9b9bf2894957bad75c1
[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 grub-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.2
24
25 prefix=/usr
26 exec_prefix=${prefix}
27 sbindir=${exec_prefix}/sbin
28 bindir=${exec_prefix}/bin
29 sysconfdir=/etc
30 host_os=linux-gnu
31 datarootdir=${prefix}/share
32 datadir=${datarootdir}
33 pkgdatadir=${datadir}/kboot
34 kboot_conf=""
35
36 self=`basename $0`
37
38 # Usage: usage
39 # Print the usage.
40 usage () {
41     cat <<EOF
42 Usage: $self [OPTION]
43 Generate a kboot config file
44
45   -o, --output=FILE       output generated config to FILE [default=stdout]
46   -h, --help              print this message and exit
47   -v, --version           print the version information and exit
48
49 Report bugs to <bug-kboot@gnu.org>.
50 EOF
51 }
52
53 argument () {
54   opt=$1
55   shift
56
57   if test $# -eq 0; then
58       echo "$0: option requires an argument -- '$opt'" 1>&2
59       exit 1
60   fi
61   echo $1
62 }
63
64 # Check the arguments.
65 while test $# -gt 0
66 do
67     option=$1
68     shift
69
70     case "$option" in
71     -h | --help)
72         usage
73         exit 0 ;;
74     -v | --version)
75         echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
76         exit 0 ;;
77     -o | --output)
78         kboot_conf=`argument $option "$@"`; shift;;
79     --output=*)
80         kboot_conf=`echo "$option" | sed 's/--output=//'`
81         ;;
82     -*)
83         echo "Unrecognized option \`$option'" 1>&2
84         usage
85         exit 1
86         ;;
87     # Explicitly ignore non-option arguments, for compatibility.
88     esac
89 done
90
91 . ${datadir}/kboot/kboot-mkconfig_lib
92
93 if [ "x$EUID" = "x" ] ; then
94   EUID=`id -u`
95 fi
96
97 if [ "$EUID" != 0 ] ; then
98   root=f
99   case "`uname 2>/dev/null`" in
100     CYGWIN*)
101       # Cygwin: Assume root if member of admin group
102       for g in `id -G 2>/dev/null` ; do
103         case $g in
104           0|544) root=t ;;
105         esac
106       done ;;
107   esac
108   if [ $root != t ] ; then
109     echo "$self: You must run this as root" >&2
110     exit 1
111   fi
112 fi
113
114
115 if test -f ${sysconfdir}/default/kboot ; then
116   . ${sysconfdir}/default/kboot
117 fi
118
119 # Default values
120 if [ "x${KBOOT_TIMEOUT}" = "x" ] ; then KBOOT_TIMEOUT=5 ; fi
121
122 if [ "x${KBOOT_DISTRIBUTOR}" = "x" ] ; then
123   OS=GNU/Linux
124 else
125   OS="${KBOOT_DISTRIBUTOR} GNU/Linux"
126 fi
127
128 # These are optional, user-defined variables.
129 export KBOOT_TIMEOUT \
130   KBOOT_DISTRIBUTOR \
131   KBOOT_CMDLINE_LINUX \
132   KBOOT_CMDLINE_LINUX_DEFAULT \
133   KBOOT_DISABLE_RECOVERY
134
135 if test "x${kboot_conf}" != "x"; then
136   rm -f ${kboot_conf}.new
137   exec > ${kboot_conf}.new
138 fi
139 echo "Generating kboot.conf ..." >&2
140
141 cat << EOF
142 #
143 # DO NOT EDIT THIS FILE
144 #
145 # It is automatically generated by $self
146 # with settings from ${sysconfdir}/default/kboot
147 #
148
149 timeout=${KBOOT_TIMEOUT}
150
151 EOF
152
153 echo "### BEGIN list of kernels ###"
154 list_kernels
155 echo "### END list of kernels ###"
156
157 if test "x${kboot_conf}" != "x" ; then
158    # none of the children aborted with error, install the new kboot.cfg
159    mv -f ${kboot_conf}.new ${kboot_conf}
160 fi
161
162 echo "done" >&2