<commit_before>#!/bin/bash

# Convert kernel/rootfs images generated by build-yocto-qemux86
# into a .VDI image suitable to be executed from VirtualBox

# BIG FAT WARNING
# A few dangerous commands are executed as sudo and may destroy your host filesystem if buggy.
# USE AT YOUR OWN RISK - YOU HAVE BEEN WARNED!!!

# Prerequisites:
#    qemu-img
#    parted
#    kpartx
#    grub-install
#    sudo

# CONFIGURATION ITEMS

TOPDIR=$PWD/tmp/build-gemini-5.0.2-qemux86

MACHINE=qemux86
FSTYPE=tar.bz2
KERNEL=$TOPDIR/tmp/deploy/images/$MACHINE/bzImage-$MACHINE.bin
ROOTFS=$TOPDIR/tmp/deploy/images/$MACHINE/gemini-image-$MACHINE.$FSTYPE

RAW_IMAGE=$PWD/test.raw
VDI_IMAGE=$PWD/test.vdi

MNT_ROOTFS=/tmp/rootfs

DISK_SIZE=256M

set -e
#set -x

# Create QEMU image
# See http://en.wikibooks.org/wiki/QEMU/Images

qemu-img create -f raw $RAW_IMAGE $DISK_SIZE

# Create partition table and partitions on RAW_IMAGE
parted $RAW_IMAGE mklabel msdos
parted $RAW_IMAGE print free
parted $RAW_IMAGE mkpart primary ext3 1 220
parted $RAW_IMAGE set 1 boot on
parted $RAW_IMAGE print free

#echo "DBG: Checking $RAW_IMAGE:"
#sfdisk -l $RAW_IMAGE
#fdisk -l $RAW_IMAGE

TMPFILE1=/tmp/kpartx-$$.tmp

LOOP_IMAGE=`sudo losetup -f --show $RAW_IMAGE`

TEMP_MAJ=`ls -l $LOOP_IMAGE | cut -d ' ' -f 5`

MAJOR=${TEMP_MAJ%?} #remove comma from major
MINOR=`ls -l $LOOP_IMAGE | cut -d ' ' -f 6`
SIZE=$((`ls -l $RAW_IMAGE | cut -d ' ' -f 5`/512))

echo "0 $SIZE linear $MAJOR:$MINOR 0" | sudo dmsetup create hda # this creates /dev/mapper/hda


sudo kpartx -v -a /dev/mapper/hda >$TMPFILE1

echo "DBG: Contents of $TMPFILE1:"
cat $TMPFILE1

BLOCKDEV=`cut -d' ' -f8 $TMPFILE1`
ROOTPART=/dev/mapper/`cut -d' ' -f3 $TMPFILE1`
echo "DBG: BLOCKDEV=$BLOCKDEV"
echo "DBG: ROOTPART=$ROOTPART"

#echo "DBG: Checking $BLOCKDEV:"
#sudo fdisk -l $BLOCKDEV

sleep 1 #wait for node creation

sudo mkfs -t ext3 -L "GENIVI" $ROOTPART

mkdir -p $MNT_ROOTFS
#sudo mount -o loop $ROOTPART $MNT_ROOTFS
sudo mount $ROOTPART $MNT_ROOTFS

#TMPFILE2=/tmp/losetup-$$.tmp

#sudo losetup -av >$TMPFILE2

#echo "DBG: Contents of $TMPFILE2:"
#cat $TMPFILE2

# TODO: Copy kernel to $MNT_ROOTFS/boot
sudo install -m755 -d $MNT_ROOTFS/boot
sudo install -m644 -o 0 -v $KERNEL $MNT_ROOTFS/boot

# Extract rootfs
sudo tar xvfj $ROOTFS -C $MNT_ROOTFS

#echo "TODO:"
# TODO: Create grub.cfg to MNT_ROOTFS/boot/grub
# grub-mkimage ???

# Create simple /boot/grub/grub.cfg on $ROOTPART
# See http://www.linuxfromscratch.org/lfs/view/development/chapter08/grub.html

sudo install -m 755 -d $MNT_ROOTFS/boot/grub

REAL_DEVICE=`readlink -f /dev/mapper/hda`
TMPFILE4=device.map
cat >$TMPFILE4 <<__END__
# Begin /boot/grub/device.map

(hd0) $REAL_DEVICE
#(hd0,msdos1) $BLOCKDEV
#(hd0,1) $ROOTPART
__END__
sudo install -m644 -o 0 -v $TMPFILE4 $MNT_ROOTFS/boot/grub/device.map

echo "DBG: Installing grub"

sudo grub-install --root-directory=$MNT_ROOTFS $REAL_DEVICE


TMPFILE3=/tmp/grubcfg-$$.tmp
cat > $TMPFILE3 <<END
# Begin /boot/grub/grub.cfg
set default=0
set timeout=1

insmod ext2
set root=(hd0,1)

menuentry "Yocto-GENIVI, Linux" {
        linux   /boot/bzImage-qemux86.bin root=/dev/hda1
}

