Package: grub2
Version: 1.98
Severity: wishlist
Tags: patch

There may be times where we want to generate a grub.cfg in an environment where 
the installation /target does not actually contain grub binaries.

Consider a scenario where we copy a squashfs image directly to a hard drive 
with the intention of booting from it in the future. Note that this differs 
from a live install in that we are not copying the uncompressed *contents* of 
the squashfs image, but rather, the compressed image itself.

In this case, we cannot chroot to /target and run grub-mkconfig as we normally 
might, since various grub scripts will not exist.

Nor can we loopback mount the squashfs image, chroot to it, and execute grub, 
since that file system will be read-only.

All we really want is to generate a grub.cfg, but for a device that is mounted 
at some other mountpoint other than /.

The proposed patch may not be the final form, but please consider it an example 
of what I am proposing. It is intended to serve the same purpose as 
'grub-install --root-directory=/target'


-- System Information:
Debian Release: squeeze/sid
  APT prefers lucid-updates
  APT policy: (500, 'lucid-updates'), (500, 'lucid-security'), (500, 'lucid')
Architecture: amd64 (x86_64)

Kernel: Linux 2.6.32-22-generic (SMP w/2 CPU cores)
Locale: LANG=en_US.utf8, LC_CTYPE=en_US.utf8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash
--- util/grub-mkconfig.in.old	2010-06-03 16:55:15.001871500 -0600
+++ util/grub-mkconfig.in	2010-06-03 16:59:58.061845841 -0600
@@ -30,6 +30,7 @@ pkgdatadir=${datadir}/`echo @PACKAGE_TAR
 grub_prefix=`echo /boot/grub | sed ${transform}`
 grub_cfg=""
 grub_mkconfig_dir=${sysconfdir}/grub.d
+TARGET_ROOT=""
 
 grub_mkdevicemap=${sbindir}/`echo grub-mkdevicemap | sed ${transform}`
 grub_probe=${sbindir}/`echo grub-probe | sed ${transform}`
@@ -42,6 +43,7 @@ Usage: $0 [OPTION]
 Generate a grub config file
 
   -o, --output=FILE       output generated config to FILE [default=stdout]
+  -t <directory>          use <directory> as device root
   -h, --help              print this message and exit
   -v, --version           print the version information and exit
 
@@ -59,12 +61,18 @@ for option in "$@"; do
 	echo "$0 (GNU GRUB ${package_version})"
 	exit 0 ;;
     -o)
+	grub_cfg=$2
+	shift
 	shift
-	grub_cfg=$1
 	;;
     --output=*)
 	grub_cfg=`echo "$option" | sed 's/--output=//'`
 	;;
+    -t)
+	TARGET_ROOT=$2
+	shift
+	shift
+	;;
     -*)
 	echo "Unrecognized option \`$option'" 1>&2
 	usage
@@ -119,16 +127,16 @@ if test -e ${grub_prefix}/device.map ; t
 fi
 
 # Device containing our userland.  Typically used for root= parameter.
-GRUB_DEVICE="`${grub_probe} --target=device /`"
+GRUB_DEVICE="`${grub_probe} --target=device ${TARGET_ROOT}/`"
 GRUB_DEVICE_UUID="`${grub_probe} --device ${GRUB_DEVICE} --target=fs_uuid 2> /dev/null`" || true
 
 # Device containing our /boot partition.  Usually the same as GRUB_DEVICE.
-GRUB_DEVICE_BOOT="`${grub_probe} --target=device /boot`"
+GRUB_DEVICE_BOOT="`${grub_probe} --target=device ${TARGET_ROOT}/boot`"
 GRUB_DEVICE_BOOT_UUID="`${grub_probe} --device ${GRUB_DEVICE_BOOT} --target=fs_uuid 2> /dev/null`" || true
 
 # Filesystem for the device containing our userland.  Used for stuff like
 # choosing Hurd filesystem module.
-GRUB_FS="`${grub_probe} --target=fs / 2> /dev/null || echo unknown`"
+GRUB_FS="`${grub_probe} --target=fs ${TARGET_ROOT}/ 2> /dev/null || echo unknown`"
 
 if test -f ${sysconfdir}/default/grub ; then
   . ${sysconfdir}/default/grub
@@ -224,7 +232,8 @@ export GRUB_DEFAULT \
   GRUB_GFXPAYLOAD_LINUX \
   GRUB_DISABLE_OS_PROBER \
   GRUB_INIT_TUNE \
-  GRUB_SAVEDEFAULT
+  GRUB_SAVEDEFAULT \
+  TARGET_ROOT
 
 if test "x${grub_cfg}" != "x"; then
   rm -f ${grub_cfg}.new

Reply via email to