LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Fedora (https://www.linuxquestions.org/questions/fedora-35/)
-   -   HOWTO: reboot to Windows with one click (https://www.linuxquestions.org/questions/fedora-35/howto-reboot-to-windows-with-one-click-145115/)

jspaar 02-12-2004 01:21 AM

HOWTO: reboot to Windows with one click
 
This is for anyone who dual boots Fedora and Windows. Since Fedora is my default in GRUB, when I need to reboot into Windows I normally have to wait for the GRUB menu to come up so I can select Windows. I wanted a command that would let me do this directly from Fedora. And since I couldn't find one, I wrote this. Now I have an icon on the Gnome panel that lets me boot to Windows in one click, while leaving the default boot as Fedora.

If you want to try it, here's how:

Updated March 01, 2007: Installation is now much easier than the instructions below. There is a downloadable rpm available for FC6. To see what bootnext looks like, and for download instructions, click here.
*disclaimer* -- I've run this on my box without problems. It doesn't do anything super smart to tell Gnome to shut down nicely, so make sure you save open files before you run it. Don't sue me, I'm already broke.*/disclaimer*

bootdos - the main program
Below is the python code for the bootdos program. You'll need to be root (or use sudo) to install it:
$ sudo mv bootdos /usr/local/sbin/bootdos
$ sudo chmod 0755 /usr/local/sbin/bootdos

Code:

#!/usr/bin/python
 
# bootdos: Tell GRUB to boot Windows for the next boot only, then reboot
#
# Written by Jack Spaar <jspaar at myrealbox dot com>
# Created Wed Feb 11 20:01:30 PST 2004
# v0.1
 
import re, os, sys
 
progName = "bootdos"
WINDOWS_TITLE = "DOS"
GRUB_CONFIG = "/etc/grub.conf"
GRUB_PATH =  "/sbin/grub"
 
def grub_title_to_index(searchTitle):
        f = open(GRUB_CONFIG, 'r')
        lines = f.readlines()
        f.close()
 
        p = re.compile('^\s*title\s*(.*)')
        titleNumber = -1
        titleCount = 0
 
        for line in lines:
                m = p.match(line)
                if m :
                        titleCount += 1
                        if ( m.group(1) == searchTitle ) :
                                titleNumber = titleCount - 1
                                break
 
        return titleNumber
 
 
## MAIN
num = grub_title_to_index(WINDOWS_TITLE)
if (num == -1) :
        print "%s: %s not found in %s" %(progName, WINDOWS_TITLE, GRUB_CONFIG)
        sys.exit(1)
 
# Now tell GRUB we want that other OS for the next boot only
grubCommands ='savedefault --default=%s --once\nquit\n'%(num)
 
print "grubCommands =",grubCommands
pipe = os.popen(GRUB_PATH, 'w')
pipe.write(grubCommands)
pipe.close()
 
# Now reboot
os.system('/sbin/shutdown -r now &')
 
sys.exit(0)

At this point you could test the program from the command line. Provided that the GRUB menu entry for your Windows partition is entitled "DOS", it will reboot into Windows:
$ sudo /usr/local/sbin/bootdos

Configuring PAM
The next steps allow us to run the program without using sudo. This is good because we want out clicky icon to run without asking for a password. It's a little complicated to set up, but makes it easy to use.

Cut-and-paste the following code to a temp file named 'xx':
Code:

USER=<user>
PROGRAM=/usr/local/sbin/bootdos

Install it like this:
$ sudo mv xx /etc/security/console.apps/bootdos
$ chmod 0644 /etc/security/console.apps/bootdos

Now paste this chunk to another temp file, say 'yy':
Code:

#%PAM-1.0
auth      sufficient  pam_rootok.so
auth      required    pam_console.so
#auth      required    pam_stack.so service=system-auth
account    required    pam_permit.so

$ sudo mv yy /etc/pam.d/bootdos
$ sudo chmod 0644 /etc/pam.d/bootdos
$ sudo ln -s /usr/bin/consolehelper /usr/local/bin/bootdos

Now when we run /usr/local/bin/bootdos as a normal user it will call consolehelper to run /usr/local/sbin/bootdos as root. Phew. (Type 'man consolehelper' to understand more about how this works.)

You have these things installed so far:
/usr/local/sbin/bootdos -- the main program
/usr/local/bin/bootdos -- symlink to consolehelper
/etc/security/console.apps/bootdos -- tells consolehelper where the main program is
/etc/pam.d/bootdos -- tells PAM to give us root permissions when we run bootdos

Last step - make the clicky icon
1) right-click on an empty area of the gnome panel. Doesn't matter where, you can move it later.
2) A menu should appear, select Add to Panel->Launcher...

