public
Last active

Boot OpenSUSE from ISO file using grub2

  • Download Gist
suse-fromiso
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
#!/bin/sh
#
# suse-fromiso - Patch the ramdisk of Open SUSE to allow booting from
# ISO image
#
# Copyright (C) 2012 Mansour <mansour@oxplot.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# - Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
 
# Example:
#
# Let's say SUSE ISO file is located at ~/suse.iso
# Then the following will generate a patched ramdisk which we will use
# later to boot from ISO (run as root or sudo):
#
# $ ./suse-fromiso ~/suse.iso ~/initrd-fromiso
#
# Let's also assume the following layout of your final boot media:
#
# /
# /suse.iso
# /initrd-fromiso (generated by this script earlier)
#
# Then the following grub2 menu entry will boot DSL from the ISO file:
#
# menuentry "Open SUSE" {
# loopback loop /suse.iso
# linux (loop)/boot/x86_64/loader/linux ramdisk_size=512000 ramdisk_blocksize=4096 splash=silent quiet preloadlog=/dev/null showopts fromiso=suse.iso
# initrd /initrd-fromiso
# }
 
# Parse command line arguments
 
ISOPATH="$1"
OUTPATH="$2"
if [ -z "$ISOPATH" ]; then
echo "Usage $0: <iso-path> [<out>]"
exit 1
fi
 
# Mount ISO and extract initrd
 
ISOMNT=$(mktemp -d /tmp/tmpXXXXXX)
if mount -t iso9660 -o ro,loop "$ISOPATH" "$ISOMNT"
then
 
TMPHOLD=$(mktemp -d /tmp/tmpXXXXXX)
gzip -d < "$ISOMNT"/boot/x86_64/loader/initrd > "$TMPHOLD"/initrd
umount "$ISOMNT"
 
INITDIR=$(mktemp -d /tmp/tmpXXXXXX)
(cd $INITDIR; cpio -id < "$TMPHOLD"/initrd)
 
# Patch /include to handler fromiso boot parameter
 
patch -d "$INITDIR" -p1 <<"EOF"
--- a/include 2012-03-19 19:35:26.812308216 +1100
+++ b/include 2012-03-19 21:54:20.585734070 +1100
@@ -3085,6 +3085,22 @@
continue
fi
if [ "$mode" = "install" ];then
+ # try loop mounting image defined by fromiso boot
+ # parameter - loop5 seems to be free to use
+ if [ -n "$fromiso" ] && [ -f "/mnt/$fromiso" ] &&
+ losetup /dev/loop5 "/mnt/$fromiso" &>/dev/null; then
+ if eval mount -o ro /dev/loop5 /mnt &>/dev/null; then
+ umount /dev/loop5 &>/dev/null &&
+ losetup -d /dev/loop5 &>/dev/null &&
+ mkdir -p /usbmnt &>/dev/null &&
+ kiwiMount "$dev" "/usbmnt" "-o ro" &>/dev/null &&
+ losetup /dev/loop5 "/usbmnt/$fromiso" &> /dev/null &&
+ eval mount -o ro /dev/loop5 /mnt &>/dev/null &&
+ stickDevice=/dev/loop5
+ else
+ losetup -d /dev/loop5 &>/dev/null
+ fi
+ fi
# /.../
# USB stick search for install media
# created with kiwi
@@ -3093,6 +3109,10 @@
[ ! -e /mnt/config.isoclient ] && \
[ ! -e /mnt/config.usbclient ]
then
+ umount /dev/loop5 &>/dev/null
+ losetup -d /dev/loop5 &>/dev/null
+ umount /usbmnt &>/dev/null
+ rmdir /usbmnt &>/dev/null
umountSystem
continue
fi
@@ -3119,6 +3139,7 @@
fi
fi
stickFound=1
+ umount /dev/loop5 &>/dev/null
umountSystem
break
done
@@ -3207,12 +3228,23 @@
else
eval mount $cdopt -o ro $i /cdrom >/dev/null
fi
+ # try loop mounting image defined by fromiso boot
+ # parameter - loop5 seems to be free to use
+ if [ -n "$fromiso" ] && [ -f "/cdrom/$fromiso" ] &&
+ losetup /dev/loop5 "/cdrom/$fromiso"; then
+ if eval mount -o ro /dev/loop5 /cdrom; then
+ i=/dev/loop5
+ else
+ losetup -d /dev/loop5 &>/dev/null
+ fi
+ fi
if [ -f $LIVECD_CONFIG ];then
cddev=$i; echo
umount $i &>/dev/null
return
fi
umount $i &>/dev/null
+ losetup -d /dev/loop5 &>/dev/null
done
if [ $count -eq 3 ]; then
break
@@ -3310,6 +3342,27 @@
return
fi
#======================================
+ # 1.5) USB disks with ISO image on them
+ #--------------------------------------
+ if [ -n "$fromiso" ]; then
+ USBStickDevice install
+ Echo -n "Checking USB device(s)..."
+ if [ ! $stickFound = 0 ];then
+ cddev=$stickDevice; echo
+ if [ -d "/usbmnt" ];then
+ eval mount -t iso9660 -o ro $cddev /cdrom 1>&2
+ if [ "$mode" = "install" ];then
+ # /.../
+ # if we found an install CD/DVD any disk device
+ # is free to serve as install target
+ # ----
+ unset imageDiskDevice
+ fi
+ return
+ fi
+ fi
+ fi
+ #======================================
# 2) hybrid disk devices
#--------------------------------------
searchImageHybridMedia
@@ -5811,6 +5864,13 @@
mkdir -p /mnt/$cdDir && mount --move /$cdDir /mnt/$cdDir
fi
#======================================
+ # move USB mount point to system
+ #--------------------------------------
+ local usbDir=/usbmnt
+ if [ -d $usbDir ];then
+ mkdir -p /mnt/$usbDir && mount --move /$usbDir /mnt/$usbDir
+ fi
+ #======================================
# move device nodes
#--------------------------------------
Echo "Activating Image: [$name]"
EOF
 
test -z "$OUTPATH" && OUTPATH="initrd-fromiso"
(
cd "$INITDIR";
echo ./include | cpio -o -H newc -A -F "$TMPHOLD"/initrd
)
gzip -9 < "$TMPHOLD"/initrd > "$OUTPATH"
 
rm -Rf "$INITDIR"
rm "$TMPHOLD"/initrd
rmdir "$TMPHOLD"
fi
rmdir "$ISOMNT"

it does not work with my iso file built with susestudio based on opensuse 12.2

This script doesnt work with latest OpenSuse LIve KDE iso image: openSUSE-13.1-KDE-Live-x86_64.iso

Can you please update this script?

Looking at the source of init and include in the initrd image, I guess the patch in this script cannot be made to work for OpenSUSE 13.1. Happily, the initrd for openSUSE-13.1 has been reworked and in most cases no initrd modification is necessary. The kernel now has boot-parameters isofrom_device and isofrom_system. The only puzzle that must be solved is exactly how grub and the kernel find the files they need.

As a concrete example, I have an OpenSUSE .iso file in directory /boot/iso on partition /dev/sde1. By trial and error I found that Grub thinks it is on partition (hd3,msdos1) and the SUSE kernel thinks it is on partition /dev/sdf1. Also, the linux kernel and initrd are in directory /boot/x86_64/loader in the .iso file.

So my entry in grub.cfg is:

menuentry 'Open SUSE .iso' {
    load_video
    insmod gzio
    insmod part_msdos
    insmod ext2
    set root='(hd3,msdos1)'
    set isofrom_device='/dev/sdf1'
    set isofrom_system='/boot/iso/openSUSE-13.1-KDE-Live-x86_64.iso'
    set loader='/boot/x86_64/loader'
    loopback loop $isofrom_system
    linux   (loop)/$loader/linux isofrom_device=$isofrom_device isofrom_system=$isofrom_system ramdisk_size=512000 ramdisk_blocksize=4096 ro quiet splash $vt_handoff preloadlog=/dev/null showopts
    initrd  (loop)/$loader/initrd
}

TNX. Unfortunately it doesnt work for me with openSUSE-13.1-KDE-Live-x86_64.iso. I tested a lot of possible values for 'isofrom_device', but the response is always:

error: unknown filesystem
error: disk 'loop' not found
error: you need to load the kernel first

No, the code above works fine. It was my mistake (wrong parameters). Sorry...

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.