Is it possible to run a "startup" script when booting into single user mode? I need to be able to boot into single user mode, run my script, then reboot into multiuser mode without any human interaction.

I've done a bunch of research and can't find anything that is clear on the topic, although of course I could replace something else that does run with a wrapper that also launches my script, but I'd prefer to do it in some official way. Ideally, the solution will work in FreeBSD 6 and up, but I'm willing to accept only new versions if it just doesn't exist in the older revs.

EDIT: In response to comments, I'm actually looking to do two things, both of which are probably possible without single user mode, but it is much easier to control all the variables in single user mode if that is at all a possibility:

  1. Grow the filesystem in a vmware installation (I've successfully used these instructions manually).

  2. Zero the free space in all file systems (in this case it is to prepare the VM for export). Again, I don't really need single user mode for this, but it would make things easier.

Of course, these aren't really VM issues, but stuff that I'd like to be able to do in general. The fact that these systems happen to be in VMware is not particularly relevant, and I didn't want to throw people off by confusing them with the fact (some people seem to go a little nutty when virtualization is mentioned, but in most cases it doesn't make a difference).

EDIT2: Regardless of my current reasons, if anyone has any suggestions on how to do this (even hacks if there are no official ways), please post them as an answer.

  • I've looked for the same thing in the past and never found anything very useful. Single user mode always asks for the shell interactively, immediately after booting the kernel. Are you trying to do a remote installworld? – Chris S Jan 18 '11 at 2:49
  • I too am curious about why your script has to run in single user mode. What special environment does it need? Can that be replicated in regular multiuser mode? This is something that is easy in linux and hard in freebsd it appears. – Phil Hollenback Jan 18 '11 at 3:39
  • @Chris and @Phil, no, not trying to do a remote installworld, actually I'd like to do two things, and I'll edit my question with them momentarily. Thanks. – Jed Daniels Jan 19 '11 at 17:35
  • #1, Growing the filesystem doesn't require single mode really, but you're right that it makes the process much easier as the FS needs to be unmounted. Perhaps creating a "service" VM that can connect to the vmdk and grow it from there would work? #2 I think you're pretty much stuck unless you only want to get 'most' of the free space. – Chris S Jan 19 '11 at 17:52

FreeBSD's single user mode doesn't run anything other then what you enter for the 'shell' at the prompt. You could write something that performed what you need in plain old bourne and have it execute that, but it still requires you entering something at the CLI to perform it. You could potentially hack /sbin/init and supply that string yourself to autoexecute in single user mode..

/usr/src/sbin/init/init.c Starting around line 629, the function single_user() is what you want to modify, specifically the value of 'shell' and the section where it asks for it interactively.

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.