7

I'm assembling some DR stuff for my Linux boxes. One item that would be really handy would be a GRUB option that would run a script to change my boxes from static IPs to dynamic.

I know we could do a single user mode, run a script, reboot, etc but it would greatly simply things for the other admins (mostly with a Windows background) to select one item and have it complete the process automatically.

I would swear that I've seen configuration settings provided to let Grub do this at one point or another but it's been a while. Not sure what to google for.

EDIT:

I'm aware of the rc.local method, however I don't want this to run on every boot. I want there to be an option listed (line item in grub.conf, probably) which allows for a specific script to be run only when that boot option is selected.

2 Answers 2

7

Grub itself can't do this. Its filesystem drivers only support reading.

You can make an entry that loads a Linux kernel (and initrd or initramfs as appropriate) and runs a specific command by passing init=/path/to/script on the kernel command line. That script would do all the requisite mounting, user prompting, file modifications, and finally reboot.

You can also make separate entries that pass a custom argument on the kernel command line. Unknown arguments are ignored, the assumption being that it may be used by some driver which is perhaps loaded as a module. So make Grub entries that pass a kernel argument local.network_interfaces=…, and in /etc/rc.local, look up the value of that argument (</proc/cmdline tr ' ' '\n' | sed -n 's/^local.network_interfaces=//p').

2
  • 1
    Perfect. Is there any way to avoid a kernel panic when the script exits using the init method? Commented Feb 19, 2013 at 21:15
  • 1
    @TimBrigham Yes: don't exit. Either exec (exec /sbin/init, probably) or reboot. Commented Feb 19, 2013 at 23:38
1

Let the kernel setup network

set netcfg='ip=none'
menuentry 'Switch DHCP on/off' {
   if [ $netcfg = 'ip=dhcp' ]; then
      set netcfg='ip=off'
   else
      set netcfg='ip=dhcp'
   fi
   echo Setting $netcfg
   sleep 1
}
menuentry 'Linux' {
   linux vmlinuz $netcfg
}
1
  • Where would one place this? /etc/grub.d/40_custom?? Is anything needed to determine/select which filesystem linux vmlivuz will be found on? (I'm fairly grub-naive, so please pardon me if this seems obvious... happy to be pointed at documentation somewhere, too.) Commented Jan 31, 2023 at 15:43

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.