#menuentry "GNU/Linux, Linux 3.13.6-lfs-SVN-20140404" {
#        linux   /boot/vmlinuz-3.13.6-lfs-SVN-20140404 root=/dev/sda2 ro
#}
END

sudo install -m644 -o 0 -v $TMPFILE3 $MNT_ROOTFS/boot/grub/grub.cfg

echo "DBG: Contents of $MNT_ROOTFS:"
ls -la $MNT_ROOTFS

if [ -e $MNT_ROOTFS/boot ]; then
    echo "DBG: Contents of $MNT_ROOTFS/boot:"
    #du -sh $MNT_ROOTFS/boot
    #ls -la $MNT_ROOTFS/boot
    ls -laR $MNT_ROOTFS/boot
fi

if [ -e $MNT_ROOTFS/boot/grub/device.map ]; then
    echo "DBG: Contents of $MNT_ROOTFS/boot/grub/device.map:"
    cat $MNT_ROOTFS/boot/grub/device.map
fi

echo "DBG: Disk space on $MNT_ROOTFS:"
df -h $MNT_ROOTFS

echo "DBG: Cleanup"

sudo umount $MNT_ROOTFS

sudo kpartx -d $BLOCKDEV

sudo dmsetup remove hda

sudo losetup -d $LOOP_IMAGE

rm -f $TMPFILE1
rm -f $TMPFILE2
rm -f $TMPFILE3
rm -f $TMPFILE4

#echo "DBG: Checking $RAW_IMAGE:"
#parted $RAW_IMAGE print free

qemu-img convert -f raw -O vdi $RAW_IMAGE $VDI_IMAGE

# TODO: Test: Run QEMU against VDI_IMAGE
echo "TODO:" qemu-system-i386 -hda $RAW_IMAGE

# TODO: Test: Run VirtualBox against VDI_IMAGE<commit_msg>align script with upstream<commit_after>#!/bin/bash

# Convert kernel/rootfs images generated by build-yocto-qemux86
# into a .VDI image suitable to be executed from VirtualBox

# BIG FAT WARNING
# A few dangerous commands are executed as sudo and may destroy your host filesystem if buggy.
# USE AT YOUR OWN RISK - YOU HAVE BEEN WARNED!!!

# Prerequisites:
#    qemu-img
#    parted
#    kpartx
#    grub-install
#    sudo

# CONFIGURATION ITEMS

TOPDIR=$PWD/tmp/build-gemini-5.0.2-qemux86

MACHINE=qemux86
FSTYPE=tar.bz2
KERNEL=$TOPDIR/tmp/deploy/images/$MACHINE/bzImage-$MACHINE.bin
ROOTFS=$TOPDIR/tmp/deploy/images/$MACHINE/gemini-image-$MACHINE.$FSTYPE

RAW_IMAGE=$PWD/test.raw
VDI_IMAGE=$PWD/test.vdi

MNT_ROOTFS=/tmp/rootfs

DISK_SIZE=256M

partmgr_use_fdisk=0
partmgr_use_parted=1

bootldr_use_grub2=1
bootldr_use_syslinux=0

echo "DBG: partmgr_use_fdisk=$partmgr_use_fdisk; partmgr_use_parted=$partmgr_use_parted"
echo "DBG: bootldr_use_grub2=$bootldr_use_grub2; bootldr_use_syslinux=$bootldr_use_syslinux"

set -e
#set -x

# Create QEMU image
# See http://en.wikibooks.org/wiki/QEMU/Images


qemu-img create -f raw $RAW_IMAGE $DISK_SIZE


if [ $partmgr_use_fdisk != 0 ]; then
echo "DBG: Use fdisk to create partition table and partition(s) on RAW_IMAGE"
# Disk geometry: 400 cylinders * 16 heads * 63 sec/track * 512 byte/sec = ???
fdisk $RAW_IMAGE <<END
x
c
400
h
16
s
63
r
n
p
1


a
1
p
w
END
fi	# [ $partmgr_use_fdisk != 0 ]

if [ $partmgr_use_parted != 0 ]; then
echo "DBG: Use parted to create partition table and partition(s) on RAW_IMAGE"
parted $RAW_IMAGE mklabel msdos
parted $RAW_IMAGE print free
parted $RAW_IMAGE mkpart primary ext3 1 200
parted $RAW_IMAGE set 1 boot on
fi	# [ $partmgr_use_parted != 0 ]


echo "DBG: Checking $RAW_IMAGE:"
#sfdisk -l $RAW_IMAGE
#fdisk -l $RAW_IMAGE
#parted $RAW_IMAGE print free

TMPFILE1=/tmp/kpartx-$$.tmp

LOOP_IMAGE=`sudo losetup -f --show $RAW_IMAGE`

TEMP_MAJ=`ls -l $LOOP_IMAGE | cut -d ' ' -f 5`

