I'm doing a project that needs to generate a vm image file which will then be used as qemu bootable disk image. Previously, our product is like an modified linux system and is made into a USB installation drive and then boot and install into an bare metal machine. But now we want to get rid of the hardware and run it in virtual machines, that's why we need an image file.
Instead of using the existing USB drive to install the system in qemu then shutdown the vm and get the image, we were asked to make an out-of-box image file directly, and skip all the booting and installing on real machine or virtual machine but still get a installed image, so that we can just deliver this image and people can load this image as a ready-to-use virtual machine image.
But during all the procedure, I can NOT use any command that requires root privilege! Don't ask why, there's a whole bunch of restrictions to our project, I just can't use root privilege, no sudo, no su, just anything only a regular user can do....
The part I already achieved is using the latest version of mke2fs -d command to populate a tree of folders and files into different partitions of this image file like this
suppose after the image is booted, we have these folder structure
$ ls /
$ bin dev boot home lib32 mnt proc run srv tmp var boot data etc lib lib64 opt root sbin sys usr
some of the folders are mounted by different partitions
extract a single partition from image
dd if=image of=partitionN skip=offset_of_partition_N count=size_of_partition_N bs=512 conv=sparse
populate a folder into the partition
mke2fs -d root_dir/etc partitionN
put the partition back into image
dd if=partitionN of=image seek=offset_of_partition_N count=size_of_partition_N bs=512 conv=sparse,notrunc
We have the first partition in the image as the boot partition, which contains the 'boot' folder and will be mounted under /boot once it is booted.
And this boot partition is an EFI compatible partition(which actually seems to be a FAT32 format), since our project needs it to be this way.
BUT After get all partitions successfully populated into the image, I can not find a way to install grub for this bootable image. And that's the most damn important step that needed to make this image bootable.
All solutions I found on the web suggest loop mount the image's boot partition, which I can not do because without root privilege I can't loop mount the image.
So does any one have any idea how to do this?
I tried to understand how grub write raw values into mbr, and how to find stage1 and stage2 from the values inside mbr, and how to figure out the sector list at the end of stage2's first sector, but that's so crazy and I eventually failed to get this trick work.