Wednesday, June 22, 2011

Adding Windows to GRUB in Debian Squeeze

Recently I installed Debian 6 Squeeze on my notebook, where I already have an installation of Windows 7 Professional. For work reasons I couldn't delete this installation, so I installed Debian on a logical partition. During the installation everything went good, even at the moment of the GRUB configuration (GRUB detected the Windows Loader), but when I booted for the first time there wasn't an option to boot Windows. So this is the steps I made to add the menu entry.

The first step is to check the names of the partitions:

tepeyolotl@satellite:/$ sudo fdisk -l|grep NTFS
[sudo] password for tepeyolotl: 
/dev/sda1   *           1          13      102400    7  HPFS/NTFS
/dev/sda2              13       16908   135705600    7  HPFS/NTFS
tepeyolotl@satellite:/$ 

The fdisk command is only available to the superuser (you can also execute all of this commands from a root terminal), th -l option lists the devices and the outputis piped to the grep to be filtered. We note that the partition we search for is listed in "/dev/sda2".

The next step is create a script to indicate this info to the GRUB.

tepeyolotl@satellite:/$ sudo gedit /etc/grub.d/11_windows

This command opens the file (if it doesn't exists, it will be created), due to the directory in which we're working we need to execute this command as superuser. In the file we should type this:

#!/bin/bash
cat<<EOF
menuentry "Windows 7 Professional"{
set root(hd0,2)
chainloader (hd0,2)+1
}
EOF

The important thing here is the fragment "(hd0,2)" which indicates the partition with the OS that we are adding to the menu. Save the file, and give the file permissions as executable, after that, the only thing to do is update the grub.

tepeyolotl@satellite:/$ sudo chmod +x /etc/grub.d/11_windows
tepeyolotl@satellite:/$ sudo update-grub
Generating grub.cfg ...
Found background image: /usr/share/images/desktop-base/desktop-grub.png
Found linux image: /boot/vmlinuz-2.6.32-5-686
Found initrd image: /boot/initrd.img-2.6.32-5-686
Found Windows 7 (loader) on /dev/sda1
done
tepeyolotl@satellite:/$

And that's all, the menu entry is now ready to start Windows 7.

Agregar Windows al GRUB desde Debian 6 Squeeze

Recientemente instalé Debian 6 Squeeze en mi computadora, que previamente tenía instalado Windows 7 Professional. Como no quería deshacerme de Windows por motivos de trabajo, instalé Debian en una partición lógica.

Durante la instalación todo iba bien, incluso al generar el menú de arranque del GRUB detectó correctamente el arranque previo de Windows 7 en la otra partición, así que me despreocupé. Grande fue mi sorpresa al ver que una vez finalizado todo el proceso de instalación, la entrada que debiera arrancar Windows 7 no estaba en el menú de GRUB.

Leyendo en los release notes de GRUB 2 (que es el que maneja Debian Squeeze), descubrí que no había error, ya que GRUB2 detectó la partición pero jamás la agregó. Así pues me di a la tarea de ver como se resolvería esto.

Lo primero era ver que nombre tenían las particiones:

tepeyolotl@satellite:/$ sudo fdisk -l|grep NTFS
[sudo] password for tepeyolotl:
/dev/sda1 * 1 13 102400 7 HPFS/NTFS
/dev/sda2 13 16908 135705600 7 HPFS/NTFS
tepeyolotl@satellite:/$

El comando fdisk solo se encuentra disponible a través de root, y con el modificador -l listamos las particiones detectadas, para ser filtradas por el tipo NTFS (esa es la función del grep). Como ya conocemos el numero de partición que contiene el arranque de Windows 7 (/dev/sda2), procedemos a agregar un archivo para que la configuraciñon del grub lo lea:

tepeyolotl@satellite:/$ sudo gedit /etc/grub.d/11_windows

Con ese comando lo que hacemos es abrir el archivo mencionado con gedit y permisos de supersusuario (necesarios para trabajar en ese directorio). Una vez abierto el archivo se ingresa lo siguiente:

#!/bin/bash
cat<<EOF
menuentry "Windows 7 Professional"{
set root(hd0,2)
chainloader (hd0,2)+1
}
EOF

Lo importante aquí son los parametros que se le dan a "root", es decir la partición en donde está nuestro Windows. Guardamos el archivo, y de vuelta en consola le damos los permisos necesarios, y actualizamos el grub.

tepeyolotl@satellite:/$ sudo chmod +x /etc/grub.d/11_windows
tepeyolotl@satellite:/$ sudo update-grub
Generating grub.cfg ...
Found background image: /usr/share/images/desktop-base/desktop-grub.png
Found linux image: /boot/vmlinuz-2.6.32-5-686
Found initrd image: /boot/initrd.img-2.6.32-5-686
Found Windows 7 (loader) on /dev/sda1
done
tepeyolotl@satellite:/$

Y bueno eso es todo y ya se puede ver la entrada para arrancar Windows 7 en el menú de GRUB.