4

On my laptop, I've got a dual-boot configuration with Windows 7 and Linux OSes installed.

I am also using Linux via Oracle VirtualBox (through raw VMDK file) when working in Windows.

Since I have only one HDD, same bootloader (GRUB) instance is used in all these cases. Is it possible to somehow make GRUB choose Linux as default inside VM and choose Windows 7 otherwise?

Other options how to achieve the same behavior are also welcome.

1
  • 1
    I think Ярослав Рахматуллин is correct about not using the same grub for the VM and booting your computer. When you start a VM with Virtual Box, it should automatically start the selected OS. If you are having problems automatically booting into Windows, see his post. If you are having problems booting into linux using Virtual Box, please explain your problem in more detail.
    – viking
    Commented Jun 16, 2011 at 22:06

3 Answers 3

0

Um.. I don't think you are are using the grub on your hard drive to boot the VM. Am I confusing something, or you?

anyway.. There is a default directive in grub:

# grep default /boot/grub/menu.lst 
default 3

If windows is second from the top, you would set this to 1 because 0 is 1st OS entry.

you can change the value with sed (or an interactive editor, of course):

# sed 's/default 3/default 2/' -i /boot/grub/menu.lst 

to change the value. There are experimental drivers to mount ext3 in windows, i would not recommend any of them to be honest, not sure about ext4. Perhaps there are good drivers for windows.

0
0

You should probably be using a different boot device inside of your VM. You could simply create a disk image of a 5mb, and use that as your boot "drive" in the VM. Then you can install grub (or any other boot loader) there to launch Windows from your VM.

As a side note, I have to wonder how happy Windows 7 will be running off the same hard disk in a VM as well as on physical hardware... won't Windows go through some nasty identity crisis every time you change?

1
  • Thanks, it seems like it might work! I've not encountered any issues yet, I guess it's fine until they are on different partitions.
    – Eldar
    Commented Jun 17, 2011 at 0:20
0

I think, as this answer says, the way to do it is to install grub in a separate virtual partition. It wasn't obvious to me at first how to do this, so I'll share the steps I took, based on info from this question.

  1. I created a new virtual disk of size 20MB, allowing it to grow dynamically (when I chose 5MB fixed size, as the linked question suggested, it wasn't big enough).
  2. I attached the disk to the virtual machine, temporarily putting it in a SATA port that comes after the existing hard drive that's already booting, so that it doesn't try to boot from an empty drive.
  3. I booted the Linux virtual machine.
  4. I installed grub on the new virtual 20MB virtual disk. These are the commands I used to do this:

    sudo fdisk /dev/sdb Created a new partition with the n command, selected primary partition, and other default options, then exited fdisk.

    sudo mkfs.ext3 /dev/sdb1

    sudo mkdir /media/new_drive/

    sudo mount /dev/sdb1 /media/new_drive/

    sudo grub-install /dev/sdb --root-directory=/media/new_drive/

    Also, in my case, I wanted grub to immediately boot Linux, since it's the only thing it can boot. To do this, I edited /etc/default/grub to set

    GRUB_TIMEOUT=0

    GRUB_RECORDFAIL_TIMEOUT=0

    I'm not sure why the GRUB_RECORDFAIL_TIMEOUT was needed in my case, but it may relate to the fact that it's a virtual machine.

    Then I ran:

    sudo grub-mkconfig -o /media/new_drive/boot/grub/grub.cfg

  5. Shutdown the virtual machine.

  6. Switched the hard drives in the virtual machine so that the new hard drive with grub installed is now in the first SATA port, so that it boots first.

EDIT: I noticed that with this method, during software updates it sometimes regenerates grub.cfg based on your configuration in /etc/default/grub. This can be a problem if you have a different version of that file for your VM grub than you do for your non-VM grub. You might have to manually regenerate it in each setting. I'm not sure if there's a better way around this.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .