LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 07-27-2012, 05:40 AM   #1
mathrisk
LQ Newbie
 
Registered: Sep 2006
Location: Kolkata, India
Distribution: CentOS, Ubuntu, Fedora, Mint
Posts: 8

Rep: Reputation: 0
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
 
Old 07-27-2012, 10:10 AM   #2
Sydney
Member
 
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

Rep: Reputation: 36
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
 
1 members found this post helpful.
Old 07-27-2012, 10:23 AM   #3
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,530
Blog Entries: 2

Rep: Reputation: 4026Reputation: 4026Reputation: 4026Reputation: 4026Reputation: 4026Reputation: 4026Reputation: 4026Reputation: 4026Reputation: 4026Reputation: 4026Reputation: 4026
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.
 
2 members found this post helpful.
Old 07-27-2012, 03:38 PM   #4
jefro
Guru
 
Registered: Mar 2008
Posts: 11,340

Rep: Reputation: 1386Reputation: 1386Reputation: 1386Reputation: 1386Reputation: 1386Reputation: 1386Reputation: 1386Reputation: 1386Reputation: 1386Reputation: 1386
"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.

Last edited by jefro; 07-27-2012 at 03:42 PM.
 
1 members found this post helpful.
Old 07-30-2012, 03:10 AM   #5
mathrisk
LQ Newbie
 
Registered: Sep 2006
Location: Kolkata, India
Distribution: CentOS, Ubuntu, Fedora, Mint
Posts: 8

Original Poster
Rep: Reputation: 0
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 )
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.
 
Old 07-30-2012, 03:14 AM   #6
mathrisk
LQ Newbie
 
Registered: Sep 2006
Location: Kolkata, India
Distribution: CentOS, Ubuntu, Fedora, Mint
Posts: 8

Original Poster
Rep: Reputation: 0
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.
 
Old 07-30-2012, 09:24 AM   #7
mathrisk
LQ Newbie
 
Registered: Sep 2006
Location: Kolkata, India
Distribution: CentOS, Ubuntu, Fedora, Mint
Posts: 8

Original Poster
Rep: Reputation: 0
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?
 
Old 07-30-2012, 03:28 PM   #8
jefro
Guru
 
Registered: Mar 2008
Posts: 11,340

Rep: Reputation: 1386Reputation: 1386Reputation: 1386Reputation: 1386Reputation: 1386Reputation: 1386Reputation: 1386Reputation: 1386Reputation: 1386Reputation: 1386
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.
 
Old 08-02-2012, 02:41 AM   #9
mathrisk
LQ Newbie
 
Registered: Sep 2006
Location: Kolkata, India
Distribution: CentOS, Ubuntu, Fedora, Mint
Posts: 8

Original Poster
Rep: Reputation: 0
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.
 
  


Reply

Tags
bootable, usb


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to create a kernel bootable image aloktiagi Linux - Newbie 1 04-23-2010 07:15 AM
How to create a bootable image from debootstrapped filesystem? unev_21 Linux - Software 1 02-04-2010 02:31 AM
[SOLVED] How to create an ISO image from a bootable USB pendrive jortalli Linux - Software 2 09-02-2009 06:46 AM
How to create a CD bootable ISO image for 2.6.16 kernel vb_sridevi Linux - Newbie 4 06-25-2008 01:47 PM
How to create bootable partition image to burn hs.chauhan Linux - General 16 01-31-2007 09:52 PM


All times are GMT -5. The time now is 02:11 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration