mycroes

There's always time to play
Showing posts with label grub. Show all posts
Showing posts with label grub. Show all posts

Thursday, September 30, 2010

Updating Ubuntu without removing grub-efi

Ubuntu still is trying to remove grub-efi everytime a new kernel arrives. I have a Mac Mini without a display, so grub-pc is useless for me, so how do I prevent this grub-efi removal all the time?

Simple solution, just tell apt you also want to install grub-efi, regardless of the availability of a new version:
$ sudo apt-get install linux-generic-pae grub-efi
Reading package lists... Done
Building dependency tree
Reading state information... Done
grub-efi is already the newest version.
The following extra packages will be installed:
linux-image-2.6.35-22-generic-pae linux-image-generic-pae
Suggested packages:
fdutils linux-doc-2.6.35 linux-source-2.6.35 linux-tools
Recommended packages:
grub-pc grub lilo
The following NEW packages will be installed:
linux-image-2.6.35-22-generic-pae
The following packages will be upgraded:
linux-generic-pae linux-image-generic-pae
2 upgraded, 1 newly installed, 0 to remove and 3 not upgraded.
Need to get 34.1MB of archives.
After this operation, 107MB of additional disk space will be used.
Do you want to continue [Y/n]?


And voila, grub-pc is a suggested package and no longer forced upon me! Thanks go out to Frank Groeneveld for suggesting the solution!

Sunday, April 4, 2010

Ubuntu Lucid on (X86) Mac Mini with EFI

I was gonna use my Mac Mini as replacement home server (uses less power than an idle Core 2 Duo + Geforce 8800GTS + 4 Harddisks), so I decided to put Ubuntu Lucid on it. Installation was really easy. I used the Lucid Lynx Beta 1 Server install CD, and installation went just fine. Press/hold c to boot from CD, be sure to create a seperate boot partition or keep some free space to create a seperate boot partition afterwards.

Just installing Ubuntu is no issue at all. You don't even need to create a seperate boot partition, because it'll boot just fine using the Mac's legacy booter. However if you're a Mac Mini owner and want to boot headless, there is only one solution (and a workaround that uses a 'dummy monitor dongle', which is not what I would want). This solution is making use of the EFI features of GRUB 2, which I'll detail in the rest of this post.

When the installation is done, install hfsplus and hfsprogs so you're able to create HFS+ volumes (you probably only need one of those, but it wasn't exactly clear for me which one to use and due to lack of time I haven't looked any further yet). Copy files from your boot partition to a temporary space, then unmount and format the boot partition as HFS+ (mine is /dev/sda2):
# mkfs.hfsplus -v boot /dev/sda2

Edit /etc/fstab to use the new boot partition as boot partition:
...
/dev/sda2 /boot hfsplus defaults 0 2
...

Unfortunately it seems UUID's can't be used for HFS+ volumes at the moment, so hardcode the device name in there.

After creating the volume mount it again and copy over all the files from your temporary space.

Now install grub-efi, this will automatically remove grub-pc. Generate a GRUB EFI executable using the following command:
# grub-mkimage -o /boot/grub/grub.efi -p /grub part_gpt hfsplus fat ext2 normal sh boot configfile linux

You actually don't even need the fat and ext2 modules and probably more can be stripped, but I haven't experimented with GRUB 2 a lot yet, that'll be for another day (and another post).

We're almost done, now it's on to getting the Mac to actually use our EFI enabled GRUB. First toggle the boot flag on your shiny HFS+ partition. Run parted (or any other GPT-aware partitioning tool) and type:
(parted) set 2 boot off

Parted will probably tell you the disk is in use and you need to reboot for the change to become effective, but it doesn't matter for us.

Now the final step is to tell the Mac (or actually, the filesystem) that our grub.efi is bootable so it'll show up in the Mac boot menu. There should be a utility call hfspbless, which allows you to do this from within Linux, however the first hit on Google doesn't seem to offer a quick guide, so I skipped this part. Instead, put in the Mac OS X install DVD. As soon as a menu bar shows up (I believe you have to click next at least once), fire up a terminal. In the terminal, enter the following:
# mkdir /Volumes/boot
# mount_hfs /dev/disk0s2 /Volumes/boot
# bless --folder=/Volumes/boot --file=/Volumes/boot/grub/grub.efi --label boot --setBoot

The bless command has now set some metadata on the HFS+ filesystem that the Mac uses to identify a native bootable image. I assumed label should set the label accordingly for the boot menu, however my entry showed up as 'EFI something' IIRC, but I can't care more or less since it's a server and I'll never see the menu anyway. Now reboot and enjoy!

This did the job for me, however there are a few issues I still have to take care of. The Mac created a fake MBR partition map for me, which I don't need and don't use. It now shows 'Windows' as option in the Mac boot menu, but luckily it starts Linux by default. Also, the MBR partition map is out of sync if you do stuff with the GPT partition table. I used rEFIt to resync the MBR table, but when I figure out how to remove the MBR that's what I'm going to do.

Also, there's a file called Volume Name Icon on my boot partition. I guess this is used for the Mac boot menu, so it probably can be changed easily too. However I have no clue what the format is, I'll have to look this up some day and change it for a genuine Tux!

Sunday, March 15, 2009

Creating a bootable USB flash drive formatted as NTFS

Next in the series of bootable flash drives: NTFS support.

I don't really care what filesystem my devices are, as long as I can read and write them. So FAT32 would be a decent choice, right? No. FAT32 has a file size limit of only 4G and I tend to store larger stuff on my Corsair Flash Survivor. I came up with two different solutions:
1. Create two separate partitions, one with stuff GRUB needs access to, one with large stuff like the filesystem images and my data,
2. Create a NTFS partition and boot from that.

Of course I first came up with solution 2, but I couldn't find much information about grub and ntfs, except for total lack of support for ntfs. I was thinking about solution 1 and then decided it sucks if you're forced to use two partitions while you don't really want to, so I went on looking for booting from ntfs drives.

After a while I came upon grub4dos. I can't say I really love it (without spending any time investigating it seems it's more of a hack than an enhancement to grub), but it's grub and it boots from ntfs formatted drives. Once I had that figured out, it was time to start doing some work.

First, partition the flash drive so there's a large ntfs partition (in my case the whole 16G). In my case there was already a partition there, if there's not you can skip deletion of the partition.
# fdisk /dev/sdx

Command (m for help): d
Selected partition 1

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-125, default 1): [return]
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-125, default 125): [return]
Using default value 125

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 7
Changed system type of partition 1 to 7 (HPFS/NTFS)

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

Calling ioctl() to re-read partition table.
Syncing disks.

Now it's time to create a filesystem on the flash drive, since we've chosen ntfs we'll need ntfsprogs, your linux distribution probably has it in it's repositories. Enter the following command to format the drive:
# mkfs.ntfs -L label -Q /dev/sdxY
Cluster size has been automatically set to 4096 bytes.
Creating NTFS volume structures.
mkntfs completed successfully. Have a nice day.

This shouldn't have been hard, if I actually needed to explain those steps to you at all, so let's continue with the serious part. The next few steps need a grub4dos archive. You can find one at their downloads page, I took grub4dos-0.4.4-2009-03-07.zip. Extract this and open a command line in the directory where the extracted files are. Now enter the following command:
# ./bootlace.com /dev/sdx

Disk geometry calculated according to the partition table:

Sectors per track = 63, Number of heads = 255

Success.

If it says Success or something similar, then grub4dos is on your flash drive. You can actually boot from it, however for grub4dos to be useful it needs some files. Copy grldr to your flash drive (of course you need to mount it for that). It should be in the root of the drive. Now create a menu.lst in the root of the drive too (yes, this differs from grub behaviour). Edit the menu.lst file so it resembles a usable grub menu.lst and you're done.

The following block is my menu.lst, if there's any interest in the files and layout I used then just leave a comment and I'll be sure to answer any questions about it.
default 0
timeout 60
splashimage=/boot/splash.xpm.gz

title Ubuntu Jaunty Alternate Install AMD64
kernel /ubuntu/jaunty/vmlinuz
initrd /ubuntu/jaunty/initrd.gz

title Gentoo Minimal X86
kernel /gentoo/x86/gentoo root=/dev/ram0 init=/linuxrc looptype=squashfs loop=/gentoo/x86/image.squashfs cdroot
initrd /gentoo/x86/gentoo.igz

title Gentoo Minimal AMD64
kernel /gentoo/amd64/gentoo root=/dev/ram0 init=/linuxrc looptype=squashfs loop=/gentoo/amd64/image.squashfs cdroot
initrd /gentoo/amd64/gentoo.igz

title GParted
kernel /gparted/vmlinuz boot=live union=aufs noswap vga=791 ip=frommedia nolocales live-media-path=gparted
initrd /gparted/initrd1.img

title Memtest 86+
kernel /memtest86/memtest86-3.5

title Boot first harddisk
rootnoverify (hd1)
chainloader +1

title Reboot
reboot

title Shut down
halt

Wednesday, February 11, 2009

Fallback kernels using GRUB (or safe rebooting of remote machines)

If you can't reach a certain computer (like a server that's colocated), but need to reboot it anyway, it's better to do it in a safe way. GRUB allows the use of the "fallback" command to specify a kernel to boot if it can't boot the kernel specified by default, but what if it can boot the default kernel but the kernel dies at some point? Well GRUB will be happy, it did boot a kernel.

The solution is in the "savedefault" option. When using "default saved" in the GRUB config file, GRUB will boot the kernel that was saved as default. Very easy to understand. But how can we use this to our advantage? Well let's assume we have a new kernel and an old kernel, named new and old, the config will probably look something like this:
default 0
fallback 1
timeout 5

title New
kernel /new root=/dev/sda1

title Old
kernel /old root=/dev/sda1

There's two things that need to be changed here. First the kernel needs to know what to do when it panics. Because there's one reasonable thing to do, it's implemented by means of a kernel commandline argument. Append "panic=5" to the kernel command line in the GRUB config and the system will reboot five seconds after the kernel panics.

The second change is that we need to tell GRUB that it needs to boot the second kernel if the first fails. We can't easily tell GRUB to boot the second only if the first has failed, but we can reliably tell GRUB to make the second default whenever the first boots. The changes will result in the following config (menu.lst):
default saved
fallback 1
timeout 5

title New
kernel /new root=/dev/sda1 panic=5
savedefault 1

title Old
kernel /old root=/dev/sda1

This config will instruct GRUB to set Old as the default kernel whenever New gets booted, and the "default saved" line will tell GRUB to boot the kernel saved as default. This can be chained to have multiple fallback kernels, but that's all up to you.

There's one issue left, if you want to reboot the system but the new kernel did work out, GRUB will still (correctly) assume it has to boot the old kernel. However there's a linux userspace utity that will allow you to change the default:
# grub-set-default 0

This will reset the default to 0. Of course if the system boots you can edit the menu.lst file by hand, setting the default to 0, but if you want to use it again you had better reset the default beforehand.

Creating a multiboot USB flash drive using GRUB

So I am currently booting my system from a USB flash drive, and probably will be for quite some time. One thing that annoyed me about syslinux is that it's really nothing more than a very simple boot loader, but sometimes you want more.

I've always used GRUB. GRUB has support for splash images so it looks nice. I also think the editing features of GRUB are great. Mistyped the kernel name after updating? Edit it in the menu. I also really like GRUB's default feature, which allows for some nice tricks, but that's for another post.

All in all, I wanted to get booting using GRUB. I've always installed GRUB 'by hand'. I've noticed the existance of grub-install, but the first time I ever read about it, I read about some issue with it. It was probably not important, but since I knew GRUB's setup commands by hand I didn't need it anyway. Turns out that if you run linux, GRUB will just map your USB flash drive as another harddrive (or at least it did for me with two different drives).

