Shell – How to overwrite file residing on ESP at Linux boot

boot-loaderdual-bootrefindshell-script

I have a 64-bit dual-boot system running Windows 7 and Linux Mint 17 Cinnamon. I use rEFInd to graphically choose which system to boot.

A little bit about rEFInd:

  • It is an EFI bootloader.
  • It uses external, text-based configuration files (*.conf)
  • Configuration files can reference others.
    However, at runtime, only the external files
    referenced by the first file (refind.conf), are honored.
  • Particular properties that are referenced
    multiple times are handled as a cascade –
    that is, each reference overrides all previous
    references; only the last is honored.

…I have a few configuration files stored on my efi drive. At (efi) boot time, these configuration files are loaded:

  • refind.conf – the default, boilerplate settings file. This file references the following two files, in order.
  • settings.conf – a separate, clean slate for my favorite settings. It is referenced by refind.conf. Any settings here will override refind.conf.
  • auto_settings.conf – contains settings dynamically handled by programmatic means. It is also referenced by refind.conf. Any settings here will override refind.conf.

In addition to these files, i also have some alternative files that are programmatically swapped out with auto_settings.conf:

  • auto_default.conf – when the contents of this file is contained within auto_settings.conf this brings about the default state for my system. This shows me a menu at boot time, prompting me to choose between windows and linux. The system waits for my input.
  • auto_mint.conf – when the contents of this file is contained within refind.conf, rEFInd will skip the GUI menu, and proceed directly to booting Linux Mint.
  • auto_win7.conf – when the contents of this file is contained within refind.conf, rEFInd will skip the GUI and boot windows.

That is all (i think) you need to know about rEFInd in order to solve this problem.

Next, the System:

When i boot my system, it asks me which OS to load. Say i choose windows.

It boots windows.

Then, I want to go into Linux.

I can right-click on my desktop, and click "Boot to Linux".

Using the registry, I have created an entry in the desktop's context menu that triggers a custom batch file. This registry entry, when calling the batch file, also passes particular arguments to the batch file that dictates its function at runtime.

So, at this moment, my batch file (lovingly called bootfacilitator.bat) is doing its job quite wonderfully. On the EFI partition, my batch file replaces the contents of auto_default.conf with the contents of auto_mint.conf. If that operation is successful, which it is, it then reboots the system.

Additionally, upon booting windows, bootfacilitator was also called
transparently, that time for the function of replacing the contents of
auto_settings.conf with the contents of auto_default.conf.

The system shuts down, and boots into rEFInd, which skips the GUI, and boots Linux.

I need Linux to replace the contents of auto_settings.conf with the contents of auto_default.conf, or I am stuck in a linux-only boot scenario. Me no likey. All I have to do is replace the contents of a file at boot.

I have a script (bootfacilitator.sh) that is contained in /mnt/data/Backup/Software/Linux.

In linux, bootfacilitator.sh should replace the contents of /mnt/efi/EFI/boot/auto_settings.conf with the contents of /mnt/efi/EFI/boot/auto_default.conf.

Additionally, I have some other goals for this project. I would also like to have two context menu entries on my linux desktop to mirror the entries in the context menu in my windows desktop: "Boot to Windows" and "Reboot Linux".

The end result of this is to have a fluid workflow from one OS to another, with the scalability to incorporate additional OSs.

PS my fstab:

# <file system> <mount point>   <type>  <options>       <dump>  <pass>
/dev/sda1   /mnt/efi    vfat    defaults    0   0
UUID=f9750eee-576b-46b2-a0a1-f6ad34f4d526 /               ext4    errors=remount-ro 0       1
UUID=1f49ddc3-88d1-40e6-b706-7623a4fb47ac none            swap    sw              0       0
/dev/sda4   /mnt/osx    hfsplus defaults    0   0
/dev/sda3   /mnt/win_7  ntfs    defaults    0   0
/dev/sda5   /mnt/data   ntfs    defaults    0   0
/mnt/data/Videos    /home/rich/Videos   none    bind    0   0
/mnt/data/Documents /home/rich/Documents    none    bind    0   0
/mnt/data/Music /home/rich/Music    none    bind    0   0
/mnt/data/Pictures  /home/rich/Pictures none    bind    0   0
/mnt/data/Downloads /home/rich/Downloads    none    bind    0   0
/mnt/data/Public    /home/rich/Public   none    bind    0   0

bootfacilitator.sh:

#!/bin/sh 
#description: Re-Enables EFI multi-select boot menu (in case of forced selection scenario) 
sudo cp /mnt/efi/EFI/Boot/auto_default.conf /mnt/efi/EFI/Boot/auto_settings.conf

Best Answer

A couple ways to accomplish this:

  • Make a .desktop file in ~/.config/autostart/. This will run when you log in. Us something like sudo cp input.txt output.txt as the command.
  • A more robust solution would be to create an Upstart job, which will run when your system boots.
Related Question