Saturday, June 22, 2013

Making a Bootable USB Disk with an NTFS Partition and GRUB

Introduction

There may be many reasons to create a bootable USB flash drive with an installed bootloader. Not all live CD's of popular distros contain hybrid MBR/El-Torito bootable sectors. So if you are trying to write the ISO image directly into the flash drive using: then it won't help. The flash drive won't boot, because BIOS sees it as a hard disk and requires bootstrap code in the Master Boot Record (MBR) of the drive. That's because the ISO image written to the drive most probably has so called "no-emulation" mode boot sector of the El Torito bootable CD specification somewhere at position 17 (sector size is 2048 bytes). On contrary, GRUB bootloader, when properly tuned, is capable of loading just few Linux distributions directly from the ISO image stored on the drive. It cannot boot all ISO files, but at least ArchLinux and Ubuntu seem to be bootable.
In addition, with the help of memdisk utility of the SYSLINUX bootloader it can load any ISO/floppy disk image of sufficiently small size directly to RAM and chainload the boot process. This method can be used to boot exotic operating systems such as Kolibri, MenuetOS, DixOS which are written in assembly language. Booting small Linux-based distros such as Tinfoil Hat Linux and Tiny Core Linux in the RAM disk is also possible. The only prerequisite is a sufficiently large RAM size of the computer.
Also it should be mentioned that GRUB provides a minimal shell environment, and acts like a small operating system. It is capable of reading data from many operating systems and even displaying graphic images of the JPEG and PNG format. Moreover, a flash drive formatted with NTFS file system can be used as a USB stick for storing and transferring data between computers (especially which are running Windows OS). This is actually the main function of the flash drive in the daily life.

Prerequisites

An IBM-compatible PC, the Arch Linux operating system, a flash drive recognized as /dev/sdb (do not accidentally overwrite your main hard disk), QEMU virual machine (optional).

Installing Necessary Packages

First of all back up your data on your flash drive. Install necessary packages.

Partitioning the Drive

Now you can partition the drive despite all warnings and non-optimal behavior of small 512 byte long sectors. But who cares? Using NTFS/EXT3 or any other popular file system is also a bad idea. Those file system do not take into consideration wear leveling when a single sector is repeatedly overwritten. So ideally it would be nice to use JFFS or some other flash file system.
Here intention is different. First of all, a lot of people have ever wondered how do the 446 bytes of the bootstrap code found in the MBR can magically locate the partition and load some files from the file system of that partition? The whole file system input/output is needed! And this requires several kilo-bytes of memory. Usually bootloaders store this code in the MS-DOS compatibility region (62 sectors of size 512 bytes each located right after the MBR, i.e. of total size 31 KB). So what happens if we create a partition table without MS-DOS compatibility region? GRUB will be able to load its core image directly using only MBR's bootstrap code without any file system driver! This method is called "blocklists" method. Here is the proof. We will create a primary partition starting right after the MBR:

Formatting the Partition

Format the first partition with NTFS file system and label it "BootableUSB":

Checking the MBR

Now check the MBR for the "55 AA" word at the end of the sector:

Checking the VBR

Similarly, check the Volume Boot Record (VBR) of the first partition for the "55 AA" word at the end of the sector. Note that VBR is located right after the MBR (option "skip=1" of the "dd" command): Note that should give the same result.

Installing GRUB into the MBR

Mount the first partition of the flash memory and install GRUB into the MBR using blocklists method: If you have a 64-bit system, change the target value "--target=x86_64-pc".

Configuring the GRUB

Now create GRUB configuration file: and paste the following content:

Installing the Memdisk Module

Simply copy the memdisk file to your GRUB folder:

Downloading the ISO/Floppy Images

Now you can download the ISO/floppy images of the operating systems listed in "/mnt/USB/grub/grub.conf" (i.e. ArchLinux, Ubuntu, Tinfoil Hat Linux, Micro-Core Linux and G4L partitioning utility) and put them under "/mnt/USB/System/" folder.

Testing the Bootable USB Disk

Now you can unmount the USB disk: and test it in the QEMU virtual machine: Instead of "qemu-system-i386" you can use "qemu-system-x86_64".
Hopefully you will get the following screen:



Now unplug the flash drive and test it on the real hardware.

No comments:

Post a Comment