From 97ed563f00fc6cd4be3f91041fe455529bc1c89c Mon Sep 17 00:00:00 2001 From: Simo Leone Date: Sun, 30 Mar 2008 02:49:47 -0500 Subject: Added USB image creation script mkusbimg creates bootable raw disk images that can be written to USB devices. It's intended to be on the same functional level as mkisofs. Signed-off-by: Simo Leone --- mkusbimg | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100755 mkusbimg diff --git a/mkusbimg b/mkusbimg new file mode 100755 index 0000000..6a41f4c --- /dev/null +++ b/mkusbimg @@ -0,0 +1,98 @@ +#!/bin/bash +# +# mkusbimg - creates a bootable disk image +# Copyright (C) 2008 Simo Leone +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# next_avail_loop() +# prints the next available loopback device +# returns 0 on success +# 1 on failure +# XXX: this is only necessary because +# the cryptoloop patch for losetup +# destroys losetup -f +next_avail_loop() +{ + for i in /dev/loop/*; do + echo `losetup -a|cut -d':' -f1` | grep -q $i + if [ $? -eq 1 ]; then + echo $i + return 0 + fi + done + return 1 +} + +# usage(exitvalue) +# outputs a usage message and exits with value +APPNAME=$(basename "${0}") +usage() +{ + echo "usage: ${APPNAME} " + exit $1 +} + +################################################## + +if [ $# -ne 2 ]; then + usage 1 +fi + +IMG="${2}" +IMGROOT="${1}" +LOOPDEV=$(next_avail_loop) +TMPDIR=$(mktemp -d) + +# TODO: there are better ways to do this +# than adding 5MB to the rootsize +# IMGSZ >= filesystem overhead + rootsize + 512bytes +# must hold or there will be insufficient space +IMGSZ=$(( $(du -ms ${IMGROOT}|cut -f1) + 5 )) + +# create the image file +dd if=/dev/zero of="$IMG" bs=1M count="$IMGSZ" + +# loop mount the disk image +losetup "$LOOPDEV" "$IMG" + +# create a partition table +# if this looks like voodoo, it's because it is +echo "63,,,*,"|sfdisk -uS "$LOOPDEV" + +# loop mount the partition we just made +# that magic number (offset in bytes to first partition) is more voodoo +losetup -d "$LOOPDEV" +losetup -o32256 "$LOOPDEV" "$IMG" + +# create a filesystem on our partition +mke2fs -m 0 "$LOOPDEV" + +# mount the filesystem and copy data +mount "$LOOPDEV" "$TMPDIR" +cp -a "$IMGROOT"/* "$TMPDIR" + +# unmount filesystem and loopback +umount "$TMPDIR" +losetup -d "$LOOPDEV" + +# install grub on the image +grub --no-floppy --batch << EOF +device (hd0) $IMG +root (hd0,0) +setup (hd0) +EOF + +# all done :) +rm -fr "$TMPDIR" -- cgit v1.2.3-54-g00ecf