Skip to content

Instantly share code, notes, and snippets.

@valentt
Last active December 14, 2015 07:09
Show Gist options
  • Save valentt/5048303 to your computer and use it in GitHub Desktop.
Save valentt/5048303 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# fedora-fromiso - Patch the ramdisk of Fedora to allow booting from
# ISO image
#
# Copyright (C) 2012 Mansour <mansour@oxplot.com>
# All rights reserved.
# Copyright (C) 2013 Matthias Saou <matthias@saou.eu>
# Update for Fedora 18
#
# 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 Fedora ISO file is located at ~/fedora.iso
# Then the following will generate a patched ramdisk which we will use
# later to boot from ISO (run as root or sudo):
#
# $ ./fedora-fromiso ~/fedora.iso ~/initrd-fromiso
#
# Let's also assume the following layout of your final boot media:
#
# /
# /fedora.iso
# /initrd-fromiso (generated by this script earlier)
#
# Then the following grub2 menu entry will boot DSL from the ISO file:
#
# menuentry "Fedora Live" {
# loopback loop /fedora.iso
# linux (loop)/isolinux/vmlinuz0 root=live:/fedora.iso rootfstype=auto ro liveimg quiet rhgb rd.luks=0 rd.md=0
# 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 sudo mount -t iso9660 -o ro,loop "$ISOPATH" "$ISOMNT"
then
TMPHOLD=$(mktemp -d /tmp/tmpXXXXXX)
gzip -d < "$ISOMNT"/isolinux/initrd0.img > "$TMPHOLD"/initrd
sudo umount "$ISOMNT"
INITDIR=$(mktemp -d /tmp/tmpXXXXXX)
(cd $INITDIR; cpio -id < "$TMPHOLD"/initrd 2>/dev/null) # Silence /dev errors
# Patch relevant files to handle live:/*.iso boot parameter
patch -d "$INITDIR" -p1 <<"EOF"
--- a/lib/dracut/hooks/pre-udev/30-dmsquash-liveiso-genrules.sh 2013-01-16 23:54:38.281877026 +0100
+++ b/lib/dracut/hooks/pre-udev/30-dmsquash-liveiso-genrules.sh 2013-01-17 00:06:14.116312470 +0100
@@ -3,7 +3,7 @@
# ex: ts=8 sw=4 sts=4 et filetype=sh
if [ "${root%%:*}" = "liveiso" ]; then
{
- printf 'KERNEL=="loop0", RUN+="/sbin/initqueue --settled --onetime --unique /sbin/dmsquash-live-root `/sbin/losetup -f --show %s`"\n' \
+ printf 'KERNEL=="loop0", RUN+="/sbin/initqueue --settled --onetime --unique /sbin/dmsquash-live-root `/sbin/liveiso-find %s`"\n' \
${root#liveiso:}
} >> /etc/udev/rules.d/99-liveiso-mount.rules
fi
diff -Naur a/sbin/liveiso-find b/sbin/liveiso-find
--- a/sbin/liveiso-find 1970-01-01 10:00:00.000000000 +1000
+++ b/sbin/liveiso-find 2012-03-20 06:35:30.634222494 +1100
@@ -0,0 +1,24 @@
+#!/bin/sh
+# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
+# ex: ts=8 sw=4 sts=4 et filetype=sh
+
+[ -z "$1" ] && exit 1
+isopath="$1"
+
+# try all the block devices under /dev and attempt to mount them
+
+mkdir -p /isodevice
+ls -l /dev/ | grep '^b' | grep -o '[a-z0-9-]\+$' | grep -v loop | while read device; do
+ device_fs=`blkid -s TYPE -o value "/dev/$device"`
+ [ -z "$device_fs" ] && continue
+ if mount -t "$device_fs" -o ro "/dev/$device" /isodevice; then
+ if [ -f "/isodevice$isopath" ]; then
+ losetup /dev/loop5 "/isodevice$isopath"
+ echo /dev/loop5
+ exit 0
+ else
+ umount /isodevice
+ fi
+ fi
+done
+rmdir /isodevice 2>/dev/null
EOF
chmod +x "$INITDIR/sbin/liveiso-find"
test -z "$OUTPATH" && OUTPATH="initrd-fromiso"
(
cd "$INITDIR";
echo './sbin/liveiso-find
./lib/dracut/hooks/pre-udev/30-dmsquash-liveiso-genrules.sh' |
cpio -o -H newc -A -F "$TMPHOLD"/initrd
)
gzip -9 < "$TMPHOLD"/initrd > "$OUTPATH"
rm -Rf "$INITDIR"
rm "$TMPHOLD"/initrd
rmdir "$TMPHOLD"
fi
rmdir "$ISOMNT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment