06 January 2011

Adding Swap Space in Solaris

Following up on "Adding Swap Space in FreeBSD", we take a look at the same
with Solaris.  As in the FreeBSD writeup, the usage of a free disk slice
or else a swap file is discussed.  The following details our host info:
        HOST:           snorkle
        PROMPT:         snorkle [0]
        OS:             Solaris 10 x86
While detailed is Solaris 10 x86 for our examples, the following should
be usable on prior Solaris versions, both sparc and x86.

SLICE:  If you already have a free slice that isn't being used for
anything else, it can be easily added as swap space:
        snorkle [0] /usr/sbin/swap -a /dev/dsk/c1t3d0s0
Since I don't have any free slice on a configured disk, I need to create
one on an available disk.  A quick check with 'format' gives us our
known disks:
        snorkle [0] echo "q" | /usr/sbin/format
        Searching for disks...done


        AVAILABLE DISK SELECTIONS:
               0. c1t0d0 <DEFAULT cyl 1334 alt 2 hd 255 sec 63>
                  /pci@0,0/pci1000,8000@14/sd@0,0
               1. c1t1d0 <DEFAULT cyl 1334 alt 2 hd 255 sec 63>
                  /pci@0,0/pci1000,8000@14/sd@1,0
               2. c1t2d0 <DEFAULT cyl 1334 alt 2 hd 255 sec 63>
                  /pci@0,0/pci1000,8000@14/sd@2,0
               3. c1t3d0 <DEFAULT cyl 509 alt 2 hd 64 sec 32>
                  /pci@0,0/pci1000,8000@14/sd@3,0
               4. c1t4d0 <DEFAULT cyl 509 alt 2 hd 64 sec 32>
                  /pci@0,0/pci1000,8000@14/sd@4,0
        Specify disk (enter its number): `q' is not an integer.
        Specify disk (enter its number):
Since I know that 'c1t3d0' isn't currently being used for anything, we
can use it to set up a swap slice.  For Solaris x86, we first need to use
'fdisk' to set up an initial Solaris partition.  (For Solaris sparc, skip
'fdisk' and proceed straight to 'format'.)  The following uses 'fdisk'
to create a Solaris partition spanning all available disk space on
'c1t3d0', destroying any previous disk labels:
        snorkle [1] /usr/sbin/fdisk -B -d /dev/rdsk/c1t3d0p0
        Physical Geometry:
          cylinders[511] heads[64] sectors[32]
          sector size[512] blocks[1046528] mbytes[511]
        Virtual (HBA) Geometry:
          cylinders[0] heads[0] sectors[0]
          sector size[512] blocks[0] mbytes[0]
        About to write fdisk table:
          SYSID ACT BHEAD BSECT BEGCYL   EHEAD ESECT ENDCYL   RELSECT   NUMSECT
          191   128 254   63    1023     254   63    1023     2048       1044480
          100   0   0     0     0        0     0     0        100        100
          100   0   0     0     0        0     0     0        100        100
          100   0   0     0     0        0     0     0        100        100
        Clearing VTOC labels from NEW table
                Clearing primary VTOC at byte 1049088 (block 2049)
                Clearing backup VTOC at byte 535806464 (block 1046497)
                Clearing backup VTOC at byte 535807488 (block 1046499)
                Clearing backup VTOC at byte 535808512 (block 1046501)
                Clearing backup VTOC at byte 535809536 (block 1046503)
                Clearing backup VTOC at byte 535810560 (block 1046505)
A second usage of 'fdisk' simply verifies the partition was created:
        snorkle [0] /usr/sbin/fdisk -W - /dev/rdsk/c1t3d0p0

        * /dev/rdsk/c1t3d0p0 default fdisk table
        * Dimensions:
        *    512 bytes/sector
        *     32 sectors/track
        *     64 tracks/cylinder
        *    511 cylinders
        *
        * systid:
        *    1: DOSOS12
        *    2: PCIXOS
        *    4: DOSOS16
        *    5: EXTDOS
        *    6: DOSBIG
        *    7: FDISK_IFS
        *    8: FDISK_AIXBOOT
        *    9: FDISK_AIXDATA
        *   10: FDISK_0S2BOOT
        *   11: FDISK_WINDOWS
        *   12: FDISK_EXT_WIN
        *   14: FDISK_FAT95
        *   15: FDISK_EXTLBA
        *   18: DIAGPART
        *   65: FDISK_LINUX
        *   82: FDISK_CPM
        *   86: DOSDATA
        *   98: OTHEROS
        *   99: UNIXOS
        *  101: FDISK_NOVELL3
        *  119: FDISK_QNX4
        *  120: FDISK_QNX42
        *  121: FDISK_QNX43
        *  130: SUNIXOS
        *  131: FDISK_LINUXNAT
        *  134: FDISK_NTFSVOL1
        *  135: FDISK_NTFSVOL2
        *  165: FDISK_BSD
        *  167: FDISK_NEXTSTEP
        *  183: FDISK_BSDIFS
        *  184: FDISK_BSDISWAP
        *  190: X86BOOT
        *  191: SUNIXOS2
        *  238: EFI_PMBR
        *  239: EFI_FS
        *

        * Id    Act  Bhead  Bsect  Bcyl    Ehead  Esect  Ecyl    Rsect      Numsect
          191   128  254    63     1023    254    63     1023    2048       1044480
A quick check of the disk slices on 'c1t3d0' via 'prtvtoc' allows us to
see how much space we can allocate for swap.  (For x86, an SMI layout
includes slice 8, used for the boot block, VTOC, disk label, etc, and thus
is required.  Sparc hosts won't have slice 8 when using an SMI layout.)
Since our host is of the x86 variety, we need to account for the space
and location used by 'slice 8' before creating our swap slice.  While we
could interactively use 'format' for slice creation, we'll automate it
instead.  To do so, account for our new slice size by subtracting the
"Sector Count" of 'slice 8' from that of 'slice 2', this will be our
block count.  After this, create an input file for 'format' to use:
        snorkle [0] /usr/sbin/prtvtoc /dev/rdsk/c1t3d0s2
        <snip...>
        *                          First     Sector    Last
        * Partition  Tag  Flags    Sector     Count    Sector  Mount Directory
               2      5    01          0   1040384   1040383
               8      1    01          0      2048      2047
        snorkle [0] /usr/bin/expr 1040384 - 2048
        1038336
        snorkle [0] /usr/bin/cat /tmp/c1t3d0.input
        partition
        0    
        ""
        ""
        1    
        1038336b
        label
        quit 
        quit 
The breakdown of the input file is:
        partition               # command to work on partitions (disk slices)
        0                       # partition (disk slice) number to work on
        ""                      # tag [unassigned]
        ""                      # permission flags [wm] (wm is default)
        1                       # starting cylinder (accounting for 'slice 8' on x86)
        1038336b                # size in blocks (1040384 - 2048)
        label                   # command to create the disk label / VTOC
        quit                    # exit work on partitions
        quit                    # exit format command
The following 'format' command suppresses STDOUT (-s), sets the command
file to use (-f), creates a log file for STDERR (-l), and operates on
'c1t3d0'.  The result of slice creation is verified via 'prtvtoc':
        snorkle [0] /usr/sbin/format -s -f /tmp/c1t3d0.input -l /tmp/c1t3d0.out c1t3d0
        snorkle [0] /usr/sbin/prtvtoc -h /dev/rdsk/c1t3d0s2
               0      0    00       2048   1038336   1040383
               2      5    01          0   1040384   1040383
               8      1    01          0      2048      2047
With the setup work out of the way, we can add the new swap space and
verify that it was added via 'swap':
        snorkle [0] /usr/sbin/swap -a /dev/dsk/c1t3d0s0

    before adding:

        snorkle [0] /usr/sbin/swap -l
        swapfile             dev  swaplo blocks   free
        /dev/dsk/c1t0d0s1   32,1       8 2313352 2313352 

    after adding:

        snorkle [0] /usr/sbin/swap -l
        swapfile             dev  swaplo blocks   free
        /dev/dsk/c1t0d0s1   32,1       8 2313352 2313352 
        /dev/dsk/c1t3d0s0   32,256      8 1038328 1038328
To retain the new swap space through a system reboot, add 'c1t3d0s0'
to /etc/vfstab as seen below:
        snorkle [0] /usr/bin/grep swap /etc/vfstab
        /dev/dsk/c1t0d0s1       -       -       swap    -       no      -
        /dev/dsk/c1t3d0s0       -       -       swap    -       no      -
        swap    -       /tmp    tmpfs   -       yes     -
SWAPFILE:  For the second option, locate an out of the way place on an
existing filesystem with adequate space for the swap file to reside.
For our purposes, we'll use '/opt/swapfile' for a new 512 MB swap file.
Start by verifying that the directory structure is available and that
the swap file doesn't already exist:
        snorkle [0] [ ! -d /opt ] && /usr/bin/mkdir /opt
        snorkle [0] /usr/bin/ls /opt/swapfile
        /opt/swapfile: No such file or directory
There are two options available to create the new swap file.  The
first, 'mkfile', is the standard in Solaris.  The second is by way of
'dd'.  The following sets up 'swapfile' via 'mkfile', and verifies its
creation and attributes:
        snorkle [2] /usr/sbin/mkfile -v 512m /opt/swapfile
        /opt/swapfile 536870912 bytes
        snorkle [0] /usr/bin/du -sh /opt/swapfile
         512M   /opt/swapfile
        snorkle [0] /usr/bin/ls -ld /opt/swapfile
        -rw------T   1 root     root     536870912 Jan  6 12:51 /opt/swapfile
To do the same with the 'dd' method, after using 'dd' we need to set
the appropriate ownership and permissions:
        snorkle [0] /usr/bin/dd if=/dev/zero of=/opt/swapfile bs=1k count=512k
        524288+0 records in
        524288+0 records out
        snorkle [0] /usr/bin/chown root:root /opt/swapfile
        snorkle [0] /usr/bin/chmod 1600 /opt/swapfile
        snorkle [0] /usr/bin/du -sh /opt/swapfile
         512M   /opt/swapfile
        snorkle [0] /usr/bin/ls -ld /opt/swapfile
        -rw------T   1 root     root     536870912 Jan  6 12:59 /opt/swapfile
Now simply add the swap file via 'swap' and verify that it was added:
        snorkle [0] /usr/sbin/swap -a /opt/swapfile
        snorkle [0] /usr/sbin/swap -l
        swapfile             dev  swaplo blocks   free
        /dev/dsk/c1t0d0s1   32,1       8 2313352 2313352
        /dev/dsk/c1t3d0s0   32,256      8 1038328 1038328
        /opt/swapfile         -        8 1048568 1048568
To retain the freshly added swap space through a system reboot, add
'/opt/swapfile' to /etc/vfstab as seen below:
        snorkle [0] /usr/bin/grep swap /etc/vfstab
        /dev/dsk/c1t0d0s1       -       -       swap    -       no      -
        /dev/dsk/c1t3d0s0       -       -       swap    -       no      -
        swap    -       /tmp    tmpfs   -       yes     -
        /opt/swapfile           -       -       swap    -       no      -
Should you need to remove swap space provided by a disk slice or swap
file, 'swap' is again used:
        snorkle [0] /usr/sbin/swap -d /dev/dsk/c1t3d0s0
        snorkle [0] /usr/sbin/swap -d /opt/swapfile
        snorkle [0] /usr/sbin/swap -l
        swapfile             dev  swaplo blocks   free
        /dev/dsk/c1t0d0s1   32,1       8 2313352 2313352
Of note, only swap space that isn't in use can be removed.


see also:
    Adding Swap Space in FreeBSD
    Adding Swap Space in Linux

No comments: