kboot-mkconfig: bump package version to 0.4
[kboot-utils.git] / kboot-mkconfig_lib
1 # kboot-mkconfig helper script.
2 # Copyright (C) 2011  Antonio Ospite <ospite@studenti.unina.it>
3 #
4 # Inspired by the 10_linux script from grub
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This progra, is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this software.  If not, see <http://www.gnu.org/licenses/>.
18
19 prefix=/usr
20 exec_prefix=${prefix}
21 bindir=${exec_prefix}/bin
22 libdir=${exec_prefix}/lib
23
24 # TODO: remove this if the following find_linux_root_device()
25 # is found to be reliable enough
26 #LINUX_ROOT_DEVICE="$(readlink -f $(mount | grep ' / ' | cut -d ' ' -f 1))"
27
28 if $(which gettext >/dev/null 2>/dev/null) ; then
29   gettext="gettext"
30 else
31   gettext="echo"
32 fi
33
34 # One layer of quotation is eaten by "", the second by sed, and the third by
35 # printf; so this turns ' into \'.  Note that you must use the output of
36 # this function in a printf format string.
37 gettext_quoted () {
38   $gettext "$@" | sed "s/'/'\\\\\\\\''/g"
39 }
40
41 # Run the first argument through gettext_quoted, and then pass that and all
42 # remaining arguments to printf.  This is a useful abbreviation and tends to
43 # be easier to type.
44 gettext_printf () {
45   local format="$1"
46   shift
47   printf "$(gettext_quoted "$format")" "$@"
48 }
49
50 kboot_file_is_not_garbage ()
51 {
52   if test -f "$1" ; then
53     case "$1" in
54       *.dpkg-*) return 1 ;; # debian dpkg
55       README*)  return 1 ;; # documentation
56     esac
57   else
58     return 1
59   fi
60   return 0
61 }
62
63 version_test_gt ()
64 {
65   local sedexp="s/[^-]*-//;s/[._-]\(pre\|rc\|test\|git\|old\|trunk\)/~\1/g"
66   local a=`echo $1 | sed -e "$sedexp"`
67   local b=`echo $2 | sed -e "$sedexp"`
68   local cmp=gt
69   if [ "x$b" = "x" ] ; then
70     return 0
71   fi
72   case $a:$b in
73     *.old:*.old) ;;
74     *.old:*) a=`echo -n $a | sed -e s/\.old$//` ; cmp=gt ;;
75     *:*.old) b=`echo -n $b | sed -e s/\.old$//` ; cmp=ge ;;
76   esac
77   dpkg --compare-versions "$a" $cmp "$b"
78   return $?
79 }
80
81 version_find_latest ()
82 {
83   local a=""
84   for i in $@ ; do
85     if version_test_gt "$i" "$a" ; then
86       a="$i"
87     fi
88   done
89   echo "$a"
90 }
91
92 linux_find_root_device() {
93   ROOT_DEVICE=$(mountpoint -d /)
94
95   for devnode in $(find /dev);
96   do
97     # Get major and minor in decimal
98     CURRENT_DEVICE=$(printf "%d:%d" $(stat --printf="0x%t 0x%T" "$devnode"))
99     if [ $CURRENT_DEVICE = $ROOT_DEVICE ];
100     then
101       ROOT_NODE="$devnode"
102       break
103     fi
104   done
105
106   echo "$ROOT_NODE"
107 }
108
109 linux_entry ()
110 {
111   os="$1"
112   version="$2"
113   recovery="$3"
114   args="$4"
115   if ${recovery} ; then
116     title="$(gettext_quoted "%s, with Linux %s (recovery mode)")"
117   else
118     title="$(gettext_quoted "%s, with Linux %s")"
119   fi
120   label="$(printf "${title}" "${os}" "${version}")"
121   echo -n "'${label}'='${dirname}/${basename} "
122   if test -n "${initrd}" ; then
123         echo -n "initrd=${dirname}/${initrd}"
124   fi
125   echo " root=${linux_root_device_thisversion} ro ${args}'"
126 }
127
128 list_kernels ()
129 {
130   list=`for i in /boot/vmlinuz-* /boot/vmlinux-* /vmlinuz-* /vmlinux-* ; do
131           if kboot_file_is_not_garbage "$i" ; then echo -n "$i " ; fi
132         done`
133   prepare_boot_cache=
134
135   while [ "x$list" != "x" ] ; do
136     linux=`version_find_latest $list`
137     echo "Found linux image: $linux" >&2
138     basename=`basename $linux`
139     dirname=`dirname $linux`
140     
141     # XXX: this relies on a binary shipped by GRUB, we just use ${dirname} for 
142     # now, check the ${dirname} entries if we need something like that later
143     #rel_dirname=`make_system_path_relative_to_its_root $dirname`
144     
145     version=`echo $basename | sed -e "s,^[^0-9]*-,,g"`
146     alt_version=`echo $version | sed -e "s,\.old$,,g"`
147     # TODO: remove this too 
148     #linux_root_device_thisversion="${LINUX_ROOT_DEVICE}"
149     linux_root_device_thisversion=`linux_find_root_device`
150
151     initrd=
152     for i in "initrd.img-${version}" "initrd-${version}.img" \
153              "initrd-${version}" "initramfs-${version}.img" \
154              "initrd.img-${alt_version}" "initrd-${alt_version}.img" \
155              "initrd-${alt_version}" "initramfs-${alt_version}.img"; do
156       if test -e "${dirname}/${i}" ; then
157         initrd="$i"
158         break
159       fi
160     done
161
162     initramfs=
163     for i in "config-${version}" "config-${alt_version}"; do
164       if test -e "${dirname}/${i}" ; then
165         initramfs=`grep CONFIG_INITRAMFS_SOURCE= "${dirname}/${i}" | cut -f2 -d= | tr -d \"`
166         break
167       fi
168     done
169
170     if test -n "${initrd}" ; then
171       echo "Found initrd image: ${dirname}/${initrd}" >&2
172     elif test -z "${initramfs}" ; then
173       # "UUID=" magic is parsed by initrd or initramfs.  Since there's
174       # no initrd or builtin initramfs, it can't work here.
175       linux_root_device_thisversion=${KBOOT_DEVICE}
176     fi
177
178     linux_entry "${OS}" "${version}" false \
179         "${KBOOT_CMDLINE_LINUX} ${KBOOT_CMDLINE_LINUX_DEFAULT}"
180     if [ "x${KBOOT_DISABLE_RECOVERY}" != "xtrue" ]; then
181       linux_entry "${OS}" "${version}" true \
182           "single ${KBOOT_CMDLINE_LINUX}"
183     fi
184
185     list=`echo $list | tr ' ' '\n' | grep -vx $linux | tr '\n' ' '`
186   done
187 }