Saturday, November 10, 2007

Editing Startup Scripts on Solaris - Part 2 - The Rub

As we mentioned yesterday, it's generally best practice to edit, or update, your init scripts by editing the source file in /etc/init.d, rather than the link to it in one of the "run level" directories, like /etc/rc2.d

Given a specific example (For instance: we need to comment out 8 or 9 scripts in /etc/init.d) that we want to do fast, we've decided that just adding "#" symbols to the beginning of every line of all 9 scripts in /etc/init.d would be the fastest way.

We have a few options, like using Sed, Awk or command-line Perl, to take care of the work for us lickety-split. However these solutions, and others like them, present a general problem. There's nothing wrong with using these methods, in general, except that Solaris, right out-of-the-box, uses a lot of "hard links" in its "run level" directories to point back to the source script in /etc/init.d.

Hard links differ from the symbolic links we discussed yesterday in that they are not just "pointers" to the source file; they are actually links to the inode that the source file uses. In essence, they are another version of the exact same file, or, in different terms, another reference to the same file.

To illustrate the difference another way: If you delete a source file, the symbolic link to it will still exist, but if you try to edit that symlink, you'll be editing an empty file, since it's just a pointer to something that doesn't exist anymore. If you delete the source file and go to edit the hard link, you will still be able to edit the original file since the hard link points to the inode (not the filename) and removing the source file has just decreased the reference count of the inode.

When a hard link to a file is created, in essence, two copies of the file now exist. This presents an issue if you use Sed, Awk or command line Perl, because when you edit the source file in /etc/init.d using these methods, you're copying the changes to another file and then writing that back over the original: Changing the inode number! Now your source file and your file in the "run level" directory both point to different inodes, and updating the source file won't change the contents of the "run level" file.

There is an easy way around this. You can edit each of the 9 files using an editor like vi. But how to do it as quickly and easily as with Sed, Awk or command line Perl?

Tomorrow, we'll create a script that will do just that. It will edit your files for you, hands-off, and not change the inode number; thus ensuring that all of your changes will remain consistent in the "run level" directories, no matter what kind of links (soft or hard) they harbour.

Have a good Saturday Night :)

, Mike