Now in the Create Launcher dialog,
3) Fill in the name field with something like "Boot to Windows"
4) Fill in the command field with: /usr/local/bin/bootdos
5) Click the "No Icon" after the Icon field to browse for an icon. Pick what you like, and OK.
6) Click OK back on the Create Launcher dialog.

You're done. You now have a one-click boot-to-windows button. W00t!

PS - if you find out there's already some super-easy pre-fabricated way to do all this don't tell me.
;)

dr0ker 02-12-2004 04:28 PM

Very cool jspaar! I will try that when I get home tonight. I always hate sitting through reboots.

/bin/bash 03-13-2004 11:16 AM

jspaar
Did you know there's already some super-easy pre-fabricated way to do all this? :D

No really, it's called rebootin, and it's a perl script that reboots your computer and allows you to select what to boot into on the next boot.

jspaar 03-15-2004 01:12 AM

Quote:

Originally posted by /bin/bash
jspaar
Did you know there's already some super-easy pre-fabricated way to do all this? :D

No really, it's called rebootin, and it's a perl script that reboots your computer and allows you to select what to boot into on the next boot.

Oh NOW you tell me! :rolleyes:

Actually that's pretty cool bash. :p I decided to add the ability to select the next boot as well.
It's gui-based instead of command line.

[I was going to learn pygtk in order to do it, but zenity saved the day. Zenity's only
annoyance is that you have to click the radio buttons, not the title names.]

So here's the latest version, for fools like me who don't already have "rebootin":

Code:

#!/usr/bin/python
 
# bootdos: Tell GRUB to boot Windows for the next boot only, then reboot
#
# Written by Jack Spaar <jspaar@myrealbox.com>
# Wed Feb 11 20:01:30 PST 2004 -- Created.
# Wed Mar  3 18:05:05 PST 2004 -- added OK/Cancel prompt.
# Sun Mar 14 22:28:07 PST 2004 -- use zenity to choose grub title to boot.
# v0.3
 
import re, os, sys
 
progName = "bootdos"
DEFAULT_TITLE = "DOS"
GRUB_CONFIG = "/etc/grub.conf"
GRUB_PATH =  "/sbin/grub"
 
ZENITY_WIDTH = 275
ZENITY_HEIGHT = 325
 
def grub_title_list():
        f = open(GRUB_CONFIG, 'r')
        lines = f.readlines()
        f.close()
 
        p = re.compile('^\s*title\s*(.*)')
        tlist = []
 
        for line in lines:
                m = p.match(line)
                if m :
                        tlist = tlist + [ m.group(1) ]
 
        return tlist
 
 
## MAIN
 
titles = grub_title_list()
 
# form a zenity command to allow user to select from the grub title list
zen = 'zenity --width %d --height %d --list --radiolist --column "Selected" --column  "Boot Title"  ' % (ZENITY_WIDTH, ZENITY_HEIGHT)
 
for title in titles:
        if (title == DEFAULT_TITLE) :
                zen += " TRUE "                        # Turn on radio button for this title
        else:
                zen += " FALSE "
        zen += "\"%s\"" % (title)
 
# run zenity and grab the user's choice
pipe = os.popen(zen, 'r')
choice = pipe.read().strip("\n")
okCancel = pipe.close()
if (okCancel != None):
        print "Cancelled."
        sys.exit(okCancel)
 
# convert the title to a grub index number
num = 0
for title in titles:
        if (title == choice):
                break
        num += 1
 
# Now tell GRUB we want that other OS for the next boot only
grubCommands ='savedefault --default=%s --once\nquit\n'%(num)
 
pipe = os.popen(GRUB_PATH, 'w')
pipe.write(grubCommands)
pipe.close()
 
