toaruos/util/create-image.sh

96 lines
2.1 KiB
Bash
Raw Normal View History

2012-10-17 09:23:16 +04:00
#!/bin/bash
if [[ $EUID -ne 0 ]]; then
echo -e "\033[1;31mYou're going to need to run this as root\033[0m" 1>&2
echo "Additionally, verify that /dev/loop4 is available and that" 1>&2
echo "/mnt is available for mounting; otherwise, modify the script" 1>&2
echo "to use alternative loop devices or mount points as needed." 1>&2
exit 1
fi
2012-10-17 09:27:38 +04:00
if [[ $# -lt 1 ]]; then
echo "I need a path to a compiled とあるOS source directory as an argument, try again." 1>&2
exit 1
fi
2012-10-17 09:23:16 +04:00
DISK=toaru-disk.img
SRCDIR=$1
BOOT=./boot
2015-12-18 03:43:51 +03:00
SIZE=1G
2012-10-17 09:23:16 +04:00
echo "Please select a disk size."
read -p "l for 1GB, s for 256MB: "
if [ "$REPLY" == "small" ] ; then
2015-12-18 03:43:51 +03:00
SIZE=256M
fi
echo "I will create partitioned, ext2 disk image of size $SIZE x 4KB at $DISK from files in $SRCDIR as well as boot scripts in $BOOT"
2012-10-17 09:23:16 +04:00
read -p "Is this correct? (Y/n)"
if [ "$REPLY" == "n" ] ; then
echo "Oh, okay, never mind then."
exit
fi
2013-02-23 02:04:27 +04:00
type kpartx >/dev/null 2>&1 || { echo "Trying to install kpartx..."; apt-get install kpartx; }
2012-10-17 09:23:16 +04:00
# Create a 1GiB blank disk image.
2015-12-18 03:43:51 +03:00
dd if=/dev/zero of=$DISK bs=$SIZE count=1
2012-10-17 09:23:16 +04:00
echo "Partitioning..."
2015-12-18 03:43:51 +03:00
cat parted.conf | parted $DISK
2012-10-17 09:23:16 +04:00
echo "Done partition."
2015-03-31 05:51:34 +03:00
2012-10-17 09:23:16 +04:00
# Here's where we need to be root.
2015-12-18 00:56:30 +03:00
LOOPRAW=`losetup -f`
losetup $LOOPRAW $DISK
TMP=`kpartx -av $DISK`
TMP2=${TMP/add map /}
LOOP=${TMP2%%p1 *}
LOOPDEV=/dev/${LOOP}
LOOPMAP=/dev/mapper/${LOOP}p1
if [ ! -e $LOOPDEV ] ; then
echo "Bailing! $LOOPDEV is not valid"
exit
fi
2012-10-17 09:23:16 +04:00
2015-12-18 00:56:30 +03:00
if [ ! -e $LOOPMAP ] ; then
echo "Bailing! $LOOPMAP is not valid"
exit
fi
2012-10-17 09:23:16 +04:00
2015-12-18 00:56:30 +03:00
mkfs.ext2 ${LOOPMAP}
2012-10-17 09:23:16 +04:00
2015-12-18 00:56:30 +03:00
mount ${LOOPMAP} /mnt
2012-10-17 09:23:16 +04:00
echo "Installing main files."
cp -r $SRCDIR/hdd/* /mnt/
echo "Installing boot files."
2012-10-20 06:38:37 +04:00
mkdir -p /mnt/boot
cp -r $BOOT/* /mnt/boot/
2012-10-17 09:23:16 +04:00
echo "Installing kernel."
cp -r $SRCDIR/toaruos-kernel /mnt/boot/
echo "Installing grub."
2015-12-18 00:56:30 +03:00
grub-install --target=i386-pc --boot-directory=/mnt/boot $LOOPRAW
2012-10-17 09:23:16 +04:00
echo "Cleaning up"
umount /mnt
2015-12-18 00:56:30 +03:00
kpartx -d ${LOOPMAP}
2015-12-18 03:43:51 +03:00
dmsetup remove ${LOOPMAP}
2015-12-18 00:56:30 +03:00
losetup -d ${LOOPDEV}
losetup -d ${LOOPRAW}
2012-10-17 09:23:16 +04:00
2013-02-23 02:04:27 +04:00
if [ -n "$SUDO_USER" ] ; then
echo "Reassigning permissions on disk image to $SUDO_USER"
chown $SUDO_USER:$SUDO_USER $DISK
fi
2012-10-17 09:23:16 +04:00
echo "Done. You can boot the disk image with qemu now."