To install GRUB, it expects some files to be present. It also expects a filesystem to be present too if you install the same way I do. Because I like my USB flash drives easily accessible on multiple platforms, I format them as FAT32. I must say I only used them in linux so far, so if you're just like me you might as well format them ext2. In typable commands:
# mkdosfs -F 32 -n [somename] /dev/sdxy
Or:
# mke2fs -L [somename] /dev/sdxy

Of course sdxy refers to the partition on the flash drive you want to use, in my case it's /dev/sde1. You don't have to use labels (-n and -L options respectively), but I prefer to label my filesystems.

I also prefer to have my filesystems clean, so I try to keep all this bootable stuff contained in a folder. Mount your flash drive, and create a folder boot:
# mount /dev/sdxy /mnt
# mkdir /mnt/boot

I will be using the folder boot to store my kernels, initramfs's and the GRUB folder with GRUB files. GRUB needs just three (3) files to get installed: stage1, stage2 and the appropiate stage1_5. Easiest way to get these is from an existing install, if that's not possible for you, ask google for another solution (i.e.: "compile grub"). When using a FAT32 formatted flash drive you need to copy fat_stage1_5, when using ext2 or ext3 you should use e2fs_stage1_5.
# mkdir /mnt/boot/grub
# cp /boot/grub/stage{1,2} /mnt/boot/grub
# cp /boot/grub/fat_stage1_5 /mnt/boot/grub

GRUB now has all the files it needs, so let's install it:
# grub
grub> device (hd7) /dev/sdx

grub> root (hd7,z)
Filesystem type is fat, partition type 0xb

grub> setup (hd7)
Checking if "/boot/grub/stage1" exists... yes
Checking if "/boot/grub/stage2" exists... yes
Checking if "/boot/grub/fat_stage1_5" exists... yes
Running "embed /boot/grub/fat_stage1_5 (hd7)"... 16 sectors are embedded.
succeeded
Running "install /boot/grub/stage1 (hd7) (hd7)1+16 p (hd7,0)/boot/grub/stage2
/boot/grub/menu.lst"... succeeded
Done.

grub> quit

this maps (hd7) to /dev/sdx, the root line needs to be changed to match z = y - 1, in other words if you're using partition 1, enter a 0 for z, increment both equally for other partitions. You should now be able to boot from the USB flash drive, but we're not done yet. I like to have a config too, and I included a nice menu background, so I copied splash.xpm.gz over to /mnt/boot/grub. As for the config, here's mine for a gentoo 2008.0 minimal livecd squashfs image on my flash drive:
default 0
timeout 30
splashimage=/boot/grub/splash.xpm.gz

title Gentoo Minimal LiveCD 2008.0
kernel /boot/kernel-2008.0.img root=/dev/ram0 init=/linuxrc dokeymap looptype=squashfs loop=/minimal-2008.0.squashfs cdroot vga=791
initrd /boot/initrd-2008.0.igz

Name the file menu.lst and GRUB should be able to find it. If you now boot from the flash drive, you should see the menu. And as another bonus, booting entries seems to be way faster than it is with syslinux!

Sunday, February 8, 2009

Issues with syslinux

A small followup to my post about a syslinux bootable usb stick, I'm writing down the current issues I have ran into.
1. syslinux is not your average bootloader
2. syslinux on a fat32 usb drive doesn't like to get recognized as USB-FDD

Point 1 is easy to understand. Syslinux is made to be useful in the special cases, not to be useful in about any case. Want to boot to a different kernel? Edit the config file. I think grub is far more userfriendly than syslinux will ever be, so I'm definately doing that grub bootable usb drive soon.

Point 2 is something different... I don't know if grub will work when the system tries to boot from the usb device as if it's a floppy drive. I don't know what's going wrong for syslinux either, but I would not be amazed if either the system or syslinux decides that it can't read beyound 2.88M (or 1.44, but I guess it would at least support large floppy disks). If it's the system, then grub probably won't make any difference at all... So now you might ask, why would you want to boot with usb floppy drive emulation? Well, I don't want to, but it seems some machines have no other option depending on the kind of usb device you use...

Expect a grub multiboot usb drive howto soon.