6

Now I'm not sure if this is on topic or not, but if not, I have no idea where to post it, superuser does not seem appropriate because this relates to grub, but I regress.

When I restart Windows it boots to grub, and then the default grub option is Ubuntu, causing some very annoying times trying to install updates, so what I want to do is be able to edit the grub boot configuration temporarily to put Windows at the top after a restart, but after Windows was booted, it would go back to Ubuntu.

How would I do this?

  • Possible duplicate of askubuntu.com/questions/52963/… – anonymous2 Oct 7 '16 at 12:44
  • 1
    Also this may help: askubuntu.com/questions/81660/… – user589808 Oct 7 '16 at 12:45
  • So you're saying you edited grub, ran sudo update-grub and then it reverted to the default config? – user589808 Oct 7 '16 at 12:56
  • 1
    Migrate? @Rinzwind – David Oct 7 '16 at 13:24
  • 1
    Then it would be duped against the link ;-) I'd just follow the instructions there and close this one. – Rinzwind Oct 7 '16 at 13:27
8

The title of the question is what the OP proposed solution to the problem is. The title could be changed to "How can I have Grub boot to the last used OS choice automatically?".

This should arguably be default grub behavior. For example, I might be using Windows for a week playing Mass Effect IV and never come into Ubuntu. Or I might be testing something in Ubuntu 14.04 with a specific Kernel and I don't like having to pick it every time over 50 reboots.

There is a link in various comments above to a Stack Exchange answer proposed as a duplicate question. We can reference an AskUbuntu Q&A too: How to get grub2 to remember last choice?

In summary edit /etc/default/grub, comment out one line and insert two lines below it:

#GRUB_DEFAULT=0 # Rather than first menu option, we'll default to last  OS.
GRUB_DEFAULT=saved
GRUB_SAVEDEFAULT=true

Then save the changes and run sudo update-grub.

Voila! Whilst working in Windows and automatic updates happen at 3 am the system reboots to Windows and applies Stage 3 of 3.

OP is proposing "Edit Grub from Windows?" in the title as a solution to the problem. However, after stating the reason why, it becomes clear the title should be changed to something like: "How do I ensure Windows automatic update reboots go to Windows instead of Ubuntu?".

If the question was "How do I reboot to the last OS choice?" it's an obvious duplicate of the link. The proposed new title (or something shorter) has merit because others may search on the same problem. I know I've had the Windows update-reboot-update problem before and never would have imagined to search on the link here.

PS You can edit Grub from Windows but, why would you want to?

1

While editing a Windows partition content from Linux is doable, the other way around is not.

Windows doesn't natively support Linux FSs. A few tools can allow you to do it, though.But those wouldn't help.

I haven't test it myself, but it seems that recently someone ported GRUB2 to Windows 10, 8, 7, Vista and XP, both in BIOS and in EFI mode.

I suspect that, even in case everything works as expected it would'n be as easy as drinking a glass of water.

  • See unix.stackexchange.com/a/11431/10017 Using FSdriver and ext2 for your /boot you can edit files from windows on linux. – Rinzwind Oct 7 '16 at 13:48
  • There's much more than EXT3/EXT4! And in any case, you wouldn't be able to run the Linux tool to update the GRUB configuration from Windows. – EnzoR Oct 7 '16 at 13:49
1

I don't know how to do this from Windows, but you can do most of what you described with a GUI in Ubuntu.

This is how you can avoid having restart Windows Always Boot to Ubuntu

Perform the steps below. After those steps restarting windows will always boot to windows automatically. Restarting Ubuntu will always reboot to ubuntu automatically. You won't have to edit every time. Just configure Grub to remember the last choice. The last choice will always be the automatic default with not further manual changes each time.

Install the GUI Grub Customizer.

$ sudo add-apt-repository ppa:danielrichter2007/grub-customizer
$ sudo apt-get update
$ sudo apt-get install grub-customizer

From the GUI you can choose which Boot will be on top by selecting it then click on the Up/Down arrows to position how it's listed.

Under the general tab you can further customize Grub's behavior. You can also click on Advance to change enable, disable, add, remove, or change the lines of the grub file.

The temporary booting to Windows part can be achieved by:

1) Select the GRUB_DEFAULT entry -> Click the Value Column.  Replace the "0"
   with "saved"
2) Add an entry named "GRUB_SAVEDDEFAULT" -> Set the value to "true"
3) Now click Close -> Save (When you click save Grub will be updated to your new
   configuration)

Now after Windows is rebooted it will not go back to Ubuntu

Now when you are performing Windows Updates and other Maintenance the system will always boot to Windows (once you have booted Windows) until you Choose Ubuntu to go back into.

  • Does not answer " but after Windows was booted, it would go back to Ubuntu." See the link in comments. You can edit the file from Windows. – Rinzwind Oct 7 '16 at 13:42
  • 1
    I suspect the original question was about some "automatic" behaviour, not manual. – EnzoR Oct 7 '16 at 13:44
  • @Rinzwind The question says edit ubuntu from windows. Then his content described what he wants to do and ask how. My answer was a description of how to achieve what he described in the content. I'll edit my answer to make it clearer to how it relates to the question. – L. D. James Oct 7 '16 at 13:45
  • I see "Edit grub from Windows?" in the title? Part of his question is about getting back into another OS from Windows. That wont be possible without changing grub from windows. – Rinzwind Oct 7 '16 at 13:49
  • @Rinzwind I understand your interpretation of the OP's description. But it appears to be by his bold emphasis but after Windows was booted, it wold go back to Ubuntu. He appears to be (as he mentioned) annoyed that he can't work with Windows and stay with Windows until he finished with his Windows maintenance. It's the going to Ubuntu that he's having this problem with. The going back to Ubuntu will be resolved by the answer. After he finishes his Windows maintenance, without any changes to grub, just select where he wants to go when he boots. – L. D. James Oct 7 '16 at 13:56
1

Update: Resolved (mostly) - The reason this was so tricky was because Windows had flagged the ESP as hidden, so WSL was getting Access denied errors. Due to a MBR2GPT conversion I believe. Changing this wasn't straight forward, if anyone is curious I'll explain if you can't find your own solution.

I've added the final steps to achieve the windows half of this setup below, I'll omit the procedure to run WSL, it's easy (hint: search Linux in windows store).

My proposed answer requires UEFI and GPT boot, more common these days. The way I avoided the problem of Windows being unaware of Linux partitions was inspired by the way Windows handles booting.

The traditional way of booting in MBR was to hand off to /boot/grub (stage 2) and use the extra space to do things the MBR couldn't. The EFI partition is essentially a giant MBR on any disk you care to put it. The eureka moment was when I noticed that Windows basically put the stage 2 of Windows boot manager on EFI partition in the same directory as bootmgr.efi and bootmgfw.efi. So why not put Grub stage 2 on there too?

TL;DR

Step-by-step explaining how this answers the OP.

  1. ESP (EFI System Partition) is formatted FAT32 - perfect for cross-platform!

  2. On Linux, mkdir /boot/efi and then mount ESP on this;

    grub-install --boot-directory=boot/efi/EFI/grub /dev/nvme0n1
    

    and so forth. (Hopefully I'm not oversimplifying.)

  3. reiterating the solution proposed by others here to the main issue

    GRUB_DEFAULT=saved
    GRUB_SAVEDEFAULT=true
    
  4. The master reveal - #Grub Environment Block - grub/grubenv is on a FAT32 partition and is trivial to modify in Linux:

    grub-editenv /boot/efi/EFI/grub/grubenv set next_entry=X
    

    where X is the menuentry numbered from 0.

  5. Ok the commands are:

    mountvol b: /S
    wsl sudo mount -t drvfs b: /mnt/b
    wsl sudo grub-editenv /mnt/b/EFI/grub/grubenv set next_entry=0
    wsl sudo umount /mnt/b
    mountvol b: /D
    

See SU link at the bottom re: script

The final issue is I'm too tired to write a script to do this right now, the wsl commands don't work separately, the state doesn't carry across. I'm not a coder, I just need some time to work out how these three Linux commands can be strung together. This is enough to satisfy the "does this answer work" requirements for now. I'll update with a final version soon. Eg. You must launch the WSL terminal to use this solution at present.

I'll update this question to ask for help writing the batch script - How to script in Windows Subsystem for Linux (WSL) and call it from a batch file?

  • See the SU thread for the working script, I'll edit my answer here soon. – Jon T Jan 12 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.