LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   Create Bootable USB image (http://www.linuxquestions.org/questions/linux-software-2/create-bootable-usb-image-4175418919/)

mathrisk 07-27-2012 05:40 AM

Create Bootable USB image
 
Hi,

I need to create a bootable image file (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 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

Sydney 07-27-2012 10:10 AM

So if I understand you correctly you want to make a backup ISO of a bootable USB drive. I would use dd.
Quote:

dd if=/dev/yourdevice of=/home/user/Desktop/my.iso

TobiSGD 07-27-2012 10:23 AM

Create your ISO as usual, then use the hybridiso tool on the created ISO. This will convert it to a hybrid-ISO file, which either can be used to burn a CD/DVD or can be copied directly to USB with the dd or cat commands.

jefro 07-27-2012 03:38 PM

"so that the same can be written into any USB drive later." Kind of trick question.

The dd command is used normally. An .img file is common for this. The extension .iso tends to be an optical drive but any name is fine for your use. Different OS's may try to burn a cd from a wrong name.

Be very careful with the dd command. I'd boot to some other media like live cd or full install to use to copy this usb drive. Then maybe it might be /dev/sdb or such. You'd want the entire disk.

I guess you can try to make it a hybrid iso but that may involve some drastic change. Dunno what you have so I can't say how easy it would be.


It is also possible that you could use some other means like partimage or clonezilla. The trick to the deal is that clones may have issues with different hardware. Generally if you have a 2G drive you need to have a 2G to use or larger.

Lastly is exactly how this thing works. It may be that you are talking about a usb hard drive and have grub booting and using a disk by name or disk by id to tell it how to boot. Those specific issues need to be generic.

mathrisk 07-30-2012 03:10 AM

Thanks guys for your replies.

Since this is for client delivery I cannot go for some complex method. (I need to deliver the output as well as the procedure)

btw, I have found a way of what I need, but still not working yet. :)

Steps I had followed

Code:

mkfs.msdos /tmp/myImage.img -C 300000
mount -t vfat -o loop /tmp/myImage.img /mnt/tmp
syslinux /dev/loop0 (or 1/2...)

copy my stuffs - vmlinuz, initrd, my custom files etc to /mnt/tmp
Edited syslinux.cfg as
Code:

default linux
prompt 0
timeout 0
label linux
kernel vmlinuz
append initrd=initrd.img rw root=/dev/ram0 init=/init ramdisk_size=524288 ramdisk_blocksize=1024

And I have written the myImage.img file into the USB from Windows using Image Writer

After this, I can boot from the USB. (partially though :D)
It shows vmlinuz loading.
It takes lots of time to load initrd, but after sometime the machine reboots without showing up anything.

Any idea what may be I am doing wrong?

TIA.

mathrisk 07-30-2012 03:14 AM

One more query..
Do I need to install grub into the .img?

I am not very confident with these grub, syslinux things.
(I am asking because, while I creating the bootable USB - without the img file, I had used grub-install in that)

TIA.

mathrisk 07-30-2012 09:24 AM

Update

I have tried installing GRUB into a disk image.
Below is what I have done

Code:


dd if=/dev/zero of=/mnt/disk.img bs=1024 count=348000
losetup /dev/loop1 disk.img
fdisk /dev/loop1
(create new partition. Linux - #83 type)

fdisk -ul /dev/loop1
losetup -o 32256 /dev/loop2 /dev/loop1
mkfs /dev/loop2

mount /dev/loop2 /mnt/tmp
mkdir -p boot/grub

Copy my stuffs int /mnt/tmp
Copy boot/grub/{stage1,stage2,menu.lst & grub.conf}
Then I ran grub
Code:

grub --device-map=/dev/null

grub>        device (hd0) /mnt/disk.img
grub>        root (hd0,0)
grub>        setup (hd0)

But again I stuck the same place. The kernel loads just fine.
But it stuck during loading initrd.
It threw some error
Code:

Kernel panic : vfs unable to mount root fs on unknown-block...
Any idea what may be I am doing wrong?

jefro 07-30-2012 03:28 PM

Better off making the usb as you wish then the dd command to the device would create a copy. Why do you need to install grub?

It may be easier to use a virtual machine instead of trying a loop mount.

mathrisk 08-02-2012 02:41 AM

Solved the problem with kernel panic. It was because I was because I gave wrong root (hd1,0) for in the grub.conf.

btw, solved the complete issue. Created an Image file with GRUB in it. Now it can be burnt to any USB using any USB writer.

Steps I followed -

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 likedefault=0
Code:

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.
Code:

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'

Wrote a blog post with these. HERE

ps: thread can be marked SOLVED.


All times are GMT -5. The time now is 07:37 AM.