Skip to content
This repository has been archived by the owner on Nov 24, 2021. It is now read-only.

Boot HermitCore from a raw image

Stefan Lankes edited this page Jun 17, 2017 · 39 revisions

Sometime is it useful to boot HermitCore from a raw image. For instance, Google Compute Platform depends on this image format.

In this tutorial, an image will be created for the application stream. At first, we create with qemu-img a file in raw format. A size of 1 Gb is the smallest possible size for the Google Compute Platform and large enough for the stream benchmark.

$ qemu-img create -f raw disk.raw 1G
Formatting 'disk.raw', fmt=raw size=1073741824

Next lets load the nbd kernel module, and associate the previous image to one of the nbd devices.

sudo modprobe nbd max_part=16
sudo qemu-nbd -c /dev/nbd0 -f raw disk.raw

Like a normal hard disk, we have to partition the image. The following command create one big partition.

$ echo ';' | sudo sfdisk /dev/nbd0
Checking that no-one is using this disk right now ... OK

Disk /dev/nbd0: 1 GiB, 1073741824 bytes, 2097152 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

>>> Created a new DOS disklabel with disk identifier 0xe2111e23.
Created a new partition 1 of type 'Linux' and of size 1023 MiB.
/dev/nbd0p2: 
New situation:

Device      Boot Start     End Sectors  Size Id Type
/dev/nbd0p1       2048 2097151 2095104 1023M 83 Linux

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Now, we are able to format our partition.

$ sudo mkfs.ext2 /dev/nbd0p1
mke2fs 1.42.13 (17-May-2015)
Discarding device blocks: failed - Input/output error
Creating filesystem with 261888 4k blocks and 65536 inodes
Filesystem UUID: a49660c4-924e-4b4b-b867-8d081bd23484
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376

Allocating group tables: done                            
Writing inode tables: done                            
Writing superblocks and filesystem accounting information: done

Afterwards, we are able to mount the partition, to install grub and our application.

$ sudo mount /dev/nbd0p1 /mnt/
$ sudo grub-install --root-directory=/mnt --no-floppy --modules="normal part_msdos ext2 multiboot biosdisk" /dev/nbd0
$ sudo cp /opt/hermit/bin/ldhermit.elf /mnt/boot/
$ sudo cp /opt/hermit/x86_64-hermit/extra/benchmarks/stream /mnt/boot/

Finally, the configuration file /mnt/boot/grub.cfg has been created for the bootloader. In this case, the configuration file looks as follows:

default=0
timeout=0

menuentry "stream" {
	multiboot /boot/ldhermit.elf -uart=io:0x3f8
	module /boot/stream
	boot
}

Sometimes it is useful to define the IO port for the UART device as kernel parameter because HermitCore is currently not able to detect all existing UART devices. In this example, we define 0x3f8 as IO port.

Don't forget to unmount the devices:

$ sudo umount /mnt
$ sudo qemu-nbd -d /dev/nbd0 

Now, you have to pack the image with tar -cSzf disk.tar.gz disk.raw to a tar file. To use this file on the Google Compute Platform, you have to create Cloud Storage bucket.

gsutil mb gs://[BUCKET_NAME]/

Please, replace [BUCKET_NAME] with an appropriate name and upload the tar file to the cloud storage.

gsutil cp disk.tar.gz gs://[BUCKET_NAME]/

Create from this file an image, which we want to use as boot disk for the virtual machine.

gcloud compute --project "[PROJECT_ID]" images create "[IMAGE_NAME]" --description "Image Description" --source-uri "https://storage.googleapis.com/[BUCKET_NAME]/disk.tar.gz"

Please, replace [BUCKET_NAME], [PROJECT_ID] and [IMAGE_NAME] with an appropriate names. Finally, we have to create a VM and to boot from this disk.

gcloud compute --project "[PROJECT_ID]" instances create "[VM_NAME]" --zone "us-central1-c" --machine-type "f1-micro" --subnet "default" --maintenance-policy "MIGRATE" --service-account "[email protected]" --scopes "https://www.googleapis.com/auth/devstorage.read_only","https://www.googleapis.com/auth/logging.write","https://www.googleapis.com/auth/monitoring.write","https://www.googleapis.com/auth/servicecontrol","https://www.googleapis.com/auth/service.management.readonly","https://www.googleapis.com/auth/trace.append" --tags "http-server" --image "hermit" --image-project "[PROJECT_ID]" --boot-disk-size "10" --boot-disk-type "pd-standard" --boot-disk-device-name "[VM_NAME]"
Clone this wiki locally