Linux How to create Bootable USB Image

mathrisk

.: deleted :.
Level G
Hi,

I need to create a bootable image "file" (something like .iso) for for USB drive.
I have all the contents ready (initrd, vmlinux, grub.conf etc) and I can create a bootable USB connecting the USB drive into the machine. But now I need a way to create an Image file of the contents (just like iso using mkisofs) so that the same can be written into any USB drive later.

So, is there any command/program/way to achieve that?
Also, if it there, how shall I write the image into USB?

Any recommendation/information/pointer regarding this would be much appreciated.

TIA
 
if you have the image file in *.iso format, then you can use YUMI to make any USB device bootable with the image file
 
Thanks for your responses.

Actually my requirement is to create an image file (say .img file) which can be write into USB using some other tool. (eg https://wiki.ubuntu.com/Win32DiskImager)
I cannot use an .iso to create the bootable USB. (I have to deliver two images - one for DVD one for USB :P)

So, how shall I create the .img file? Any way for that??
 
I have created a bootable Image file. Below is the procedure I followed (and wrote a blog post too. :P)

Step 1: Creating the image
Using 'dd' command first create a image file with null bytes.
Code:
dd if=/dev/zero of=/tmp/disk.img bs=1024 count=250000
bs is blocksize and count is number of blocks. This will create a image file of size 245MB. Modify the count according to the need.

Step 2: Next we need a loopback device which would be attached to the disk image so that other applications can use the image file as block device.
Code:
losetup -f
gives the first free loopback device that can be used. (say /dev/loop0 is free)
Code:
losetup /dev/loop0 /tmp/disk.img

Step 3: Create a partition in the image using the loopback device. fdisk can be used to do that.
Code:
fdisk /dev/loop0
Provide proper options for fdisk.
(I gave n->p->1->Enter->Enter->t->c->w to create a FAT32 partition in the image)

Step 4: Next we need another loopback device for the partition we have just created to make the partition available for other applications as device. Now the loopback device /dev/loop0 is similar to /dev/sda. What we need a loopback device attached to the partition, for example a device like /dev/sda1.
Code:
fdisk -ul /dev/loop0
will show the partition and block size. losetup takes offsets as the number of bytes to skip at the beginning of the file. The output from fdisk -ul /dev/loop0 shows that the first partition starts at block 63, and each blocks are of 512 bytes. So partition 1 starts at byte 32,256.
Code:
losetup -o 32256 /dev/loop1 /dev/loop0
This command will create another loopback device and attach to the parition created above.

Step 5: Now we need to format and mount the partition attached to the device /dev/loop1
Code:
mkfs.vfat /dev/loop1
This command formats the partition with file system as FAT.
(here a message may show up as
"Loop device does not match a floppy size, using default hd params"
but which seems fine as the image worked.)

Next create a temporary directory and mount the partition to it
Code:
mkdir /mnt/tmp
    mount /dev/loop2 /mnt/tmp

Step 6: Copy the Kernel and Initrd files to the mount directory.

Step 7: Copy GRUB related stuffs.
Code:
mkdir -p /mnt/tmp/boot/grub
    cp /boot/grub/stage1 /boot/grub/stage2 /boot/grub/fat_stage1_5 /mnt/tmp/boot/grub

Then create a grub.conf file in the grub directory. A default grub.conf file seems like

Code:
default=0
    timeout=5
    title My USB Linux
    root (hd0,0)
    kernel /mykernel rw root=/dev/ram0 init=/init ramdisk_size=524288 ramdisk_blocksize=1024
    initrd /initrd.img

Provide proper kernel and initrd names.

Step 8: Install GRUB. The following command will take to the grub console
Code:
grub --device-map=/dev/null
And the following grub commands needs to be executed.
    device (hd0) /tmp/disk.img
    root (hd0,0)
    setup (hd0)
    quit

Step 9: Cleaning the mess.
Code:
umount /mnt/tmp
Deleting the loopback devices
Code:
losetup -d /dev/loop1
    losetup -d /dev/loop0

And thats it. A bootable disk image is ready. Now it can burnt to any USB using any software like 'Image Writer'

(link to my blog post)
 
Back
Top