File mkimage.sh of Package altimagebuild

#!/bin/sh
# This script creates an image from a rootfs
#
# by Bernhard M. Wiedemann
# License: MIT

rbase=$1
r=$rbase/.root
img=$rbase/.img
mnt=/mnt/1
uuid=$(printf "%08x" $(($RANDOM$RANDOM &0xffffffff)))-a4ec-4304-a70f-c549ea829da8
dd if=/dev/zero of=$img bs=1M count=2000
# partitioning rootfs
echo "
n
p



a
p
w
" | fdisk -u $img

# prepare filesystems
modprobe loop
l=$(losetup --find --show $l $img)
kpartx -afsv $l ; sleep 1
l2=/dev/mapper/loop\*
ls -l /dev/mapper/loop*
ls -l $l2
rootdev=${l2}p1
mkfs.ext4 -F -U $uuid -L root ${rootdev}

mkdir $mnt
mount ${rootdev} $mnt || exit 17
rsync -aSHx $r/ $mnt/

pushd $mnt
for m in proc sys dev ; do mount --bind /$m $m ; done
chroot $mnt grub2-install --verbose --target=i386-pc --force ${l}
chroot $mnt grub2-mkconfig | sed -e "s,root=/dev/mapper/loop.p1,root=UUID=$uuid," > $mnt/boot/grub2/grub.cfg
for m in proc sys dev ; do umount $m ; done
popd

# cleanup
umount $mnt
sleep 1
e2fsck -f -y $rootdev
kpartx -dsv $l
losetup -d $l

echo result is in $img
echo now write it to an SD-card with dd bs=1M if=$img of=/dev/sdX
openSUSE Build Service is sponsored by