Create grub2 image for qemu
Posted: Sun Dec 18, 2011 12:19 pm
Dear i try to create a script for create grub2 image for boot with qemu
but it fail for grub-mkimage
and i found this missing file with
I do :
the bash script:
but it fail for grub-mkimage
Code: Select all
./grub-1.99/grub-mkimage: error: cannot open /usr/lib//i386-pc/moddep.lst.
Code: Select all
$ locate i386-pc/moddep.ls
/usr/lib/grub2/i386-pc/moddep.lst
Code: Select all
su -c './create_img.sh -i /tmp/grub2.img -m /mnt/floppy/ -r 5.3M '
Code: Select all
#!/usr/bin/env bash
function usage(){
cat << EOF
usage: $0 options
This script create a grub2 image for qemu/bochs.
OPTIONS:
-h Show this message
-v verbose could be use twice time as -v -v for increase verbosity
-q No verbosity
-i image path
-m mount point
-r require size for create image
-s skip grub build
-u grub url to downloading
EOF
}
[ `id -u` -ne 0 ] && echo -e "\033[1;4;31mMust be root\033[0;0m" && exit 1
declare -r loopdevice="$(losetup -f)"
declare -r fs="ext2"
declare STRING grub_url="ftp://ftp.gnu.org/gnu/grub/grub-1.99.tar.xz"
declare -i skip_grub=1
declare -i verbose=1
declare -i -r byte_per_sector=512
declare require_size=0.0
declare STRING img_path=""
declare STRING mount_path=""
declare STRING lang_save=$LANG
LANG=C
while getopts "hvqi:m:r:s" OPTION
do
case $OPTION in
h)
usage
exit 1
;;
i)
img_path="${OPTARG}"
;;
m)
mount_path="${OPTARG}"
;;
q)
verbose=0
;;
r)
lastchr=${OPTARG#${OPTARG%?}}
case $lastchr in
[[:digit:]])
require_size=${OPTARG}
;;
"k")
;&
"K")
require_size=$(printf %0.f $(echo "scale=9; ${OPTARG: 0 : ${#OPTARG}-1} * 1024"| bc))
;;
"m")
;&
"M")
require_size=$(printf %0.f $(echo "scale=9;${OPTARG: 0 : ${#OPTARG}-1} * 1024 * 1024"| bc))
;;
"g")
;&
"G")
require_size=$(printf %0.f $(echo "scale=9;${OPTARG: 0 : ${#OPTARG}-1} * 1024 * 1024 * 1024"| bc))
;;
"t")
;&
"T")
require_size=$(printf %0.f $(echo "scale=9;${OPTARG: 0 : ${#OPTARG}-1} * 1024 * 1024 * 1024 * 1024"| bc))
;;
*)
echo "Invalid size ${OPTARG}"
exit 1
;;
esac
;;
s)
skip_grub=0
;;
v)
let verbose++
;;
u)
grub_url="${OPTARG}"
;;
?)
usage
LANG=$lang_save
losetup -d $loopdevice
exit 1
;;
esac
done
if [[ "${img_path}" == "" || "${mount_path}" == "" ]]; then
echo -e "\033[31mYou need at least set image path and mount path \033[0;0m"
usage
LANG=$lang_save
losetup -d $loopdevice
exit 1
fi
############### BUILD GRUB
if [[ $skip_grub -ne 0 ]]; then
if [[ $verbose -ge 1 ]]; then
echo -e "\033[31mgrub 2 downloading\033[0;0m"
fi
curl "${grub_url}" -o grub2.tar.xz
if [[ $verbose -ge 1 ]]; then
echo -e "\033[31mExtract grub 2\033[0;0m"
fi
tar xvf grub2.tar.xz grub-1.99
cd grub-1.99
if [[ $verbose -ge 1 ]]; then
echo -e "\033[31mBuild grub 2\033[0;0m"
fi
./autogen
./configure --with-platform=efi --target=i686 --program-transform-name=s,grub,grub2 --sbindir=/sbin --prefix=/usr --sysconfdir=/etc
make -j
cd -
fi
############### CREATE IMAGE
if [[ ! -d "${mount_path}" ]]; then
mkdir -p "${mount_path}"
fi
if [[ $verbose -ge 1 ]]; then
echo -e "\033[31mCreate image file to: ${img_path}\033[0;0m"
fi
num_sectors=$(printf %0.f $(echo "scale=9; $require_size/ $byte_per_sector"| bc))
count_size=$(printf %0.f $(echo "$num_sectors * $byte_per_sector"| bc))
if [[ count_size -lt $require_size ]]; then
let num_sector++
count_size=$(printf %0.f $(echo "$num_sectors * $byte_per_sector"| bc))
fi
if [[ $verbose -ge 2 ]]; then
echo -e "\033[31m\tTotal size: $num_sectors sectors in $count_size bytes\033[0;0m"
fi
dd if=/dev/zero of="${img_path}" bs=$byte_per_sector count=$num_sectors # use qemu-img or dd
#qemu-img create "${img_path}" 5.3M
if [[ $? -ne 0 ]]; then
echo -e "\033[1;4;31mdd fail\033[0;0m"
LANG=$lang_save
losetup -d $loopdevice
exit 1
fi
if [[ $verbose -ge 2 ]]; then
echo -e "\033[31mSetup a loopback device \033[0;0m"
fi
losetup $loopdevice "${img_path}"
if [[ $verbose -ge 2 ]]; then
echo -e "\033[31mCreate a filesystem\033[0;0m"
fi
#mkfs -V -t $fs $loopdevice
mke2fs -m 0 ${img_path}
############### Create GRUB image
mount -t ext2 -o loop,rw ${loopdevice} ${mount_path}
if [[ ! -d "${mount_path}/efi/grub" ]]; then
mkdir -p "${mount_path}/efi/grub"
fi
./grub-1.99/grub-mkimage -o "${mount_path}/efi/grub/grub.efi boot linux part_gpt fat ext2 normal sh configfile lspci ls reboot datetime loadenv search lvm help multiboot elf efi_gop efi_uga" -O i386-pc
if [[ $? -ne 0 ]]; then
echo -e "\033[1;4;31mgrub-mkimage fail\033[0;0m"
LANG=$lang_save
umount "${mount_path}"
losetup -d $loopdevice
exit 1
fi
############### Install GRUB
if [[ $verbose -ge 1 ]]; then
echo -e "\033[31mInstall in mounted directory\033[0;0m"
fi
cd grub-1.99
ls
make -j DESTDIR="${mount_path}" install
cd -
cp -r etc/* "${mount_path}/etc"
if [[ $verbose -ge 2 ]]; then
echo -e "\033[31mUnmount the image\033[0;0m"
fi
umount "${mount_path}"
if [[ $verbose -ge 2 ]]; then
echo -e "\033[31mIFinally delete the loopback device.\033[0;0m"
fi
losetup -d $loopdevice
echo -e "Now run:\033[34m qemu -fda ${img_path}\033[0;0m"
echo -e "For see image content:\033[34m mount -t ext2 -o loop,rw ${img_path} ${mount_path}\033[0;0m"
LANG=$lang_save