Swap Locations

Team-Fly    

Solaris™ Operating Environment Boot Camp
By David Rhodes, Dominic Butler
Table of Contents
Chapter 7.  Swap Space


Swap space can be built up from raw disk partitions and/or swap files. In this section we'll have a look at both of these. We'll start with the most commonly used typedisk partitions.

Raw Disk Partitions

When you first install Solaris, a default swap partition will normally be created for you, so it is unlikely that you would need to create the main swap partition. You are much more likely to need to create an additional swap partition or alter the size of the one that Solaris created for you.

Using raw disk areas for swap size is a very simple procedurewe simply need to define the partition boundaries, just as we would for a normal file system (see Chapter 6, "The Filesystem and Its Contents"). In fact, the only difference between creating a swap partition and one that will be used with a filesystem is that you don't actually create anything on the partition used for swap. All we need to do is inform the system about the new area, after which the kernel will start to use it (if it needs to!).

For example, if we have a look at the following label from a boot disk, the only difference we can see between the partitions is that partition 3 has its flags set to "unmountable" and it doesn't have a mount point, although this does not prove we are looking at a swap partition:

 hydrogen# prtvtoc /dev/rdsk/c0t2d0s2 <lines removed for clarity> * Flags: *   1: unmountable *  10: read-only <lines removed for clarity> *                       First Sector  Last * Partition Tag Flags  Sector Count Sector Mount Directory     0        2   00       0   103005   103004  /     1        7   00  103005   132435   235439  /var     2        5   00       0  3441675  3441674     3        3   01  235440   163500   398939     4        9   00 3229125   206010  3435134     5        0   00  398940    83385   482324  /opt     6        4   00  482325  1639905  2122229  /usr     7        8   00 2122230  1106895  3229124  /export/home hydrogen# 

Now, we have a valid swap space. We'll look at some of the options we can use to access it in a moment. For now, we'll move on and look at the second type of swap locationswap files.

Swap Files

As well as defining a swap partition, we can also create a swap file. This is a file that resides within a standard filesystem for the sole use of the kernel. Swap files have many of the same attributes as a standard file, in that they appear in ls output. Their sizes can be seen with programs such as du and df.

Swap files have a number of advantages over raw partitions. For example, they can be quickly added or removed, and it's very easy to alter their size.

Interestingly, there is no difference in performance between a swap partition and a swap file. This is because the swap file is not accessed through the normal filesystem mechanism. Instead, it is accessed directly by the kernel, in exactly the same way as a swap partition.

Let's work through the creation of a 100 MB swap file nowthe first step is to create the actual file itself. For this, we need to choose a filesystem that has enough space to hold the file. We also need to make sure that it's not one of the most heavily used. We'll use the /export/home filesystem for this, as it meets both of the above criteria; we'll call the file extra_swap:

 hydrogen# mkfile 100m /export/home/extra_swap hydrogen# ls -l /export/home/extra_swap -rw------T   1 root     other    104857600 Nov  4 15:35    /export/home/extra_swap hydrogen# 

When mkfile is run by root, it automatically sets the sticky bit on the newly created file, but without the execute bit; hence the "T" instead of "t." It is still possible for non-root users to use mkfile, but they need to manually set the sticky bit.

This is a specific state that only applies to files being used as swap spacethe reason being that it turns an inode's access time update (atime) into an asynchronous (nonblocking) update, rather than the normal, synchronous update.

The mkfile command creates a file of the size specified and fills it with nulls (a null is represented by the key sequence backslash zero"\0"). The file is created in this way so that the space allocated to it is contiguous on the disk.

It is important that the permissions are set to prevent the file from being accidentally overwritten.

Adding the Swap Devices

Now we've got two swap devices: a raw disk and a swap file. The next job is to let the system know they are available. We can do this in two ways:

  • At boot-time, using /etc/vfstab

  • Dynamically, using the swap command

Statically with /etc/vfstab

The first point to be aware of is that the system isn't really concerned about whether it's using a disk or a file. They can both be mounted at boot-time by defining entries in /etc/vfstab. Similarly, they can both be manipulated using the swap command. We will create the vfstab entry for the raw disk here and add the swap file entry later.

We can see from the following entry that we don't provide any information about the swap size, just the name of the device that should be used. Also, no checking is done for the devices specified. The kernel will simply use the area. This means that if, for example, a filesystem already exists on the partition specified, it would be destroyedprovided it was not already mounted.

Our file will be as follows:

 hydrogen# cat /etc/vfstab #device     device     mount    FS    fsck   mount    mount #to mount   to fsck    point    type  pass   at boot  options # <lines removed for clarity> /dev/dsk/c0t2d0s3 -    -        swap   -     no       - swap              -    /tmp     tmpfs  -     yes      - hydrogen# 

This example shows that the swap partition is located on the disk partition /dev/dsk/c0t2d0s3. You will also notice that the device for /tmp is defined as being the swap partition. This does not mean that /tmp has been created on the swap partition; it is actually created in memory, but it may make use of the swap partition, since most things in memory are fair game for the sched process to swap out (see Chapter 6, "The Filesystem and Its Contents").

So, how does swap get mounted? The simple answer is "It doesn't." As we mentioned earlier, the swap area is not a filesystemit's just a partition on a disk. The data that is stored there is under the control of the kernel rather than the user. It is not the user who requests that a specific part of memory should be copied to swap in order for a different process to make use of it. The swap is only used while the system is actually running, so there should be no need to keep the data between reboots (as there would be with most filesystems).

Therefore, all the system really needs to know is which device is allocated for use as swap, each time it reboots. It does this by running the script /etc/init.d/standardmounts (linked to /etc/rcS.d/S40standardmounts.sh), which in turn calls swapadd. This searches /etc/vfstab for any swap partitions and adds them. (There is no man page for the swapadd script, since a system administrator should never have to run it directly.)

Dynamically with Swap

We said that one advantage of using swap files is that it's a quick and easy method. For this reason, they are often added dynamically (as a temporary measure to get around a problem, for example). Now let's add a file device to our existing swap space:

 hydrogen# swap a /export/home/extra_swap hydrogen# 

The swap file is now available for use. However, if the machine were to be shut down or the /export/home filesystem were unmounted, it would no longer be available. To make the setting permanent, we need to add the following entry to /etc/vfstab:

 /export/home/extra_swap  -  -  swap  -  no  - 

Now we just need to check that the new swap space is available:

 hydrogen swap -l swapfile dev swaplo blocks free /dev/dsk/c0t2d0s3        32,27    8 163288 163288 /export/home/extra_swap  -        8 204792 204792 hydrogen# 

A swap partition can also be removed using the swap command. If any blocks within the swap area are in use, they will be moved into an alternative active swap partition:

 hydrogen# swap d /export/home/extra_swap hydrogen# swap l swapfile             dev  swaplo blocks   free /dev/dsk/c0t2d0s3   32,27      8 163288 163288 hydrogen# 

This command will not actually delete the file, but will de-allocate it from use as swap. Consequently, you will need to manually remove the file and also remove the entry from /etc/vfstab.

 hydrogen# rm /export/home/extra_swap hydrogen# 

Good. Now we know how to manipulate the swap devices as we please.


    Team-Fly    
    Top
     



    Solaris Operating Environment Boot Camp
    Solaris Operating Environment Boot Camp
    ISBN: 0130342874
    EAN: 2147483647
    Year: 2002
    Pages: 301

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net