MAJOR=${TEMP_MAJ%?} #remove comma from major
MINOR=`ls -l $LOOP_IMAGE | cut -d ' ' -f 6`
SIZE=$((`ls -l $RAW_IMAGE | cut -d ' ' -f 5`/512))

echo "0 $SIZE linear $MAJOR:$MINOR 0" | sudo dmsetup create hda # this creates /dev/mapper/hda


sudo kpartx -v -a /dev/mapper/hda >$TMPFILE1

echo "DBG: Contents of $TMPFILE1:"
cat $TMPFILE1

BLOCKDEV=`cut -d' ' -f8 $TMPFILE1`
ROOTPART=/dev/mapper/`cut -d' ' -f3 $TMPFILE1`
echo "DBG: BLOCKDEV=$BLOCKDEV"
echo "DBG: ROOTPART=$ROOTPART"

#echo "DBG: Checking $BLOCKDEV:"
#sudo fdisk -l $BLOCKDEV

sleep 1 #wait for node creation

sudo mkfs -t ext3 -L "GENIVI" $ROOTPART

mkdir -p $MNT_ROOTFS
sudo mount $ROOTPART $MNT_ROOTFS

#TMPFILE2=/tmp/losetup-$$.tmp

#sudo losetup -av >$TMPFILE2

#echo "DBG: Contents of $TMPFILE2:"
#cat $TMPFILE2

# Copy kernel to $MNT_ROOTFS/boot
sudo install -m755 -d $MNT_ROOTFS/boot
sudo install -m644 -o 0 -v $KERNEL $MNT_ROOTFS/boot

# Extract rootfs
sudo tar xvfj $ROOTFS -C $MNT_ROOTFS

#echo "DBG: Listing all disks IDs"
#ls -la /dev/disk/by-id/

#echo "DBG: Listing all disks labels"
#ls -la /dev/disk/by-label/


sudo install -m 755 -d $MNT_ROOTFS/boot/grub

REAL_DEVICE=`readlink -f /dev/mapper/hda`
TMPFILE4=device.map
cat >$TMPFILE4 <<__END__
# Begin /boot/grub/device.map

(hd0) $REAL_DEVICE
#(hd0,msdos1) $BLOCKDEV
#(hd0,1) $ROOTPART
__END__
sudo install -m644 -o 0 -v $TMPFILE4 $MNT_ROOTFS/boot/grub/device.map

echo "DBG: Installing grub"

sudo grub-install --root-directory=$MNT_ROOTFS $REAL_DEVICE


TMPFILE3=/tmp/grubcfg-$$.tmp
cat > $TMPFILE3 <<END
# Begin /boot/grub/grub.cfg
set default=0
set timeout=1

insmod ext2
#set prefix=(hd0,1)/boot/grub
set root=(hd0,1)

menuentry "Yocto-GENIVI, Linux" {
        linux   /boot/bzImage-qemux86.bin root=/dev/hda1
}

#menuentry "GNU/Linux, Linux 3.13.6-lfs-SVN-20140404" {
#        linux   /boot/vmlinuz-3.13.6-lfs-SVN-20140404 root=/dev/sda2 ro
#}
END

sudo install -m644 -o 0 -v $TMPFILE3 $MNT_ROOTFS/boot/grub/grub.cfg

echo "DBG: Contents of $MNT_ROOTFS:"
ls -la $MNT_ROOTFS

if [ -e $MNT_ROOTFS/boot ]; then
    echo "DBG: Contents of $MNT_ROOTFS/boot:"
    #du -sh $MNT_ROOTFS/boot
    #ls -la $MNT_ROOTFS/boot
    ls -laR $MNT_ROOTFS/boot
fi

if [ -e $MNT_ROOTFS/boot/grub/device.map ]; then
    echo "DBG: Contents of $MNT_ROOTFS/boot/grub/device.map:"
    cat $MNT_ROOTFS/boot/grub/device.map
fi

echo "DBG: Disk space on $MNT_ROOTFS:"
df -h $MNT_ROOTFS

echo "DBG: Cleanup"

sudo umount $MNT_ROOTFS

sudo kpartx -d $BLOCKDEV

sudo dmsetup remove hda

sudo losetup -d $LOOP_IMAGE

rm -f $TMPFILE1
rm -f $TMPFILE2
rm -f $TMPFILE3
rm -f $TMPFILE4

#echo "DBG: Checking $RAW_IMAGE:"
#parted $RAW_IMAGE print free

qemu-img convert -f raw -O vdi $RAW_IMAGE $VDI_IMAGE

## TODO: Test: Run QEMU against VDI_IMAGE
#echo "TODO:" qemu-system-i386 -hda $RAW_IMAGE

# TODO: Test: Run VirtualBox against VDI_IMAGE
{'fraction_non_alphanumeric': 0.12703510909292282, 'fraction_numerical': 0.028131576032783254, 'mean_word_length': 4.036252091466816, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '103910', 'n_tokens_mistral': 4073, 'n_tokens_neox': 3832, 'n_words': 1000}