Created
September 14, 2012 13:33
-
-
Save hydra35/3721930 to your computer and use it in GitHub Desktop.
create grub1 empty image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
tarball=$1 | |
output_img=$2 | |
size=$3 | |
check_tarball() { | |
if [[ ! -s $tarball ]]; then | |
echo "tarball: $tarball not found" >&2 | |
fi | |
} | |
create_partition() { | |
local output_img=$1; shift | |
local loop_dev=$(losetup --show --find $output_img) | |
# 创建Label | |
echo Yes | parted -- $loop_dev mklabel msdos | |
# 获取分区大小 | |
local rc="$(parted -- $loop_dev unit B print free | grep 'Free Space' | tail -1)" | |
local begin=$(echo $rc | awk '{print $1}' | egrep -o '[0-9]+') | |
begin=$((begin+1)) | |
local end=$(echo $rc | awk '{print $2}' | egrep -o '[0-9]+') | |
# 创建文件系统 | |
# TODO:忽略了aligment的提示 | |
echo Ignore | parted -- $loop_dev unit B mkpart primary ext4 $begin $end | |
Create_partition=$loop_dev | |
} | |
mount_tarball() { | |
local tarball=$1; shift | |
local mnt_point=$(uuidgen) | |
mnt_point="/mnt/$mnt_point" | |
[[ -d $mnt_point ]] || mkdir -p $mnt_point | |
#guestmount -a $input_img -i $mnt_point | |
tar -xf $tarball -C $mnt_point | |
Mount_tarball=$mnt_point | |
} | |
mount_output_image() { | |
local device=$1; shift | |
local mnt_point=$(uuidgen) | |
mnt_point="/mnt/$mnt_point" | |
[[ -d $mnt_point ]] || mkdir -p $mnt_point | |
mount $device $mnt_point | |
Mount_output_image=$mnt_point | |
} | |
bailout() { | |
umount $Mount_output_image | |
rmdir $Mount_output_image | |
[[ -d $Mount_tarball ]] && rm -rf $Mount_tarball | |
kpartx -d $Create_partition | |
losetup -d $Create_partition | |
} | |
main() { | |
echo "check tarball" | |
check_tarball | |
if [[ ! -n $size ]]; then | |
echo "output image size not specified" >&2 | |
exit 1 | |
fi | |
echo "create output image: $output_img, size: $size" | |
qemu-img create -f raw $output_img $size 2>/dev/null >&2 | |
echo "create partition on $output_img" | |
create_partition $output_img | |
kpartx -a $Create_partition | |
local short_loop_dev=$(echo $Create_partition | sed -e 's%/dev/\(loop[0-9]*\)%\1%') | |
if [[ $IMG_FILESYSTEM = "ext3" ]]; then | |
mkfs.ext3 -I 128 /dev/mapper/${short_loop_dev}p1 | |
else | |
mkfs.ext4 -E lazy_itable_init=0 /dev/mapper/${short_loop_dev}p1 | |
fi | |
mount_output_image /dev/mapper/${short_loop_dev}p1 | |
mount_tarball $tarball | |
echo "mount tarball on [$Mount_tarball]" | |
echo "mount output image on [$Mount_output_image]" | |
echo "install grub1 to $output_img" | |
bash -x ./install_grub1.sh $output_img $Mount_output_image $Mount_tarball | |
bailout | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment