2008-10-20 22:39:25 +02:00
|
|
|
#!/bin/bash
|
|
|
|
# based on mkusbimg, modified to use syslinux
|
|
|
|
# by Tobias Powalowski <tpowa@archlinux.org>
|
|
|
|
#
|
|
|
|
# mkusbimg - creates a bootable disk image
|
|
|
|
# Copyright (C) 2008 Simo Leone <simo@archlinux.org>
|
|
|
|
#
|
|
|
|
# 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# usage(exitvalue)
|
|
|
|
# outputs a usage message and exits with value
|
2011-07-24 09:23:25 +02:00
|
|
|
|
|
|
|
APPNAME="$(basename "${0}")"
|
|
|
|
|
|
|
|
_usage()
|
2008-10-20 22:39:25 +02:00
|
|
|
{
|
|
|
|
echo "usage: ${APPNAME} <imageroot> <imagefile>"
|
2011-07-24 09:23:25 +02:00
|
|
|
exit ${1}
|
2008-10-20 22:39:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
##################################################
|
|
|
|
|
|
|
|
if [ $# -ne 2 ]; then
|
2011-07-24 09:23:25 +02:00
|
|
|
_usage 1
|
2008-10-20 22:39:25 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
DISKIMG="${2}"
|
|
|
|
IMGROOT="${1}"
|
|
|
|
TMPDIR=$(mktemp -d)
|
2011-04-17 20:40:05 +02:00
|
|
|
TMPDIR2=$(mktemp -d)
|
2008-10-20 22:39:25 +02:00
|
|
|
FSIMG=$(mktemp)
|
|
|
|
|
|
|
|
# ext2 overhead's upper bound is 6%
|
|
|
|
# empirically tested up to 1GB
|
|
|
|
rootsize=$(du -bs ${IMGROOT}|cut -f1)
|
|
|
|
IMGSZ=$(( (${rootsize}*102)/100/512 + 1)) # image size in sectors
|
|
|
|
|
|
|
|
# create the filesystem image file
|
2011-03-16 20:34:33 +01:00
|
|
|
dd if=/dev/zero of="${FSIMG}" bs=512 count="${IMGSZ}"
|
2008-10-20 22:39:25 +02:00
|
|
|
|
|
|
|
# create a filesystem on the image
|
2011-03-16 20:34:33 +01:00
|
|
|
mkfs.vfat -S 512 -F32 -n "ARCHBOOT" "${FSIMG}"
|
2008-10-20 22:39:25 +02:00
|
|
|
|
|
|
|
# mount the filesystem and copy data
|
2011-07-29 09:18:51 +02:00
|
|
|
if ! [ "$(grep ^loop /proc/modules)" ]; then
|
|
|
|
modprobe -q loop || echo "Your hostsystem has a different kernel version installed, please load loop module first on hostsystem!"
|
|
|
|
fi
|
2011-07-24 09:23:25 +02:00
|
|
|
LOOP_DEVICE="$(losetup --show --find "${FSIMG}")"
|
2011-03-16 20:34:33 +01:00
|
|
|
mount -o rw,users -t vfat "${LOOP_DEVICE}" "${TMPDIR}"
|
2011-04-17 20:40:05 +02:00
|
|
|
|
2011-07-24 09:23:25 +02:00
|
|
|
if [ ! -e "${IMGROOT}/efi/boot/bootx64.efi" ]
|
|
|
|
then
|
|
|
|
|
|
|
|
LOOP_DEVICE2="$(losetup --show --find "${IMGROOT}/efi/grub2/grub2_efi.bin")"
|
|
|
|
mount -o ro,users -t vfat "${LOOP_DEVICE2}" "${TMPDIR2}"
|
|
|
|
cp "${TMPDIR2}/bootx64.efi" "${IMGROOT}/efi/boot/bootx64.efi"
|
|
|
|
|
|
|
|
umount "${TMPDIR2}"
|
|
|
|
losetup --detach "${LOOP_DEVICE2}"
|
|
|
|
|
|
|
|
rm -rf "${TMPDIR2}"
|
2011-07-29 09:18:51 +02:00
|
|
|
fi
|
2011-04-17 20:40:05 +02:00
|
|
|
|
2011-03-16 20:34:33 +01:00
|
|
|
cp -r "${IMGROOT}"/* "${TMPDIR}"
|
2008-10-20 22:39:25 +02:00
|
|
|
|
|
|
|
# unmount filesystem
|
2011-03-16 20:34:33 +01:00
|
|
|
umount "${TMPDIR}"
|
2011-04-17 20:40:05 +02:00
|
|
|
losetup --detach "${LOOP_DEVICE}"
|
|
|
|
|
2011-03-16 20:34:33 +01:00
|
|
|
cat "${FSIMG}" > "${DISKIMG}"
|
2008-10-20 22:39:25 +02:00
|
|
|
|
|
|
|
# install syslinux on the image
|
2011-03-16 20:34:33 +01:00
|
|
|
syslinux "${DISKIMG}"
|
2008-10-20 22:39:25 +02:00
|
|
|
|
|
|
|
# all done :)
|
2011-07-24 09:23:25 +02:00
|
|
|
rm -rf "${TMPDIR}"
|
|
|
|
rm -f "${FSIMG}"
|