added script for Grub2 image creation;
[MuString.git] / scripts / mkimage.sh
blob2db3f3692402603cc95830c6a7c558c97b4df259
1 #!/bin/bash
3 APP=`basename $0`
4 PT=mbr
6 usage()
8 echo "Usage: ${APP} <image_file> <kernel_elf>"
9 exit $*
12 TMP=`getopt -o hp:k:i: --long help,partition:,kernel:,image: -n ${APP} -- "$@"`
13 [ $? != 0 ] && usage 1
14 eval set -- "$TMP"
15 while true ; do
16 case "$1" in
17 -h|--help) usage 0 ; shift ;;
18 -p|--partition)
19 case "$2" in
20 "gpt"|"mbr") PT=$2 ; shift 2 ;;
21 *) echo "Unsupported partition type '$2'" >&2 ; exit 1 ;;
22 esac ;;
23 -k|--kernel) KERNEL_ELF=$2 ; shift 2 ;;
24 -i|--image) IMAGE_FILE=$2 ; shift 2 ;;
25 --) shift ; break ;;
26 *) echo "Internal error!" ; exit 1 ;;
27 esac
28 done
30 [ -z ${IMAGE_FILE} ] &&
32 [ $# -gt 0 ] || usage 1
33 IMAGE_FILE=$1
34 shift
37 [ -z ${KERNEL_ELF} ] &&
39 [ $# -gt 0 ] || usage 1
40 KERNEL_ELF=$1
41 shift
44 dd if=/dev/zero of=${IMAGE_FILE} bs=8192 count=65536
45 case "${PT}" in
46 "mbr")
47 sfdisk -D ${IMAGE_FILE} <<-EOF
48 ,,L,*
49 EOF
50 MODS="part_msdos" ;
52 "gpt")
53 sgdisk -a 1 -n 0:0:-4096K -a 1 -n 0:0:0 -t 2:ef02 ${IMAGE_FILE} ;
54 # sgdisk -h 1 ${IMAGE_FILE} ;
55 # sfdisk -A2 ${IMAGE_FILE} ;
56 MODS="part_gpt" ;
58 *) echo "Unexpected partition type '${PT}'" >&2 ; exit 1 ;;
59 esac
61 BLOCK_DEVICE=`losetup --partscan --find --show ${IMAGE_FILE}`
62 mke2fs -b 1024 -t ext2 ${BLOCK_DEVICE}p1
63 MOUNT_POINT=`mktemp --directory`
64 mount -t ext2 ${BLOCK_DEVICE}p1 ${MOUNT_POINT}
65 grub2-install --boot-directory=${MOUNT_POINT}/boot --modules="${MODS} ext2" ${BLOCK_DEVICE}
66 #grub2-install --target=i386-multiboot --boot-directory=${MOUNT_POINT}/boot --force --modules="${MODS} ext2 multiboot multiboot2" --debug ${BLOCK_DEVICE}
67 #grub2-mkimage -O i386-multiboot -o ${MOUNT_POINT}/boot/grub2/i386-multiboot/core.img -v
68 #grub2-bios-setup -s -m /dev/null -d /usr/lib/grub -b i386-pc/boot.img -c ../../..${MOUNT_POINT}/boot/grub2/i386-multiboot/core.img -v ${BLOCK_DEVICE}
69 install ${KERNEL_ELF} ${MOUNT_POINT}/boot/
70 NAME=`basename ${KERNEL_ELF}`
71 cat > ${MOUNT_POINT}/boot/grub2/grub.cfg <<EOF
72 set default=0
73 set timeout=30
74 insmod gfxterm
75 insmod vbe
76 if terminal_output gfxterm ; then true ; else terminal gfxterm; fi
77 set color_normal=black/white
78 set menu_color_normal=green/light-blue
79 set menu_color_highlight=yellow/blue
80 menuentry "MString kernel" {
81 insmod multiboot
82 insmod multiboot2
83 search -n -f /boot/grub2/grub.cfg -s
84 multiboot /boot/${NAME}
85 EOF
86 for MOD do
88 NAME=`basename ${MOD}`
89 install ${MOD} ${MOUNT_POINT}/boot/ && echo -e "\t\tmodule /boot/${NAME}" >> ${MOUNT_POINT}/boot/grub2/grub.cfg
90 } ; done
91 echo -e "\t}\n" >> ${MOUNT_POINT}/boot/grub2/grub.cfg
93 umount ${MOUNT_POINT}
94 losetup --detach ${BLOCK_DEVICE}
95 rmdir ${MOUNT_POINT}
96 exit 0