# Now reboot
os.system('/sbin/shutdown -r now &')
 
sys.exit(0)


jspaar 05-14-2005 12:04 AM

Here's a slightly updated version. You can specify the default Grub title choice on the command line (or properties page of the panel launcher).

The default title can be a Python regular expression. If you don't put one on the command line, the first non-Fedora title will be selected using this expression: "(?!.*Fedora)". So if you have only Fedora and Windows installed, Windows will be the default selection for your next boot.

Code:

#!/usr/bin/python

# bootnext: Tell GRUB to boot Windows (or other menu choice) for the
# next boot only, then reboots
#
# Allows user to choose which OS to reboot into for next reboot only.
# Useful for folks who dual-boot another OS occasionaly but don't want to
# permanently change the default.
#
# Default menu choice is set to match expression DEFAULT_TITLE_RE.
# Must be run with superuser priveleges.
# The menu is graphical, and requires that X be running.
#
# Written by Jack Spaar <jspaar@myrealbox.com>
# Changelog:
# Wed Feb 11 20:01:30 PST 2004 -- Created.
# Wed Mar  3 18:05:05 PST 2004 -- added OK/Cancel prompt.
# Sun Mar 14 22:28:07 PST 2004 -- use zenity to choose grub title to boot.
# Thu Jan 27 22:32:41 PST 2005 -- allow a regex for finding default title
# Wed Feb  2 19:56:11 PST 2005 -- allow default title on command line
#
# v0.8

import re, os, sys

progName = "bootnext"

# Edit the following to set the default menu selection
DEFAULT_TITLE_RE = "(?!.*Fedora)"      # match first non-Fedora title.

GRUB_CONFIG = "/etc/grub.conf"
GRUB_PATH =  "/sbin/grub --batch --no-floppy"
DIALOG_TEXT = "Select which OS to boot next:"

ZENITY_WIDTH = 295
ZENITY_HEIGHT = 325

def grub_title_list():
        f = open(GRUB_CONFIG, 'r')
        lines = f.readlines()
        f.close()

        p = re.compile('^\s*title\s*(.*)')
        tlist = []

        for line in lines:
                m = p.match(line)
                if m :
                        tlist = tlist + [ m.group(1) ]

        return tlist


## MAIN

if (len(sys.argv) > 1) :
    default_title_re = sys.argv[1]
else:
    default_title_re = DEFAULT_TITLE_RE

titles = grub_title_list()

# form a zenity command to allow user to select from the grub title list
zen = 'zenity --width %d --height %d --list --radiolist --title "%s" --text "%s" --column "Selected" --column  "Boot Title"  ' % (ZENITY_WIDTH, ZENITY_HEIGHT, progName, DIALOG_TEXT)

p = re.compile(default_title_re)
default_found = 0
for title in titles:
        # Turn on radio button for 1st title matching DEFAULT_TITLE_RE
        if ((not default_found) and (p.match(title))):
                zen += " TRUE "
                default_found = 1
        else:
                zen += " FALSE "
        zen += "\"%s\"" % (title)

# run zenity and grab the user's choice
pipe = os.popen(zen, 'r')
choice = pipe.read().strip("\n")
okCancel = pipe.close()
if (okCancel != None):
        print "Cancelled."
        sys.exit(okCancel)

# convert the title to a grub index number
num = 0
for title in titles:
        if (title == choice):
                break
        num += 1

# Now tell GRUB we want that other OS for the next boot only
grubCommands ='savedefault --default=%s --once\nquit\n'%(num)

pipe = os.popen(GRUB_PATH, 'w')
pipe.write(grubCommands)
pipe.close()

# Now reboot
os.system('/sbin/shutdown -r now &')

sys.exit(0)


mpajoh 08-02-2006 08:17 AM

This is great jspaar.
I tried it and it works fine.
Request: Could you please add the the option to shutdown:
Code:

shutdown -h now
to the selections also?

Thank you very much.

Masoud

jspaar 08-02-2006 01:09 PM

Quote:

Originally Posted by mpajoh
This is great jspaar.
I tried it and it works fine.
Request: Could you please add the the option to shutdown:
Code:

shutdown -h now
to the selections also?

Thank you very much.

Masoud

Hi Masoud, glad you liked it. For anyone else stumbling across this thread, bootnext is available as an RPM package, so you can install it without all those steps. You can find it here.

I'll have to think about adding the shutdown option when I get some time. It wouldn't be complicated. Of course, now that suspend works on my desktop, I hardly ever shutdown anymore :p .

For now you could add the shutdown menu to your panel - right click in an empty area of the panel, choose "Add to Panel...", scroll down to "Shut Down" and click. HTH.

mpajoh 08-02-2006 01:29 PM

Quote:

Originally Posted by jspaar
. . .
For now you could add the shutdown menu to your panel - right click in an empty area of the panel, choose "Add to Panel...", scroll down to "Shut Down" and click. HTH.

Hi jspaar
I will try that, I do not remeber seeing "Shut Down" on "Add to Panel..." I use FC5.
I have seen logout in the menus but not Shutdown. I start Gnome via startx.

Anyway thanks for considering that.

Masoud.

Brianetta 03-01-2007 02:19 PM

jspaar, I installed the RPM you made available at dcheng(dot)members(dot)sonic(dot)net (I'm not able to write URLs here yet) onto my Fedora Core 6 workstation. Thanks for the utility; my USB keyboard doesn't (and will never) work in my GRUB menu, so this is a godsend when it comes to multi-booting.

It didn't work at first. Running bootnext as a non-root user came up with,
Quote:

The password you typed is invalid.
Please try again.
...without even asking for a password. I managed to solve the problem by editing the pam configuration file /etc/pam.d/bootnext as follows:
Code:

#%PAM-1.0
auth      required    pam_env.so
auth      required    pam_permit.so
account    required    pam_nologin.so
account    include    system-auth
password  include    system-auth
session    optional    pam_keyinit.so force revoke
session    include    system-auth
session    optional    pam_console.so

I hope this turns out useful in the event that others are having problems.

jspaar, I intended to send this directly to you, but you seem to have no public contact details. (:

jspaar 03-01-2007 05:22 PM

Brianetta,

Can you tell me what version of bootnext you have installed? You can check like this:

Code:

$ rpm -q bootnext
For FC6, you need bootnext-0.11-1 since -0.09 will only work with FC5 and earlier. If you had that problem with 0.11 let me know, I'll have to look into it.

Quote:

jspaar, I intended to send this directly to you, but you seem to have no public contact details. (:
I'm fine with posting here, but my email address can also be found in the bootnext man page. Either way is OK.

Brianetta 03-01-2007 05:49 PM

bootnext-0.11-1.fc6

It all works as it should (:

Well, bootnext does. Shame about grub. Nothing I do gets grub to use the default mechanism properly. savedefault does nothing at boot time, and in batch mode it seems to ignore --once completely. There is no grub_set_default command, and grub studiously ignored any file called "default" even when default saved is specified. I've given up; I can't multi-boot this machine unless I hook up an additional PS/2 keyboard.

mpajoh 03-02-2007 08:31 AM

So jspaar, did you decide against adding shutdown to your menue?

jspaar 03-03-2007 03:04 PM

Quote:

Originally Posted by mpajoh
So jspaar, did you decide against adding shutdown to your menue?

Hi mpajoh. Honestly, because of the way I use bootnext, I just haven't felt the need strongly enough to mess with it. In my case, I mostly use shutdown/hibernate and only occasionally use bootnext. So I have put the shutdown launcher in the middle of my gnome-panel, and keep bootnext off the the side.

But I'm curious - are you wanting to set the next boot *and* shutdown (instead of reboot)? Or are you just wishing bootnext could do a shutdown *instead* of setting the next boot?

mpajoh 03-05-2007 07:37 AM

Quote:

Originally Posted by jspaar
. . .
But I'm curious - are you wanting to set the next boot *and* shutdown (instead of reboot)? Or are you just wishing bootnext could do a shutdown *instead* of setting the next boot?

I am trying to see if could add another option to just shutdown the computer.
I use startx to log on gnome, doing so the standard gnome menue only has logout not shutdown.

Thanks for listening.


All times are GMT -5. The time now is 10:44 PM.