Skip to content

rohith2011/Bootable-Linux-image-via-QEMU

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Bootable Linux Image via QEMU

This repository contains a shell script that creates a fully bootable AMD64 Linux disk image using QEMU. The image, once booted, prints "hello world" to the console and does not include any user/session management or login prompts.

Overview

The script is created by Kondreddy Rohith Sai Reddy and performs the following steps:

  • Kernel Image Verification: Checks for a kernel image named vmlinuz in the current directory. If not found, it downloads a prebuilt AMD64 kernel image from a provided URL (update the URL as needed).
  • Minimal Init Program: Creates and statically compiles a small C program that prints "hello world".
  • Disk Image Creation:
    • Creates a 64 MB disk image.
    • Uses parted to partition the image with an MBR partition table.
    • Creates a primary ext4 partition and marks it as bootable.
  • Filesystem Setup:
    • Formats the partition as ext4.
    • Mounts the partition and copies the kernel image and the init binary.
    • Sets up a minimal GRUB configuration.
  • GRUB Bootloader Installation: Installs GRUB onto the disk image to make it fully bootable.
  • QEMU Boot: Launches QEMU to boot the disk image, directing console output to the terminal.

Prerequisites

Ensure the following tools are installed on your system:

  • qemu-system-x86_64
  • gcc
  • wget
  • cpio
  • gzip
  • dd
  • parted
  • mkfs.ext4
  • losetup
  • mount
  • umount
  • grub-install

Note: Several operations (e.g., mounting, loop device management, GRUB installation) require superuser privileges. It is recommended to run the script using sudo.

Usage

  1. Prepare the Environment:
    Verify that all required tools are installed. If not, install them using your package manager.

  2. Kernel Image:
    Place a valid AMD64 Linux kernel image named vmlinuz in the current directory.
    Alternatively, update the KERNEL_URL variable in the script with a valid URL to download a kernel image automatically.

  3. Run the Script:
    Execute the script (e.g., sudo ./script_name.sh).
    The script operates entirely within the current working directory and does not require any additional user input.

Script Details

  • Kernel Image Verification:
    Checks for vmlinuz in the current directory and downloads it if missing.

  • Minimal Init Program:
    A C program (hello.c) is created and compiled statically into an executable init that prints "hello world".

  • Disk Image Creation and Partitioning:

    • A 64 MB disk image (disk.img) is created using dd.
    • The disk is partitioned with parted (MBR) and a primary partition is set as bootable.
  • Filesystem and GRUB Setup:

    • The partition is formatted as ext4.
    • The kernel and init binary are copied to the appropriate locations.
    • A minimal GRUB configuration is created in /boot/grub/grub.cfg to boot the kernel with init=/init.
    • GRUB is installed onto the disk image with grub-install.
  • Booting via QEMU:
    The disk image is booted with QEMU using the -nographic option so that all output is sent to the terminal.

Assumptions

  • The script is designed for Ubuntu 20.04 LTS or 22.04 LTS (or similar Linux distributions).
  • All operations are confined to the current working directory.
  • Superuser privileges are required for certain operations such as mounting, loop device management, and GRUB installation.
  • The placeholder kernel URL in the script must be updated to a valid URL if no local kernel image is provided.

Cleanup

The script automatically cleans up temporary files and unmounts any mounted partitions after installing GRUB.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages