Less Known Solaris features: lockfs
Okay, quite often the configuration of a feature or application mandates that the data on the disk doesn´t change while you activate it. Okay, an easy way would be simply unmounting the disk. Okay, that´s possible but then you can´t access the data on the disk at all. You can´t even read from the filesystem, albeit this doesn´t change anything on the disk (okay, as long you´ve mounted the disk with noatime).
So: How do you ensure, that the content of a filesystem doesn´t change while you work with the disk. ufs has a an interesting feature. It´s called lockfs
you can lock the filesystem. You can lock it to an extend that you can only unmount and remount it to gather access to the data, but you can lock only some ways to access it.
Types of Locks
The lockfs
command can establish a number of locks on an UFS filesystem:
- Delete Lock (
-d
):suspends access that could remove directory entries - Hard Lock (
-h
):suspends all access to the filesystem. It can´t be unlocked. You have to unmount and remount it - Name Lock (
-n
):suspends all access to the filesystem, that could remove or change directory entries - Error Lock (
-n
):This lock is normally established, when UFS detects an internal inconsistency. It´s released by the usage offsck
. It suspends all access to the filesystem. - write lock (
-w
):This lock suspends all accesses that could modify the filesystem</b> - flush log (
-f
): This isn´t really a lock. This option forces a synchronous flush of the named filesystem. It returns wenn all data has been written to disk and the log has been rolled
</ul>
Write Lock
Okay, let´s assume we´ve mounted an UFS filesystem at/mnt
.No problem. Our testfile found it´s way into file system. Okay, now we establish a write lock on our file system.
You set the locks with the
lockfs
command, the switch -w
tells command to set an write lock. With a write lock, you can read a filesystem, but you can´t write to it. Okay, let´s check the existing locks. You use the lockfs
command without any further options.When we try to add an addtional file, the write system call simply blocks. We have to break the echo command with CTRL-C. Okay, now let´s release the lock.
The
-u
commands lockfs to release the lock. When you list the existing locks, the lock on /mnt
is gone.And just to check it, we try to write "test" to
testfile2
again.Not problem. The command returns an an instance. When you check the filesystem, you will see both files.
Delete lock
A rather strange kind of lock is the delete lock. It blocks all delete operations. It´s of less practical use, as you would think at first. You can´t delete a file, but you can zero it or fill it with other content. Okay, let´s use the testfile from our last example. At first we try to delete the first file:No problem. Now we establish the delete lock. This time we add a comment. You can use this command to tell other admins why you have established the lock. When you check for existing locks, you will see the delete lock on
/mnt and the comment:
When you try to delete the file, the rm
just blocks and you have to break it with CTRL-C again:
When you´ve delete-locked an filesystem, you can create new files, you can append data to ist, you can overwrite it:
There is only one thing you can´t do with this new file: You simply can´t delete it.
Now we release the lock
Now you can clean up your testdirectory /mnt
again.
Conclusion
The lockfs
is a really neat feature to deny certain accesses to your filesystem without unmounting it completly. Some locks are more useful for general use than other other. For example the write lock is really useful, when you want to freeze the content of the filesystem while working with tools like AVS. Delete lock or name lock are more of use, when you need a stable directory. The usecase is more an internal one for other tools.
Do you want to learn more?
Documentation
docs.sun.com: man page lockfs