]> git.proxmox.com Git - grub2.git/blob - util/update-grub_lib.in
2008-07-23 Robert Millan <rmh@aybabtu.com>
[grub2.git] / util / update-grub_lib.in
1 # Helper library for update-grub
2 # Copyright (C) 2007,2008 Free Software Foundation, Inc.
3 #
4 # GRUB is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # GRUB is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with GRUB. If not, see <http://www.gnu.org/licenses/>.
16
17 transform="@program_transform_name@"
18
19 prefix=@prefix@
20 exec_prefix=@exec_prefix@
21 datarootdir=@datarootdir@
22 datadir=@datadir@
23 sbindir=@sbindir@
24 pkgdatadir=${datadir}/`echo @PACKAGE_TARNAME@ | sed "${transform}"`
25
26 grub_probe=${sbindir}/`echo grub-probe | sed ${transform}`
27
28 make_system_path_relative_to_its_root ()
29 {
30 path=$1
31 # abort if file doesn't exist
32 if test -e $path ; then : ;else
33 return 1
34 fi
35
36 # canonicalize
37 if path=`readlink -f $path` ; then : ; else
38 return 1
39 fi
40
41 # if not a directory, climb up to the directory containing it
42 if test -d $path ; then
43 dir=$path
44 else
45 dir=`echo $path | sed -e "s,/[^/]*$,,g"`
46 fi
47
48 num=`stat -c %d $dir`
49
50 # this loop sets $dir to the root directory of the filesystem we're inspecting
51 while : ; do
52 parent=`readlink -f $dir/..`
53 if [ "x`stat -c %d $parent`" = "x$num" ] ; then : ; else
54 # $parent is another filesystem; we found it.
55 break
56 fi
57 if [ "x$dir" = "x/" ] ; then
58 # / is our root.
59 break
60 fi
61 dir=$parent
62 done
63
64 # This function never prints trailing slashes (so that its output can be
65 # appended a slash unconditionally). Each slash in $dir is considered a
66 # preceding slash, and therefore the root directory is an empty string.
67 if [ "$dir" = "/" ] ; then
68 dir=""
69 fi
70
71 echo $path | sed -e "s,^$dir,,g"
72 }
73
74 is_path_readable_by_grub ()
75 {
76 path=$1
77
78 # abort if path doesn't exist
79 if test -e $path ; then : ;else
80 return 1
81 fi
82
83 # abort if file is in a filesystem we can't read
84 if ${grub_probe} -t fs $path > /dev/null 2>&1 ; then : ; else
85 return 1
86 fi
87
88 return 0
89 }
90
91 convert_system_path_to_grub_path ()
92 {
93 path=$1
94
95 echo "Warning: convert_system_path_to_grub_path() is deprecated. Use prepare_grub_to_access_device() instead." >&2
96
97 # abort if GRUB can't access the path
98 if is_path_readable_by_grub ${path} ; then : ; else
99 return 1
100 fi
101
102 if drive=`${grub_probe} -t drive $path` ; then : ; else
103 return 1
104 fi
105
106 if relative_path=`make_system_path_relative_to_its_root $path` ; then : ; else
107 return 1
108 fi
109
110 echo ${drive}${relative_path}
111 }
112
113 prepare_grub_to_access_device ()
114 {
115 device=$1
116
117 # Abstraction modules aren't auto-loaded.
118 abstraction="`${grub_probe} --device ${device} --target=abstraction`"
119 if [ "x${abstraction}" = "x" ] ; then : ; else
120 echo "insmod ${abstraction}"
121 fi
122
123 # If there's a filesystem UUID that GRUB is capable of identifying, use it;
124 # otherwise set root as per value in device.map.
125 echo "set root=`${grub_probe} --device ${device} --target=drive`"
126 if fs_uuid="`${grub_probe} --device ${device} --target=fs_uuid 2> /dev/null`" ; then
127 echo "search --fs-uuid --set ${fs_uuid}"
128 fi
129 }
130
131 font_path ()
132 {
133 for dir in ${pkgdatadir} /boot/grub /usr/share/grub ; do
134 # FIXME: We prefer ascii because loading complete fonts is too slow (and
135 # we don't yet provide the gettext magic that would make unicode useful).
136 for basename in ascii unicode unifont ; do
137 path="${dir}/${basename}.pff"
138 if is_path_readable_by_grub ${path} > /dev/null ; then
139 echo "${path}"
140 return 0
141 fi
142 done
143 done
144
145 return 1
146 }
147
148 grub_file_is_not_garbage ()
149 {
150 if test -f "$1" ; then
151 case "$1" in
152 *.dpkg-dist|*.dpkg-old|*.dpkg-tmp) return 1 ;; # debian dpkg
153 esac
154 else
155 return 1
156 fi
157 return 0
158 }