Ubuntu Bootable USB sticks

=The various ways of making bootable USB sticks=

For these examples we are using a USB stick that is a block device on /dev/sdd. I am going to be using a (512Mb) SanDisk  U3 Cruzer Micro  2.15 as it was at hand when I came to write this. (At some point in the distant past I used a Windows utility to remove the U3 part of it, so its just a regular stick now). This is Sandisks Windows one and This is the opensource one for Linux/Win

In all these examples I am going to be creating them as hard drives, with an MBR/partition. I am not going to do any with the filesystem starting at the beginning like a floppy. (Superfloppy)

Wiping the USB stick clean
It is a good idea to start by wiping the entire stick to make sure there aren't other backup MBR's or boot sectors in different places that you can easily get in a ravel with. Easiest but not quickest is to write zero's to it with dd

$ dd if=/dev/zero of=/dev/sdd bs=1M dd: writing `/dev/sdd': No space left on device 490+0 records in 489+0 records out 513614336 bytes (514 MB) copied, 76.6514 s, 6.7 MB/s

Now you have effectively destroyed everything on the stick, however linux kernel still holds the partition table from when it was inserted so you need to inform it. The best way to achieve this is simply using eject, which simulates an ejection, sleeps, then a reinsertion. This avoids needing root. (If this fails for any reason you can just manually pull out, wait a second and then put it back in.) $ eject /dev/sdd; sleep 1; eject -t /dev/sdd

If you look at /var/log/syslog and should see sdd: detected capacity change from 513614336 to 0 sdd: unknown partition table

(Equivalent commands with root are $ partprobe or $ hdparm -z /dev/sdd)

The problems
Next, by default you can easily get in a mess with the heads/cylinders because for bootable FAT/NTFS filesystems they need to match the partition table. This is made even more messy because various programs gparted/parted/fdisk dont tell you the same thing.

After 0'ing fdisk by default shows this geometry $ fdisk /dev/sdd

Command (m for help): p

Disk /dev/sdd: 513 MB, 513614336 bytes 16 heads, 62 sectors/track, 1011 cylinders, total 1003153 sectors

However if you partition it and then save it out, and go back into the program it changes the geometry to 4 heads, 55 sec/track.

$ fdisk /dev/sdd

Command (m for help): p

Disk /dev/sdd: 513 MB, 513614336 bytes 4 heads, 55 sectors/track, 4559 cylinders, total 1003153 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x30395976

Device Boot     Start         End      Blocks   Id  System /dev/sdd1  *        2048     1003152      500552+   b  W95 FAT32

Now for some added complication - partitioning in gparted (which says its using 255 heads 63 sectors/ track and 62 cylinders) actually doesn't.

This is what fdisks says it ends up being (87 heads 24 sec/track) (I also forgot to set bootable flag in this example and set FAT16 for some reason!) $ fdisk /dev/sdd

Command (m for help): p

Disk /dev/sdd: 513 MB, 513614336 bytes 87 heads, 24 sectors/track, 480 cylinders, total 1003153 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000712a8

Device Boot     Start         End      Blocks   Id  System /dev/sdd1           2048     1001471      499712    6  FAT16

I also tried using cfdisk, this gives 16 heads 62 sec/track when creating the partition, but going back into the program after writing the partition out, it actually then used 4 heads and 55 sectors/track. It also starts the partition at sector 62. This is effectively the same as fdisk except fdisk now likes to start at sector 2048.

Where does the truth lie? I think looking at what fdisk says AFTER you have written the partition is the best reflection of reality.(see below)

I think this kind of problem exists more with smaller sticks, newer ones over a few Gb generally default to 255 heads 63 sec/track and are pretty consistant. So in a way this is a good example as it will help you see /understand what is going on.

From past experience I have always found it best to fdisk and manually force heads and sectors to the 'standard' fdisk -H255 -S63 /dev/sdd ...however this USB stick doesnt seem to like being forced, although on most others it is usually fine. So I will stick with what it made (4/55) in the various booting scenarios.

Brief partition table explanation
In depth explanation of the MBR (and partition table)

I have geared this really towards just getting a feel of whats going on enough to be able to sort your USB stick out.

save partition table out
dd if=/dev/sdd of=/tmp/myptable ibs=1 skip=446 count=64
 * example of backing up the 64 byte partition table from offset 446 from start of media (copying 64 1 byte blocks)

write partition table backup back
dd if=/tmp/myptable of=/dev/sdd obs=1 seek=446 count=64

Example
This is taken from the above partitioning that gparted made, just as an example. This is the first part of the partition table (16 bytes) for the first (and only) partition created. 00 20 21 00 06 56 18 3E  00 08 00 00  00 40 0F 00

00h	Current State of Partition (00h=Inactive, 80h=Active)			1 Byte		(not marked bootable, oops) 01h	Beginning of Partition - Head						1 Byte		(DOS) 02h	Beginning of Partition - Cylinder/Sector (See Below)			1 Word		(DOS) 04h	Type of Partition (See List Below)					1 Byte		(06=FAT16) 05h	End of Partition - Head							1 Byte		(DOS) 06h	End of Partition - Cylinder/Sector					1 Word		(DOS) 08h	Number of Sectors Between the MBR and the First Sector in the Partition	1 Double Word	(Linux) 0Ch	Number of Sectors in the Partition					1 Double Word	(Linux)

Linux
Linux doesn't care for the geometry and uses (08-0B h) starting sector number and (0C-0F h) number of sectors (hence knows start and end) Note these double words are in little endian so 00 08 00 00 is 00000800h = 2048 (dec) and 00 40 0F 00 os 000F4000=999424 (dec) You can see then 2048 is starting sectors and end is 2048 + 999424 = 1001472 (MBR is 512 bytes=1 sector out from fdisk's 1001471, dont quite understand logic!)

DOS
Unfortunately DOS uses (01-03 h) start CHS and (05-07 h) end CHS and so the geometry does matter, hence why all this matters. This has to match the filesystem. Obviously the problem is if you dont know what the values are its actually matching it makes that rather difficult!

Partition it
As mentioned above we will leave fdisk to its own devices, choose one partition, with default start and end sectors, set partition 1 active, set type 0B (fat32), write it out.

$ fdisk /dev/sdd Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0xffdc2e83. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): n Command action e  extended p  primary partition (1-4) p Partition number (1-4, default 1): 1 First sector (2048-1003152, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-1003152, default 1003152): Using default value 1003152

Command (m for help): t Selected partition 1 Hex code (type L to list codes): 0B Changed system type of partition 1 to b (W95 FAT32)

Command (m for help): a Partition number (1-4): 1

Command (m for help): w The partition table has been altered!

Calling ioctl to re-read partition table.

WARNING: Re-reading the partition table failed with error 13: Permission denied. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8)

WARNING: If you have created or modified any DOS 6.x partitions, please see the fdisk manual page for additional information. Syncing disks.

Show how it reads back (note this shows what you have done by looking on the stick, but not what the kernel is seeing, which is what your system is still using)

$ fdisk -l /dev/sdd

Disk /dev/sdd: 513 MB, 513614336 bytes 4 heads, 55 sectors/track, 4559 cylinders, total 1003153 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x33663899

Device Boot     Start         End      Blocks   Id  System /dev/sdd1  *        2048     1003152      500552+   b  W95 FAT32

To update the kernel (the whole system) with the reality of the new partition table on the stick, you now MUST do a eject / reinsert. As before do this command (or just pull and reinsert the stick) eject /dev/sdd; sleep 1; eject -t /dev/sdd

Write MBR to it
Using syslinux 440 byte MBR (MBR boot code, not overwriting partition table), MBR type doesnt really matter so long as it jumps to the boot sector of the active filesystem. $ dd if=/usr/lib/syslinux/mbr.bin of=/dev/sdd bs=440 count=1 conv=notrunc 1+0 records in 1+0 records out 440 bytes (440 B) copied, 0.000615162 s, 715 kB/s

Format filesystem FAT32
Eject the stick and reinsert to make sure partition table is re-read by kernel. $ mkfs.msdos -F 32 -n "richud.com" /dev/sdd1 mkfs.msdos 3.0.9 (31 Jan 2010)

Mount new filesystem
If it didnt automount when the formatting finished, then manually mount it udisks --mount /dev/sdd1

Copy DOS system files
Copy Win98SE core system boot files, these are from 4.10.2222


 * io.sys
 * msdos.sys
 * command.com

Unmount Filesystem
$ udisks --unmount /dev/sdd1

Create bootsector at start of first partition
$ ~/Downloads/ms-sys-2.2.1/bin/ms-sys -3 /dev/sdd1 FAT32 DOS boot record successfully written to /dev/sdd1

Fixing the boot sector
Now its a physical drive, its possible to use ms-sys to fixup the boot sectors geometry with the partition. Unfortunately its back to thinking its 16 heads when you run it!

ms-sys -p /dev/sdd1 Start sector 2048 (nr of hidden sectors) successfully written to /dev/sdd1 Physical disk drive id 0x80 (C:) successfully written to /dev/sdd1 Number of heads (16) successfully written to /dev/sdd1

Remember fdisk thinks its (4 heads, 55 sectors/track, 4559 cylinders, total 1003153 sectors) - cfdisk also shows its 4 head and 55 sec/track

hexedit /dev/sdd1

This is the start of the original partition (boot sector) as created by mkfs.msdos 00000000  EB 58 90 6D  6B 64 6F 73  66 73 00 00  02 08 20 00  02 00 00 00  00 F8 00 00  .X.mkdosfs.... ......... 00000018  3E 00 10 00  00 00 00 00  91 46 0F 00  D0 03 00 00  00 00 00 00  02 00 00 00  >........F.............. 00000030  01 00 06 00  00 00 00 00  00 00 00 00  00 00 00 00  00 00 29 42  46 65 D3 72  ..................)BFe.r]

This is the start of ms-sys's modification when writing a FAT32 DOS boot sector. 00000000  EB 58 90 4D  53 57 49 4E  34 2E 31 00  02 08 20 00  02 00 00 00  00 F8 00 00  .X.MSWIN4.1... ......... 00000018  3E 00 10 00  00 00 00 00  91 46 0F 00  D0 03 00 00  00 00 00 00  02 00 00 00  >........F.............. 00000030  01 00 06 00  00 00 00 00  00 00 00 00  00 00 00 00  00 00 29 88  4B A4 B1 72  ..................).K..r

This is my-sys fixed up part, notice the problems as its using the same geometry that fdisk thought before it wrote anything (16 heads, 62 sec/track), not what it ended up with (4 heads 55 sectors/track) It has fixed the start sector though. 00000000  EB 58 90 4D  53 57 49 4E  34 2E 31 00  02 08 20 00  02 00 00 00  00 F8 00 00  .X.MSWIN4.1... ......... 00000018  3E 00 10 00  00 08 00 00  91 46 0F 00  D0 03 00 00  00 00 00 00  02 00 00 00  >........F.............. 00000030  01 00 06 00  00 00 00 00  00 00 00 00  00 00 00 00  80 00 29 28  43 BE 87 72  ..................)(C..r

Check


 * offset 15h (F8) shows its a HDD, correct
 * offset 18h (3E) equates to 62 sectors/track, WRONG
 * offset 1A (10) equates to 16 heads, WRONG
 * offset 1C-D (00 08) is little endian, which is 0800 hex, which is 2048 hidden sectors, (now)correct
 * offset 40 (80), Logical drive number of partition (HDD), correct

Unsuprisingly, if you try it, it doesn't boot.

Fixing all the boot sector
Seems we need some manual correcting
 * offset 18h to 37h (55 sec/track)
 * offset 1Ah to 04h (4 heads)

00000018  37 00 04 00

Now it boots...



Grub4Dos
Download latest grub4dos here at time of writing grub4dos-0.4.5b-2011-12-26.zip

Grub4DOS is rather cool. It does many things and can get you in a ravel like nothing else if you start fiddling with bootlace (its installer) It tends to backup previous MBR's, copy old boot sectors around and also its MBR contains boot sectors so if you are looking at your stick with a hex editor you never are entirely sure what you are looking at/editing.

MBR
Unlike other ways, it's MBR sort is somewhat enlarged, it continues on the sectors after the partition table, surrounding it. Therefor its easiest to write this first. As we are using a real block device, we can use cat to write the MBR to it. cat grldr.mbr > /dev/sdd

bootlace
Alternatively you can use bootlace, but be careful as it can and will do other wonderful things and if you are trying to do methodical testing will rapidly complicate things) Details are in the README_GRUB4DOS.txt ./bootlace.com --mbr-no-bpb --no-backup-mbr --time-out=0 /dev/sdd

Partition the stick
As above with fdisk See here, getting back to 5 heads and 55 sec/track. Don't forget to eject/reinsert with command or physically!

Format the stick
As above with mkfs.msdos FAT32, See here

mkfs.msdos -F 32 -n "richud.com" /dev/sdd1

Mount it if it doesnt automount
As above with udisks See here

udisks --mount /dev/sdd1

Copy grldr and menu.lst
Once its mounted copy these to the root of the partition. (Think of these are the equivalent of DOS's command.com io.sys msdos.sys autoexec.bat etc)
 * grldr
 * menu.lst
 * default

You will need to edit menu.lst to do what you want but the default will give you some ideas.

Unmount it
As above with udisks See here

Fix partition boot sector
Despite the fact the grldr MBR should be capable of finding the grldr itself, on some hardware (e.g. Intel DQ965GF) you again need to fix the geometry in the boot sector. I don't understand why as it doesn't do anything as I understand it. It is fixed as described above but as it's now just the mkdosfs one that isn't meant to boot anything anyway I will go over it again.


 * offset 15h (F8) is a HDD
 * offset 18h (3E) -> (37) 55 sectors/track
 * offset 1A (10) -> (04) 4 heads
 * offset 1C-D (00 00) -> (00 08) is little endian, which is 0800 hex, which is 2048 hidden sectors
 * offset 40 (00) -> (80), Logical drive number of partition (HDD)

Unfixed, Start of Boot sector at start of partition after mkfs.msdos formatted it. $ hexedit /dev/sdd1

00000000  EB 58 90 6D  6B 64 6F 73  66 73 00 00  02 08 20 00  02 00 00 00  00 F8 00 00  .X.mkdosfs.... ......... 00000018  3E 00 10 00  00 00 00 00  91 46 0F 00  D0 03 00 00  00 00 00 00  02 00 00 00  >........F.............. 00000030  01 00 06 00  00 00 00 00  00 00 00 00  00 00 00 00  00 00 29 21  D9 BB 6D 72  ..................)!..mr

Fixed Up Manually

00000000  EB 58 90 6D  6B 64 6F 73  66 73 00 00  02 08 20 00  02 00 00 00  00 F8 00 00  .X.mkdosfs.... ......... 00000018  37 00 04 00  00 08 00 00  91 46 0F 00  D0 03 00 00  00 00 00 00  02 00 00 00  7........F.............. 00000030  01 00 06 00  00 00 00 00  00 00 00 00  00 00 00 00  80 00 29 21  D9 BB 6D 72  ..................)!..mr

Syslinux
Latest Syslinux here from kernel.org At time of writing syslinux-4.05.zip is latest version.

Syslinux is more straightforward and has a normal MBR that points to the boot sector of the active partition ala DOS above. The boot sector code is generated by syslinux's installer.

Partition the stick
As above with fdisk See here, getting back to 5 heads and 55 sec/track. Don't forget to eject/reinsert with command or physically!

MBR
Write the MBR to the stick $ cat syslinux-4.05/mbr/mbr.bin > /dev/sdd

Note, if you have issues with the standard MBR there are slight variations provided with syslinux. You can manually change these with the below (change mbr.bin to the alternate which are in the /mbr/ folder). (If you are new and having problems though it is far more likely you haven't fixed the boot sector or missed a step out.)

Format the stick
As above with mkfs.msdos FAT32, See here

mkfs.msdos -F 32 -n "richud.com" /dev/sdd1

Mount it if it doesnt automount
As above with udisks See here

udisks --mount /dev/sdd1

Copy syslinux files and syslinux.cfg
Once its mounted copy syslinux core files to the root of the partition.

Perhaps more usefully run the little script which copies other files you will need.
 * Change directory to below where you extracted syslinux too. i.e. if its in /tmp/syslinux-4.05 change to /tmp/
 * Change the target (/media/richud.com) to whatever you called the stick when you formatted it. (Make sure its mounted too.)

$ ls -ln /media/richud.com total 296 -rw-r--r-- 1 1000 1000 20704 2012-01-04 11:41 chain.c32 -rw-r--r-- 1 1000 1000 21948 2012-01-04 11:41 gfxboot.c32 -rw-r--r-- 1 1000 1000  1312 2012-01-04 11:41 localboot.c32 -rw-r--r-- 1 1000 1000 26140 2012-01-04 11:41 memdisk -rw-r--r-- 1 1000 1000 56292 2012-01-04 11:41 menu.c32 -rw-r--r-- 1 1000 1000   800 2012-01-04 11:41 reboot.c32 -rw-r--r-- 1 1000 1000 155792 2012-01-04 11:41 vesamenu.c32

You will need to manually create your own syslinux.cfg too and put it with them, but for the sake of testing you can omit this for the moment. More details about this are on another page. 

You can put these in other places but (/boot/syslinux) where syslinux looks but to be honest you may as well keep them in the root, at least while you are getting it all working.

Boot sector
Still with it mounted - you need to use the installer as it does;
 * boot sector
 * copies the next stage loader file (ldlinux.sys)(Think of ldlinux.sys ~ equivalent of DOS's command.com/io.sys/msdos.sys)

Note you target the partition sdd1 not drive sdd. (This needs to be run as administrator for some reason.)

$ sudo ./syslinux-4.05/linux/syslinux --install /dev/sdd1

Note, there are options for specifying heads/sectors as options with the above command, but this doesn't alter the boot sector code, so don't use this thinking it will fix the boot sector.

Unmount it
As above with udisks See here

udisks --unmount /dev/sdd1

Fix up the boot sector
As above with same fix procedure See here

hexedit /dev/sdd1

Check;
 * offset 15h (F8) is a HDD
 * offset 18h (3E) -> (37) 55 sectors/track
 * offset 1A (10) -> (04) 4 heads
 * offset 1C-D (00 00) -> (00 08) is little endian, which is 0800 hex, which is 2048 hidden sectors
 * offset 40 (00) -> (80), Logical drive number of partition (HDD)

This is vital so dont miss this last bit out!

Again if you omit this newer machines may not care but certain ones definitely do (e.g. DQ965GF)

Extlinux
Extlinux is really just a different 'frontend' of syslinux in so much as it runs on Ext filesystem rather than FAT, but everything else is the same. If you dont need to use FAT on the stick this is probably the best to use. You will bypass all the pantsness of FAT filesystems.

Latest Syslinux here from kernel.org At time of writing syslinux-4.05.zip is latest version. (This contains extlinux)

Partition the stick
As above with fdisk See here, getting back to 5 heads and 55 sec/track. BUT, keep the type to 83 (Linux,default) not 0B (FAT32) Don't forget to eject/reinsert with command or physically!

MBR
Write the MBR to the stick $ cat syslinux-4.05/mbr/mbr.bin > /dev/sdd

Note, if you have issues with the standard MBR there are slight variations provided with syslinux. You can manually change these with the below (change mbr.bin to the alternate which are in the /mbr/ folder). (If you are new and having problems though it is far more likely you haven't fixed the boot sector or missed a step out.)

Format the stick Ext4
Format the stick ext4

$ mkfs.ext4 /dev/sdd1 -L richud.com mke2fs 1.41.14 (22-Dec-2010) Filesystem label=richud.com OS type: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) Stride=0 blocks, Stripe width=0 blocks 125488 inodes, 500552 blocks 25027 blocks (5.00%) reserved for the super user First data block=1 Maximum filesystem blocks=67633152 62 block groups 8192 blocks per group, 8192 fragments per group 2024 inodes per group Superblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

Writing inode tables: done Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 26 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.

remove any reserved blocks
$ tune2fs /dev/sdd1 -m0 -O sparse_super tune2fs 1.41.14 (22-Dec-2010) Setting reserved blocks percentage to 0% (0 blocks)

Note, it may now automount after running this if you have automount on

turn the journalling off
$ tune2fs -O ^has_journal /dev/sdd1 tune2fs 1.41.14 (22-Dec-2010)

Mount it if it doesnt automount
As above with udisks See here

udisks --mount /dev/sdd1

Copy syslinux files and syslinux.cfg
Once its mounted copy syslinux core files to the root of the partition.

Perhaps more usefully run the little script which copies other files you will need.
 * Change directory to below where you extracted syslinux too. i.e. if its in /tmp/syslinux-4.05 change to /tmp/
 * Change the target (/media/richud.com) to whatever you called the stick when you formatted it. (Make sure its mounted too.)

$ ls -ln /media/richud.com total 296 -rw-r--r-- 1 1000 1000 20704 2012-01-04 11:41 chain.c32 -rw-r--r-- 1 1000 1000 21948 2012-01-04 11:41 gfxboot.c32 -rw-r--r-- 1 1000 1000  1312 2012-01-04 11:41 localboot.c32 -rw-r--r-- 1 1000 1000 26140 2012-01-04 11:41 memdisk -rw-r--r-- 1 1000 1000 56292 2012-01-04 11:41 menu.c32 -rw-r--r-- 1 1000 1000   800 2012-01-04 11:41 reboot.c32 -rw-r--r-- 1 1000 1000 155792 2012-01-04 11:41 vesamenu.c32

You will need to manually create your own syslinux.cfg too and put it with them, but for the sake of testing you can omit this for the moment. More details about this are on another page. 

You can put these in other places but (/boot/syslinux) where syslinux looks but to be honest you may as well keep them in the root, at least while you are getting it all working.

Boot sector
Still with it mounted - you need to use the installer as it does;
 * boot sector
 * copies the next stage loader file (ldlinux.sys)(Think of ldlinux.sys ~ equivalent of DOS's command.com/io.sys/msdos.sys)


 * Note you target the mounted drive, not the block device/partition, odd!


 * Note, If you get "extlinux: No such file or directory" you may need to install your distro's extlinux package (sudo apt-get install extlinux) as it has some dependancy on the local machine for some reason.

(This may need to be run with sudo, it did in Ubuntu 12.04 and in 12.10 appears not ... in fact in 12.10 you can only use the inbuilt extlinux package, e.g. just extlinux --install /media/richud.com)

$ sudo ./syslinux-4.05/extlinux/extlinux --install /media/richud.com


 * Note, there are options for specifying heads/sectors as options with the above command, this is nothing to do with aforementioned the bootsector fixes


 * Note, If using Ext filesystem, extlinux writes the correct starting sector in little endian to offset 1C-Dh in /dev/sdd1 and also writes (wrongly) the number of heads & sec/track, not sure why. The partition is Ext4 and purely from a few tests doesn't seem to make any difference whats written at these (FAT only? offsets), you dont need to have any of it set correctly to work. I have no explanations for this at all :)


 * Note, If using FAT32 filesystem (extlinux can use this too) it doesn't write any of the offset values at all - and wont work without fixing them but if you fix all 5 then it does work fine (on a DQ965GF)

Unmount it
As above with udisks See here

udisks --unmount /dev/sdd1

Grub2
I have had least success with Grub2, but have fiddled with it least. I am using grub 1.99-12ubuntu5 here. It is similar to grub4dos (or vice versa) in so much as it has a fat MBR overspilling and doesn't do anything with the boot sector at the start of the partition. The second stage loader bits still run from the filesystem though.

Partition the stick
As above with fdisk See here, getting back to 5 heads and 55 sec/track. BUT, keep the type to 83 (Linux,default) not 0B (FAT32) Don't forget to eject/reinsert with command or physically!

Format the stick
As above with mkfs.ext4,[[#Format the stick Ext4|See here]

mkfs.ext4 /dev/sdd1 -L richud.com tune2fs /dev/sdd1 -m0 -O sparse_super tune2fs -O ^has_journal /dev/sdd1

Mount it if it doesnt automount
As above with udisks See here

udisks --mount /dev/sdd1

Install MBR and loader files
--boot-directory is the place the filesystem was mounted (which will be the formatted label /media/richud.com, if you followed the above) followed by 'boot' which will become a root folder on the stick.

Note, older versions (grub2 < 1.99) it has slight syntax change from '--root' to '--boot', it is grub-install --root-directory=/media/richud.com/boot /dev/sdd

$ grub-install --no-floppy --boot-directory=/media/richud.com/boot /dev/sdd Installation finished. No error reported.

You should end up with /media/richud.com/boot/grub/ containing a whole lot of files.

Inside boot/grub/ you can create boot/grub/grub.cfg

Unmount it
As above with udisks See here

udisks --unmount /dev/sdd1

Ubuntu 12.10
Things are not mounted via gvfs directly into /media/xxx but now /media/yourusername/xxx IF you mount via Nautilus, but IS ok on command line with udisks.

So in this example /media/richud.com will be /media/rich/richud.com, if you username was rich.

Problem Solving
Hex view the stick with hexedit and tab across to search as ASCII (text search), search for the failure message on the screen (a short keyword from it) and this should give you a good idea where the problem lies. e.g. from the MBR or the boot sector. (If it is in neither of these it may be from the machines BIOS which means you probably don't have an active partition)


 * Fully wipe the stick and start again if you have problems as it is all too easy to get in a ravel and make wrong assumptions as to what you did or didnt do ten minutes previously. I know, I've done it.
 * Make sure after doing anything with the partition table the stick is ejected/reinserted with command or physically, this includes after wiping it AND after creating or adjusting the partition table.
 * Make sure you have correct boot sector and it is fixed correctly (if using FAT/FAT32). There are 5 things to check.
 * Try it with extlinux and ext4 filesystem as this negates issues with the bootsector fixing.
 * 'Missing operating system' means you probably forgot to copy the boot sector.
 * If it doesn't appear to do anything check you set the partition active.
 * 'Boot Error' may result from you missing off adjusting offset 0x40h, setting 00 to 80 (hexedit the boot sector, e.g. /dev/sdd1 and you will see 'Boot Error' message originates from here)