Create independent Grub2 boot loader menu in USB stick.
Hi guys,
I am looking for a step by step approach - the "do-it-yourself" kind to achieve what I mentioned in the subject. I've a little knowledge of programming - so, will need hand-holding, to carry out terminal commands! Since I am at the lower end of the learning curve, explanations will be essential to grasp and understand what I am doing. I am sure, this will help a lot of late starters like me! Let me elaborate on what I am planning on, and my requirements. I have 2 old disks now. In each, I have windows in the Primary drive. I have installed Ubuntu, Fedora, Mint, Suse, etc. etc. versions in the residing in the extended partitions. (Crazy, but, this is just a hobby to me. I like to check and try out various distros' as they come, and maybe I plan to add one more disk!) Both of them have their own boot menus and I have to change the boot options in the BIOS every time to switch. When I read references to Grub2, chain loading etc. I understood enough to know that I can accomplish this with a simpler option. Only my knowledge is too limited to implement it! My wish list is to have Grub2 installed in any USB. So, when I boot through the USB, I should have the complete menu to boot onto any OS in the whole System - from the USB. Also, when I install a new flavour, or remove an old one, an update-grub should update new menu! On the other hand, the boot loaders in the individual disks also should not be disturbed, so that I can boot into any disk by the BIOS method. I am willing to experiment, and maybe even document it for others, if only a few of you can spare some of your time. I am sure, you can guide me, but you will need some patience! If you have any questions, please shoot. Thanks in advance - and I do find these forums very informative. cr4321 |
First, open a terminal (in any linux distro, I am using Ubuntu) and type
Code:
sudo fdisk -l Code:
Device Start End Blocks Id System Code:
sudo mkdir /mnt/root Code:
sudo mount -t ext3 /dev/sdxy /mnt/root Code:
sudo mount -t proc none /mnt/root/proc Code:
sudo grub-install "(hdx,z)" |
Using a USB flash drive to house Grub is a bad idea.
Reason: If you install a Linux in an internal hard disk and boot from it that disk is the 1st boot disk and all the intructions in the boot loader and Linux register it being installed from the position of 1st boot disk. If you boot from a USB device you tell the Bios that is your new 1st boot disk and so the internal hard disk has to be relegated to the 2nd disk position and this can cause booting problem. Cure: Put Grub in a floppy or CD (with or without the booting menu.) If a floppy or a CD is booted the original 1st boot disk is still valid and not altered. Advice: Forget about the booting menu. Just a Grub prompt will do. This mean just install Grub1 or Grub2 on its own. Check out just booting tips in my signature for putting Grub in various devices. No installed operating system in a PC cannot be booted manually by a Grub prompt. |
Quote:
|
Saikee,
Ok, I am now on hold till I have further confirmation... You said it is a "bad idea" due to the reasons given. Is there any way to overcome that problem? I read about issuing commands to "re-map" the 1st and 2nd disks "on-the-fly". Or is it not applicable here? I am going thru the links you posted on your signature, and even on second reading, it all seems to be a bit "beyond me" - since my basics are rather fudgy! Any way, I will await clarity before I proceed further. Thanks a ton, for raising a alert - since I do not want to ruin the current setup! cr4321 |
cr4321,
Yes you can use "drivemap" in Grub2 or "map" in Grub1 to change the disk order. It is a bit of inconvenience which is avoidable if you put Grub in a CD or a floppy. Once booted up, say from a CD, you can ask Grub to boot any disk, USB device, flash drive etc. In your following skykooler the error Code:
mount: according to mtab, /dev/sdc1 is mounted on /media/usb0 Code:
sudo su |
Saikee,
Please confirm if I can try out the commands as you said. Is there a risk that I will spoil the current setup? At least if it will not disturb the disks (minus the usb) - then it is okay for me. Also, I am not clear about the "mapping" - I read about it but lack exact meaning - or how to do it in the current situation! Thanks... I will await your input.. |
To put Grub2 in a partition all you need is to mount that partition and do a "grub-install". The extra bits are to ensure the dev names etc are available during the grub-install.
The "map" command must be used in pair for exchange the order of two disks. In Grub1 the disk (hd0) and (hd1) will be swapped after these two statements Code:
map (hd0) (hd1) Lilo also provide something similar. The disk order is given from the Bios information. Grub just acts a a middleman to swap the two disk order. After the Linux has been powered down the original disk order prevails. |
This is what I get. Can't understand the "error" - since it has already created the 2 directories on the USB.
(I am using Grub2 (Ubuntu 10.04) cr@cr-ub10:~$ sudo su [sudo] password for cr: root@cr-ub10:/home/cr# mkdir /media/usb0/proc root@cr-ub10:/home/cr# mkdir /media/usb0/dev root@cr-ub10:/home/cr# mount -t proc none /media/usb0/proc root@cr-ub10:/home/cr# mount -o bind /dev /media/usb0/dev root@cr-ub10:/home/cr# grub-install /media/usb0 /usr/sbin/grub-probe: error: /media/usb0 is not a block device. /usr/sbin/grub-setup: error: cannot seek `/media/usb0'. Can you tell me how to overcome the error? Thanks... |
This is what I get. Can't understand the "error" - since it has already created the 2 directories on the USB.
(I am using Grub2 (Ubuntu 10.04). I do not have a floppy drive, and I do not know how to install grub in a CD! cr@cr-ub10:~$ sudo su [sudo] password for cr: root@cr-ub10:/home/cr# mkdir /media/usb0/proc root@cr-ub10:/home/cr# mkdir /media/usb0/dev root@cr-ub10:/home/cr# mount -t proc none /media/usb0/proc root@cr-ub10:/home/cr# mount -o bind /dev /media/usb0/dev root@cr-ub10:/home/cr# grub-install /media/usb0 /usr/sbin/grub-probe: error: /media/usb0 is not a block device. /usr/sbin/grub-setup: error: cannot seek `/media/usb0'. Can you tell me how to overcome the error? Thanks... |
To make a Grub2 CD just follow the Task K2 of "just booting tips" in my signature.
I copied skykooler's commands quickly. Come to think of it the correct syntax should be Code:
grub-install --root-directory=/media/usb0 /dev/sdc The kubuntu forum site has a very detailed Grub2 Guide which is among the best to be found. |
Wow, that was finally accomplished. ahem. Thanks again.
Now I have 3 new directories in the USB - /boot, /dev and /proc The message was : [code] root@cr-ub10:/home/cr# root@cr-ub10:/home/cr# grub-install --root-directory=/media/usb0 /dev/sdc Installation finished. No error reported. root@cr-ub10:/home/cr# [code] Just now I rebooted the machine with the USB. I think, I got the same menu as before. (it did not seem as if it booted thru the USB and even if it did, it did not show up the boot menus of the 2nd disk. I will recheck that, but would like your comments too. Meanwhile, I am reading your link about booting from cd. 1st line creates a rescue image. What does the 2nd line mean? - wodim? [code] grub-mkrescue --image-type=cdrom /tmp/grub-rescue.iso wodim /tmp/grub-rescue.iso [code] |
Hello,
Everything seemed all right as below : root@cr-ub10:/home/cr# grub-install --root-directory=/media/usb0 /dev/sdc Installation finished. No error reported. But want to re-confirm that it is not booting from USB... |
I think the two lines combined create an iso image for Grub2 to be burnt into a CD. I have done it couple of time and can confirm that it works.
You can use any Grub to boot any device. Here is how you do At the Grub2 booting screen do not boot any system but just press the "c" key to drop into a Grub prompt. Issue command [code[ls[/code]which will list out all the partitions in the PC. These are just (hd0,1), (hd0,2)...(hd1,1), (hd1,2).. By examining the number of partitions in each disk you should have an idea which is which. As you have sdc as a flash drive and created sdc1 there so mostly this would be (hd2,1) assuming you have not instruct the Bios to boot flash drive first and there are sda and sdb disks in the PC. You can then try the Grub2 you have just installed in sdc by command Code:
set root=(hd2) Equally when you are in sdc Grub2 prompt you can jump back to the original disk (hd0) by commands Code:
set root=(hd0) |
Saikee,
Thanks for all the guidance so far, but I think I missed something somewhere ... or there is some other problem that I can't identify. I checked all over that I have carried out the instructions right. The current situation is : 1) USB a) It is not booting from USB - in spite of BIOS settings. b) from grub prompt "ls" shows only (hd0) and (hd1) drives. c) set root=(hd2) - does not show any error. d) "chainloader +1" and "boot" command give error "h2 cannot get C/H/S values. (missing to give the drivemap command at some point ?) d) set root=(hd0) + chainloader +1 + boot - leads to disk1 menu e) set root=(hd1) + chainloader +1 + boot - leads to windows on disk i f) (maybe because there is no grub in disk2 ?) 2) As for the CD - I tried mkrescue ...... (it shows error - "unknown command - mkrescue" (doubt - even tab did not show that command) I am somewhere near, I know - just requires that one last push!!! Do give me some hints at they occur to you. Thanks for all your help so far. |
I used (hd2) in thinking since your USB disk is sdc so there must be sda and sdb. The corresponding names to Grub2 are therefore (hd0), (hd1) and (hd2).
To solve the mystery can you post here the output of Code:
sudo fdisk -l It has been known that for some mobo booting from a USB device is only possible if the Bios function "legacy USB support" is switched on. If the mobo does not have this function then it can either able to do it automatically or technically unable to carry out such a task. The mkrescue is a system command and might require you to be in root. Thus you can either prefix the command with "sudo", like "sudo mkrescue ...." or you type "su" once, supply your password and then able to work with the system commands untit you exit the terminal. |
Sorry, I did not notice that this has jumped to page-2 - hence the delay!
Now, the output as you requested for sudo fdisk -l : cr@cr-ub10:~$ sudo fdisk -l [sudo] password for cr: Disk /dev/sdb: 40.1 GB, 40060403712 bytes 255 heads, 63 sectors/track, 4870 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00049ce0 Device Boot Start End Blocks Id System /dev/sdb1 * 1 1046 8401963+ b W95 FAT32 /dev/sdb2 1047 4870 30716249+ f W95 Ext'd (LBA) /dev/sdb5 1047 2092 8401963+ 83 Linux /dev/sdb6 2093 3138 8401920 83 Linux /dev/sdb7 3139 4087 7622811 83 Linux /dev/sdb8 4088 4609 4192933+ b W95 FAT32 /dev/sdb9 4610 4870 2096451 82 Linux swap / Solaris Disk /dev/sda: 160.0 GB, 160041885696 bytes 255 heads, 63 sectors/track, 19457 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00070b56 Device Boot Start End Blocks Id System /dev/sda1 * 1 3916 31455238+ c W95 FAT32 (LBA) /dev/sda2 3917 19457 124833082+ 5 Extended /dev/sda5 3917 7832 31455238+ b W95 FAT32 /dev/sda6 7833 11748 31455238+ b W95 FAT32 /dev/sda7 11749 15664 31455238+ b W95 FAT32 /dev/sda8 15665 19457 30467241 b W95 FAT32 Disk /dev/sdc: 1027 MB, 1027603456 bytes 255 heads, 63 sectors/track, 124 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x01040a50 Device Boot Start End Blocks Id System /dev/sdc1 * 1 125 1003487+ c W95 FAT32 (LBA) Partition 1 has different physical/logical endings: phys=(123, 254, 63) logical=(124, 237, 47) cr@cr-ub10:~$ Meanwhile, since it was not booting to USB - I tried out another USB (which I had used for booting SLAX, which I formated - to eliminate the possibility of unbootable USB). Still, it is not booting after grub2 was installed on it. Moreover, at grub prompt - the command "ls" only shows (hd0) and (hd1) with all their respective partitions. Thanks s far... |
Finally, for the grub rescue cd part, this was generated and a cd was written.
This was the terminal commands, and the output messages : Quote:
|
Can I summarize your current status as follow?
(1) Putting Grub2 into a USB stick still not working according to my suggested commands. (2) You now managed to create a Grub2 bootable CD but its result has not been reported. I have tested the commands myself and it seems if you format the USB stick to a Ext2/3 filing system everything will work out alright. This could be a bug or error as Grub2 does not have a shell and relies exclusively on Linux for its installation by using a Bash terminal command "grub-install". In that the developers may not have expected the user to put Grub2 directly into a fat32 partition. I therefore got a complaint saying the filing system not recognised. If the partition you want Grub2 inside is a Ext2/3 type then everything will be perfect. I have successfully booted up my USB stick using exactly the following commands (A) First I umount my USB stick by command Code:
umount /dev/sdc1 (B) I use "cfdisk /dev/sdc" to alter the partition type to 83 for Linux, select "write" and "quit" cfdisk. Then I check the disk layout to make sure sdc1 has a partition type 83 by command Code:
fdisk -l Code:
mkfs.ext2 /dev/sdc1 Code:
mkdir /mnt/sdc1 Code:
grub-install --root-directory=/mnt/sdc1 /dev/sdc Code:
root (hd2) The above is the quick way to put Grub2 in a empty ext2 partition in a USB stick that will fire to a Grub2 prompt which can be used to boot up any "installed" operating system in a PC. Its use is identical to Grub1 except somecommands are slightly different. |
Ah, am I glad to see your post!!! (I will try out the USB trick a little later, and I am sure your suggesion could be the solution, since I have tried almost everything else - and somewhere along the way, I think I have made a mess of the MBR of (hd1) - and on booting, now it shows a "read error"!
First - yes, the CD is booting to grub prompt and I have all the grub commands available there. Is there a way to restore the boot menu of hd1 from the CD? Meanwhile, I will follow your instruction for the USB - using my live 10.04 Ubuntu cd. I think I am really testing your patience, and I really appreciate your patience. Warm regards. |
In a normal application your PC only boot up the first hard disk and that should be your (hd0) or sda.
The MBR in the (hd1) is a spare. This is because the boot loader controlling the MBR should be able to boot every partition in every hard disk. The first partition of (hd1) is (hd1,1) in Grub2 term so (hd1) has no usable purpose at all. Your sdb or (hd1) has Linux inside, right? My guess is it is in sdb5 which if true can be checked by your newly made bootable CD by command Code:
set root=(hd1,5) Code:
configfile /boot/grub/grub.cfg Once Linux in sdb5 has been booted you can choose it to be linked to the following possibilities (1) Letting the boot up Linux to control the MBR (that is (hd0) or sda you issue this command in a root terminal Code:
grub-install /dev/sda Code:
grub-install -force /dev/sdb5 Code:
set root=(hd1,5) Code:
grub-install /dev/sdb Code:
set root=(hd1) Don't worry about my patience. Booting is in fact very easy in Linux. You will eventually be able to put 1 and 1 together to get 2. |
Saikee,
Wow, done it! The USB is now booting to Grub - and I managed to chainload into (hd1) and lo I got the menu which I thought, I had washed away along with the MBR!! So, now I have a USB and CD booting to Grub - A big thanks to you. Now please help me with this : Quote:
I don't want to spoil your week-end, but doing this, I had a gr8 weekend! Regards. Thanks, you been wonderful, and I now have some idea about this .. This is some achievement.. |
Please Note :
In the above, I forgot to tell you that my BIOS is not recognizing this USB as a USB. It has gone into the list of Hard Disks! |
To copy a Grub2 menu into a USB flash drive sdc1
When you boot up a Linux its Grub2 menu is /boot/grub/grub.cfg. So if you want to copy it into device sdc1 you mount it first and use a file copying command to move it across. However when a USB device is attached to a modern Linux it will get mounted automatically by the kernel. In your previous post the mounting position has been confirmed to be /media/usb0. Thereafter you can use a terminal to copy the file across like Code:
sudo cp /boot/grub/grub.cfg /media/usb0/boot/grub/ This is no different to you open up one file manger (click "place" and "Home folder" and navigate to /boot/grub to drag grub.cfg to another file manager opened to the folder of /media/usb0/boot/grub. The GUI method sometimes refuses to work if you as a user does not own the files. To put Ubuntu onto sdc1 This is a new request as your current thread relates to putting Grub2 as a stand alone system. You do not need Ubuntu put inside sdc1 in order to use the Grub2 menu. The steps described above is all you need. Basically Grub2 when boot to a media like sdc1 will check if grub.cfg exists. It it exists it will execute it otherwise it defaults to a Grub prompt. Therefore having a grub.cfg is the same of having it from the hard disk. When you put Ubuntu into a flash drive you actually install it and there are two methods. Method 1 - Normal installation The flash drive is just a hard disk in Linux so you can install Ubuntu just like you did in the hard disk except you now use sdc1 and not sdb5. This type of installation is about 3 to 5 times larger than the CD image as many files are expanded and various settings are recorded. As a CD is 700Mb so an installed Ubuntu can be around 3.5Gb. This can take a very long time!!!!!!!! Once installed your Ubuntu is specific to the PC it has been installed with. Method 2 - Frugal install In this method you effective just copy the contents of the Ubuntu CD into sdc1. UBuntu in a CD is shipped to boot from a CD and has the boot loader called ioslinux which boot a filing system created in according to ios9660 standard. Isolinux does not boot a hard disk so you need to change to a hard disk boot loader like Grub2. This part you have already done. Once you copy the contents of Ubuntu CD onto the sdc1 you still need to write instruction to boot Ubuntu. The easiest is to follow isolinux's configuration file and convert it to Grub2. You can post the ioslinux.cfg here for advice. If you have no CD drive but just an iso from ubuntu then you need to mount the iso file on a loop back device, kind of like a virtual CD, in order to read/write the contents inside an iso file. The mounting instruction is given in most reference on the mount command, man page of mount etc. I have written a thread showing how to combine several iso into a DVD and then on a flash drive. You can see the details of mounting instruction and conversion of ioslinux to Grub commands (mainly for Grub1). In a Frugal install you use Ubuntu as a Live CD. The expansion of file takes place in the memory and as a rule settings are not saved but the flash drive can be used on any PC. |
Saikee,
Thanks a ton for all you have given. All I need for now is to get the menu into the USB. Once, I do that, (and I have been also reading your other posts) I get the confidence to boot my systems (from grub, or otherwise), and like earlier will not have to re-install everything once I mess up the menu! (It has happened many times in the past!) Once I am a bit familiar with this, I will go further and experiment and understand things better! So, for now I will just pause and get the grub config file into the usb following your instructions. I will come again to post the final observations. I feel mighty pleased with my efforts - though in your parlance, I may have not even scratched the surface! Of course, all credit to you - to teach a 60 year old the ABC's of grub! With best regards. I will be seeking you out again, for sure! |
Saikee,
Used a brute force method to copy the grub.cfg file into the USB. (had to!) and finally have accomplished everything what I had set out to do! That's a great feeling. Thanks all the way to you. (why had to!) As I earlier mentioned, in the course of experimenting, I had overwritten/erased the boot menu/mbr of the the disk which had the XP+Linux OS's. (It was giving a read error). So, I did a "FIXMBR" and "FIXBOOT" with the XP recovery. This erased the Grub menu and it directly boots Windows. (In the normal course, I would be actually reinstalling all the OS's and the programs! Now with the additional knowledge, and after copying the grub.cfg file into USB, I am getting the menu alright and also booting into the system. I know I should be doing a "grub update" - but I don't want to risk a mess-up at this stage - so : (I will be glad to learn the final tip. To safely reverse the boot menu back to the disk!!) Warmest regards. |
You can get back to where you were by
Boot up the Ubuntu and do a grub-install to the first boot disk sda. In root terminal of booted up Ubuntu from sdb5 this command will link your Ubuntu to the MBR of sda Code:
grub-install /dev/sda Code:
menyentry 'Winodws in sda1 known to Grub2 as (hd0,1)' { |
Saikee,
Quote:
Thanks a lot, and be seeing you. Greatly appreciate your efforts. It was wonderful. (A point of interest on the side. Since the USB is recognised as a "Drive" in my BIOS, I tried to install Ubuntu 10.04 into a USB after creating partitions. Surprisingly it works - albeit quite slow!) |
All times are GMT -5. The time now is 03:28 AM. |