Friday, March 16, 2012

How to restart from windows to linux / linux to windows on dual boot PCs


If you have a dual boot installation of Ubuntu and Windows 7 on the same PC like me, you need to switch windows for gaming or business purposes occasionally. Menu selection screen only shows few seconds and if you miss, you have to restart the PC again. I need a shortcut on both linux and windows desktop to switch OS after restart. 

You may want to use permanently default OS on grub and search "one time boot" options. Here I will explain a method for changing default OS by a small script on both windows and linux sides.

Please note that I'm using EXT2 file system on linux for this method to work. Last version 0.51 of ext2fsd software claims that they support writing to ext3/4 volumes too. However I didn't try it yet. If someone try this method with ext3 or 4 please notify me... 



On Windows side:




This software mounts ext2 partitions and maps to windows. 


  • Copy and edit the grub.cfg script that set the default OS as linux. 0 is Ubuntu, 5 is Windows on my PC's grub configuration. So I change the value to 0 to open the pc in linux OS. 

#

# DO NOT EDIT THIS FILE

#
# It is automatically generated by grub-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#

### BEGIN /etc/grub.d/00_header ###
if [ -s $prefix/grubenv ]; then
  set have_grubenv=true
  load_env
fi
set default="0"
if [ "${prev_saved_entry}" ]; then
  set saved_entry="${prev_saved_entry}"
  save_env saved_entry
  set prev_saved_entry=
  save_env prev_saved_entry
  set boot_once=true
fi

function savedefault {
  if [ -z "${boot_once}" ]; then
    saved_entry="${chosen}"
    save_env saved_entry
  fi
}

  • Create a batch script to copy and overwrite the grub.cfg file to linux partititon and restart windows.

batch script "restart to linux" is:


start /d "C:\Program Files\Ext2Fsd" Ext2Mgr.exe
timeout /t 10
copy d:\grub.cfg I:\boot\grub
shutdown /r /t 0


Meaning:

start /d "C:\Program Files\Ext2Fsd" Ext2Mgr.exe 

Here letter "I" is my drive that has grub config file "/boot/grub/grub.cfg".




timeout /t 10 


wait for 10 seconds to start the ext2fsd program and mount the drivers



 copy d:\grub.cfg I:\boot\grub

Copy the grub.cfg file which has default OS as linux



shutdown /r /t 0 


Reboot windows immediately.




On Linux side:


  • We need to use sudo without password in the script file.  Run visudo and add your username to its config file.

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

#includedir /etc/sudoers.d
username  ALL=(ALL) NOPASSWD:ALL


  • Copy and modify grub.cfg file that has default OS as windows. Here "5" is windows OS on my PC.

grub.cfg file must be like:

#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by grub-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#

### BEGIN /etc/grub.d/00_header ###
if [ -s $prefix/grubenv ]; then
  set have_grubenv=true
  load_env
fi
set default="5"
if [ "${prev_saved_entry}" ]; then
  set saved_entry="${prev_saved_entry}"
  save_env saved_entry
  set prev_saved_entry=
  save_env prev_saved_entry
  set boot_once=true
fi


  • create a file on linux desktop, name it as "reboot_to_windows", give execute grants



# chomod 777 reboot_to_windows


  • Add these lines to copy the modified grub.cfg file to /boot/grub directory and reboot linux
sudo cp -f /home/oturan/Desktop/grub.cfg /boot/grub
sudo reboot





Thats all! It worked for me, hope it works on your PC too. Bye.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.