The Root (/) File System
Describe the purpose, features, and functions of root subdirectories, file components, file types, and hard links in the Solaris directory hierarchy.
Solaris comes with many file systems already created. These file systems were described earlier in this chapter. One of the most important file systems is the root file system. This file system is important because it is the first file system that is mounted when the system begins to boot. It is the file system that contains the kernel and all of the bootup scripts and programs. Without this file system, the system will not boot.
Furthermore, the root file system is at the top of the hierarchy of all file systems and contains all of the directory mount points for the other file systems. Taking a closer look at the contents of the root file system, we see that it contains many important directories, which are described in Table 1.7.
In addition, Solaris 10 introduces additional in-memory system directories and subdirectories that are described in Table 1.8. These in-memory directories are maintained by the kernel and system services. With the exception of /tmp, do not attempt to manually create, alter, or remove files from these directories.
As you browse the directories in the root file system, you'll notice many file types. The file type can usually be identified by looking at the first character of the first column of information displayed when issuing the ls -l command.
ls -l /etc total 583 lrwxrwxrwx 1 root root 14 Aug 9 19:18 TIMEZONE -> ./default/init drwxr-xr-x 6 root other 512 Aug 9 20:51 X11 drwxr-xr-x 2 adm adm 512 Aug 10 09:24 acct lrwxrwxrwx 1 root root 14 Aug 9 19:31 aliases -> ./mail/aliases drwxr-xr-x 7 root bin 512 Aug 10 09:34 apache drwxr-xr-x 2 root bin 512 Aug 10 09:26 apache2 drwxr-xr-x 2 root other 512 Aug 9 20:29 apoc -rw-r--r-- 1 root bin 226 Sep 13 14:17 auto_home -rw-r--r-- 1 root bin 248 Aug 9 19:29 auto_master lrwxrwxrwx 1 root root 16 Aug 9 19:18 autopush -> ../sbin/autopush drwxr-xr-x 2 root other 512 Aug 9 20:19 bonobo-activation drwxr-xr-x 2 root sys 512 Aug 9 19:18 certs lrwxrwxrwx 1 root root 18 Aug 9 19:18 cfgadm -> ../usr/sbin/cfgadm lrwxrwxrwx 1 root root 18 Aug 9 20:18 chroot -> ../usr/sbin/chroot <output has been truncated>
The information displayed in the long listing is in the form of columns and is as follows (reading from left to right):
Soft (Symbolic) Links
Sometimes symbolic links are used for shortcuts; other times we use them to link to a filename from each user's home directory to a centralized location or file. For example, perhaps we want to have a common directory named documents where every user stores their documents. This directory exists as /export/data/documents. In each user's directory, we create a link named documents that points to /export/data/documents. As a user, whenever I store something in the directory named $HOME/documents, the file actually gets directed to /export/data/documents. We can identify links when we perform a long listing on a directory as follows:
Notice the use of the -i option used with the ls command and the results displayed. This option is used to display the inode number (in the left column of the output) that has been assigned to each file and directory. I'll describe why this inode number is relevant to links later in this section when I describe hard links.
ls -li $HOME
The system displays the following:
75264 drwxr--r-- 2 bcalkins staff 512 Jun 6 20:36 dir1 78848 drwxrwxr-x 2 bcalkins staff 512 Jun 6 20:38 dir2 82432 drw-r--r-- 2 bcalkins staff 512 Jun 6 20:39 dir3 3593 lrwxrwxrwx 1 bcalkins staff 22 Jun 17 17:09 documents\ -> /export/data/documents
Output has been truncated.
Notice the file that has an l as the first character of column 2. This is a soft or symbolic link. The sixth field shows a file size of 22 bytes and the last field shows which file or directory this link is pointing to. Each file has a unique inode number identified in column 1; the importance of this column is discussed later in this chapter.
When storing a file in $HOME/documents, the system is redirecting it to be stored in /export/data/documents. Now when changing to the /export/data/documents directory and issuing the ls -li command:
cd /export/data/documents ls -li
The system displays the following:
125461 drwxr-xr-x 2 root other 512 Jun 17 17:09 documents
Notice that the file that has a d as the first character of column 2. This is the directory that the $HOME/documents link points to. The first column shows the inode number, and the sixth column shows the file size as 512 bytes.
Symbolic links can point to files anywhere on the network. The file or directory could exist in another file system, on another disk, or on another system on the network.
The syntax for creating a symbolic link is as follows:
ln -s source-file link-name
For example, you might have a file named file1 that has the following contents:
This is the contents of file1
To create a symbolic link named link1, which will be linked to the existing file named file1, you issue the following command:
ln -s file1 link1
Now when you list the contents of the directory you see two files:
3588 -rw-r--r-- 1 bcalkins staff 30 Jun 17 17:51 file1 3594 lrwxrwxrwx 1 bcalkins staff 5 Jun 17 18:09 link1 -> file1
See the link named link1 pointing to file1? If you display the contents of link1, it shows the following:
This is the contents of file1
If you remove file1, the source file, link1 will still exist, but it points to a file that does not exist. Type the following:
The cat command can't print out the contents of the file, so you get this message:
cat: Cannot open link1
Explain how to create and remove hard links in a Solaris directory.
the system displays the following:
total 102 <output has been truncated> 2123 -rwxr--r-- 5 root sys 1718 Jan 21 2005 S47pppd 2102 -rwxr--r-- 2 root sys 327 Jan 21 2005 S70uucp 1368 -rwxr-xr-x 2 root other 1558 Jan 9 2005 S72autoinstall 241 -rwxr--r-- 2 root sys 1262 Jan 21 2005 S73cachefs.daemon 1315 -rwxr--r-- 2 root sys 1028 Jan 21 2005 S81dodatadm.udaplt 237 -rwxr--r-- 2 root sys 256 Jan 21 2005 S89PRESERVE 2103 lrwxrwxrwx 1 root root 31 Aug 10 09:49 S89bdconfig ->\ ../init.d/buttons_n_dials-setup 1898 -rwxr--r-- 5 root sys 3506 Jan 10 2005 S90wbem 2100 -rwxr--r-- 5 root sys 1250 Jan 10 2005 S90webconsole <output has been truncated>
The first character in the second column of information displays the file type as a regular file (-). The third column, link count, shows a number greater than 1. It displays the number of links used by this inode number. These are hard links, but they are not identified as links and the display does not show which file they are linked to.
Think of a hard link as a file that has many names. In other words, they all share the same inode number. As described in the section titled "The inode" later in this chapter, a file system identifies a file not by its name, but by its inode number. Looking at the file named S90wbem in the previous listing, we see an inode number of 1898. List all file names in this file system that share this inode number as follows:
find / -mount -inum 1898 -ls
The system displays the following list of files:
1898 4 -rwxr--r-- 5 root sys 3506 Jan 10 2005 /etc/init.d/init.wbem 1898 4 -rwxr--r-- 5 root sys 3506 Jan 10 2005 /etc/rc0.d/K36wbem 1898 4 -rwxr--r-- 5 root sys 3506 Jan 10 2005 /etc/rc1.d/K36wbem 1898 4 -rwxr--r-- 5 root sys 3506 Jan 10 2005 /etc/rc2.d/S90wbem 1898 4 -rwxr--r-- 5 root sys 3506 Jan 10 2005 /etc/rcS.d/K36wbem
In this example, the file is named file1 in $HOME:
3588 -rw-r--r-- 3 bcalkins staff 30 Jun 17 17:51 file1
The contents of this file are displayed with the cat command:
This is the contents of file1
The syntax for creating a hard link is as follows:
ln source-file link-name
To create a hard link named link1, which will be linked to the existing file named file1, issue the following command:
ln file1 link1
Now when I list the contents of the directory, I see two files:
3588 -rw-r--r-- 2 bcalkins staff 30 Jun 17 17:51 file1 3588 -rw-r--r-- 2 bcalkins staff 30 Jun 17 17:51 link1
Both files share the same inode number, the number of links is two, and the file size is 30 bytes. If I display the contents of link1, it shows the following:
This is the content of file1
If I remove file1, the source file, link1 still exists and still contains the data. The data will not be deleted until I destroy the last file that shares this inode number.
A hard link cannot span file systems; it can only point to another file located within its file system. The reason is that hard links all share an inode number. Each file system has its own set of inode numbers; therefore, a file with inode number 3588 in the /export/home file system may not even exist in the /var file system.
An advantage of a symbolic link over a hard link is that you can create a symbolic link to a file that does not yet exist. You cannot create a hard link unless the source file already exists. Here's what happens when you create a symbolic link to a file that does not exist:
ln -s nonexistentfile link5
When you list the file:
ls -l link5
lrwxrwxrwx 1 bcalkins staff 14 Jun 17 18:24 link5 -> nonexistentfile
Now, here's what happens when you create a hard link to a file that does not exist:
ln noexistentfile link6
The system responds with
ln: cannot access noexistentfile
Removing a Link
For example, to remove the link named link1, type
Removing Files and Links When you remove a file, it's always a good idea to remove the symbolic links that pointed to that file, unless you plan to use them again if the file gets re-created.