Linux Swap Space Creation and Monitoring

Overview

This Post is intended to understand the swap creation, monitoring and extending in Redhat Linux.

Swap space is a restricted amount of physical memory that is allocated for use by the operating system when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a slower access time than physical memory. Read more

Recommended System Swap Space
In years past, the recommended amount of swap space increased linearly with the amount of RAM in the system. But because the amount of memory in modern systems has increased into the hundreds of gigabytes, it is now recognized that the amount of swap space that a system needs is a function of the memory workload running on that system. However, given that swap space is usually designated at install time, and that it can be difficult to determine beforehand the memory workload of a system, Redhat recommend determining system swap using the following table.

Amount of RAM in the System Recommended Amount of Swap Space
4GB of RAM or less a minimum of 2GB of swap space
4GB to 16GB of RAM a minimum of 4GB of swap space
16GB to 64GB of RAM a minimum of 8GB of swap space
64GB to 256GB of RAM a minimum of 16GB of swap space
256GB to 512GB of RAM a minimum of 32GB of swap space

Note : On most distributions of Linux, it is recommended that you set swap space while installing the operating system

 

How to Monitor Swap Space

We shall look at different commands and tools that can help you to monitor your swap space usage in your Linux systems as follows

Using the swapon Command

To view all devices marked as swap in the /etc/fstab file you can use the –all option. Though devices that are already working as swap space are skipped

If you want to view a summary of swap space usage by device, use the – summary (swapon –s) option.

[[email protected] ~]# swapon –summary
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       2097148 0       -1
[[email protected] ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       2097148 0       -1
Note :- Use –help option to view more options and information.
Using /proc/swaps

The /proc filesystem is a process information pseudo-file system. It actually does not contain ‘real’ files but runtime system information, for example system memory, devices mounted, hardware configuration and many more.

[[email protected] ~]# cat /proc/swaps

Filename                                Type            Size    Used    Priority

/dev/dm-1                               partition       2097148 0       -1

[[email protected] ~]#

Using ‘free’ Command
The free command is used to display the amount of free and used system memory. Using the free command with -h option, which displays output in a human readable format.
[[email protected] ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           7.6G        674M        6.5G        9.8M        507M        6.7G
Swap:          2.0G          0B        2.0G
 Using top Command
To check swap space usage with the help of ‘top’ command
Using the vmstat Command
This command is used to display information about virtual memory statistics
[[email protected] ~]# vmstat
procs ———–memory———- —swap– —–io—- -system– ——cpu—–
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 6791708   2784 516484    0    0     7     0   24   23  0  0 100  0  0
ADDING SWAP SPACE
Sometimes it is necessary to add more swap space after installation
You have three options: create a new swap partition, create a new swap file, or extend swap on an existing LVM2 logical volume. It is recommended that you extend an existing logical volume
Extending Swap on an LVM2 Logical Volume
To extend an LVM2 swap logical volume(suppose /dev/mapper/centos-swap is our swap volume)
1. Disable swapping for the associated logical volume:
[[email protected] ~]# swapoff -v /dev/mapper/centos-swap
swapoff /dev/mapper/centos-swap
[[email protected] ~]# swapon -s
2. Resize the LVM2 logical volume by 256 MB
 [[email protected] ~]# lvresize /dev/mapper/centos-swap -L +256M
  Size of logical volume centos/swap changed from 2.00 GiB (512 extents) to 2.25 GiB (576 extents).
  Logical volume centos/swap successfully resized.
3. Format the new swap space
[[email protected] ~]# mkswap /dev/centos/swap
mkswap: /dev/centos/swap: warning: wiping old swap signature.
Setting up swapspace version 1, size = 2359292 KiB
no label, UUID=5e487401-9ae0-4e1d-adff-2346edfc6244
4. Enable the extended logical volume
[[email protected] ~]# swapon -va
swapon /dev/mapper/centos-swap
swapon: /dev/mapper/centos-swap: found swap signature: version 1, page-size 4, same byte order
swapon: /dev/mapper/centos-swap: pagesize=4096, swapsize=2415919104, devsize=2415919104
5. Test that the logical volume has been extended properly
[[email protected] ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           7.6G        677M        6.5G        9.8M        507M        6.7G
Swap:          2.2G          0B        2.2G
[[email protected] ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       2359292 0       -1
Creating an LVM2 Logical Volume for Swap
To add a swap volume group (suppose /dev/centos/swap2 is the new volume)
1. Create the LVM2 logical volume of size 256 MB
[[email protected] ~]# lvcreate centos -n swap2 -L 256M
  Logical volume “swap2” created.
2. Format the new swap space
[[email protected] ~]# mkswap /dev/centos/swap2
Setting up swapspace version 1, size = 262140 KiB
no label, UUID=6ea40455-47a0-46bf-844e-ec0ebd4a4e6a
3. Add the following entry to the /etc/fstab file
/dev/mapper/centos-swap2 swap                    swap    defaults        0 0
4. Enable the extended logical volume
[[email protected] ~]# swapon –va
swapon /dev/mapper/centos-swap2
swapon: /dev/mapper/centos-swap2: found swap signature: version 1, page-size 4, same byte order
swapon: /dev/mapper/centos-swap2: pagesize=4096, swapsize=268435456, devsize=268435456
5. Verify the swap space
[[email protected] ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       2097148 0       -1
/dev/dm-3                               partition       262140  0       -2
Creating a Swap File
To Add a swap file
1. Determine the size of the new swap file in megabytes and multiply by 1024 to determine the number of blocks. For example, the block size of a 64 MB swap file is 65536.
2. At a shell prompt as root, type the following command with count being equal to the desired block size:
[[email protected] ~]# dd if=/dev/zero of=/swapfile bs=1024 count=65536
65536+0 records in
65536+0 records out
67108864 bytes (67 MB) copied, 0.0893063 s, 751 MB/s
[[email protected] ~]# ls -ld /swapfile
-rw-r–r–. 1 root root 67108864 May 17 16:38 /swapfile
[[email protected] ~]# du -sh /swapfile
64M     /swapfile
3. Change the permissions of the newly created file
[[email protected] ~]# chmod 0600 /swapfile
4. Setup the swap file with the command
[[email protected] ~]# mkswap /swapfile
Setting up swapspace version 1, size = 65532 KiB
no label, UUID=8a404550-e8a3-4f2b-9daf-137fc34f7b6d
5. Edit /etc/fstab and enable the newly added swap space
/swapfile          swap            swap    defaults        0 0
[[email protected] ~]# swapon -va
swapon /swapfile
swapon: /swapfile: found swap signature: version 1, page-size 4, same byte order
swapon: /swapfile: pagesize=4096, swapsize=67108864, devsize=67108864
6. Verify the swap space created.
[[email protected] ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       2097148 0       -1
/dev/dm-3                               partition       262140  0       -2
/swapfile                               file    65532   0       -3
Hope this has helped you ..
Thanks!!!!

Mirror disk replacement in solaris

Let’s here discuss how to replace the failed root mirror disk in solaris under SVM.

==========================================================

1.Identify the faluty disk and its partition

[email protected] /root>metastat -c
d65 m 29GB d63 d61 (maint)
d63 s 29GB c1t1d0s6
d61 s 29GB c1t0d0s6 (maint) <—— Disk showing in maintanace state and it has to be replaced

d30 m 9.8GB d31 d32 (maint)
d31 s 9.8GB c1t0d0s3 (maint) <——
d32 s 9.8GB c1t1d0s3

d55 m 5.9GB d53 d51 (maint)
d53 s 5.9GB c1t1d0s5
d51 s 5.9GB c1t0d0s5 (maint) <——

Read more

d45 m 7.8GB d43 d41 (maint)
d43 s 7.8GB c1t1d0s4
d41 s 7.8GB c1t0d0s4 (maint) <——
d5 m 7.8GB d3 d1 (maint)
d3 s 7.8GB c1t1d0s0
d1 s 7.8GB c1t0d0s0 (maint) <——

d10 m 7.8GB d11 d12 (maint)
d11 s 7.8GB c1t0d0s1 (maint) <——
d12 s 7.8GB c1t1d0s1

2. So we have identified the disk c1t0d0 as faulty which is showing need maintanace in the metastat output.

3. Confirm the disk is having errors in the iostat output and /var/adm/messages also.

[email protected] /root>iostat -En
c1t0d0 Soft Errors: 173 Hard Errors: 0 Transport Errors: 0
Vendor: HITACHI Product: H101473SCSUN72G Revision: SA23 Serial No: 0810DTE8YA
Size: 73.41GB <73407865856 bytes>

4.identify the boot path

prtconf -vp|grep bootpath

bootpath: ‘/[email protected],600000/[email protected]/[email protected]/[email protected]/[email protected]/[email protected]/[email protected],0:a’

echo|format

c1t1d0 <SUN72G cyl 14087 alt 2 hd 24 sec 424>
/[email protected],600000/[email protected]/[email protected]/[email protected]/[email protected]/[email protected]/[email protected],0

So the primary boot disk is c1t1d0. And the faulty disk is its mirror.

Steps to do unconfigure the faulty disk before proceeding with the replacement. (its an online activity)

=========================================================================

1. Take all neccessory pre outputs as below.

df -h,metastat -c,metadb,echo | format,cat /etc/vfstab,swap -l

2. Detach the faluty submirros

metadetach d65 d61

metadetach d30 d31

metadetach d55 d51

metadetach d45 d41

metadetach d5 d1

metadetach d10 d11

3. Delete the metadb information from the disk.

# metadb -d /dev/dsk/c1t0d0s7

4.  Use the cfgadm command to display all the disks in the server

cfgadm -al

[email protected] /root>cfgadm -al
Ap_Id Type Receptacle Occupant Condition
c0 scsi-bus connected configured unknown
c0::dsk/c0t0d0 CD-ROM connected configured unknown
c1 scsi-bus connected configured unknown

c1::dsk/c1t0d0 disk connected configured unknown <——- Failed Disk

c1::dsk/c1t1d0 disk connected configured unknown
c1::dsk/c1t2d0 disk connected configured unknown
c2 fc-fabric connected configured unknown
c2::500009720822514c disk connected configured unknown
c3 fc-fabric connected configured unknown
c3::5000097208225168 disk connected configured unknown

On identifying the disk to be removed, unconfigure the disk. You may have to use -f along with -c to forcibly remove the disk in some cases.

cfgadm -c unconfigure c1::dsk/c1t0d0

 

5). Verify the status of the disk in cfgadm -al command. It should show unconfigured and unavailable.

# cfgadm -al

c1::dsk/c1t0d0 connected unconfigured unknown

You can safely remove the disk from the server now.

6.  Request FE to insert the new disk into the disk slot of the server and run the below command.

# devfsadm

You should see the new disk detected in the OS:

 

Steps to configure the newly added disk

==============================

1. cfgadm -c configure c1::dsk/c1t0d0

2. verify the disk is configured

c1::dsk/c1t0d0 available connected configured unknown

2. copy the prtvtoc from the primary disk.

prtvtoc /dev/rdsk/c1t1d0s2 | fmthard -s – /dev/rdsk/c1t0d0s2

3. now add the metadb in the disk.

metadb -afc3 /dev/dsk/c1t0d0s7

4.Install the bootblk on slice 0 of the new disk.

installboot /usr/platform/`uname -i`/lib/fs/ufs/bootblk /dev/rdsk/c1t0d0s0

5.Update the device ID in the SVM database

metadvadm -u c1t0d0

6. Attach the detach sub mirrors using the metaattach command. The syntax to do so is :

metattach d65 d61

metattach d30 d31

metattach d55 d51

metattach d45 d41

metattach d5 d1

metattach d10 d11

7. You can verify the resync status by using metastat -c command.

 

Thank You !!!

How to create a ufs filesystem on a newly added disk in Solaris

Let’s here discuss how to create a new Filesystem on a newly added disk in Solaris. Below is the scenario shown with examples.

HDD

 

1. Once disk is added in the server ,check if it is visible in OS by running the below command

bash-3.00# echo | format
Searching for disks…done
AVAILABLE DISK SELECTIONS:
0. c0d1 <DEFAULT cyl 2607 alt 2 hd 255 sec 63>
/[email protected],0/[email protected],1/[email protected]/[email protected],0
Specify disk (enter its number): Specify disk (enter its number):
bash-3.00# Read more

2. If it is not visible in the OS run the below command to scan the newly added hardware.

bash-3.00# devfsadm          ===>(it works only for Solaris 10 and later versions. The older versions you need to perform a reconfigure reboot)

3. Check the format command again to confirm the disk is showing in the OS.

bash-3.00# echo | format
Searching for disks…done
AVAILABLE DISK SELECTIONS:
0. c0d1 <DEFAULT cyl 2607 alt 2 hd 255 sec 63>
/[email protected],0/[email protected],1/[email protected]/[email protected],0
1. c1t0d0 <DEFAULT cyl 1303 alt 2 hd 255 sec 63>
/[email protected],0/pci15ad,[email protected]/[email protected],0
Specify disk (enter its number): Specify disk (enter its number):
bash-3.00#


========================================================================================

4. Now we can partition the disk

bash-3.00# format
Searching for disks…done
AVAILABLE DISK SELECTIONS:
0. c0d1 <DEFAULT cyl 2607 alt 2 hd 255 sec 63>
/[email protected],0/[email protected],1/[email protected]/[email protected],0
1. c1t0d0 <DEFAULT cyl 1303 alt 2 hd 255 sec 63>
/[email protected],0/pci15ad,1976[email protected]/[email protected],0
Specify disk (enter its number): 1
selecting c1t0d0
[disk formatted]

FORMAT MENU:
disk – select a disk
type – select (define) a disk type
partition – select (define) a partition table
current – describe the current disk
format – format and analyze the disk
fdisk – run the fdisk program
repair – repair a defective sector
label – write label to the disk
analyze – surface analysis
defect – defect list management
backup – search for backup labels
verify – read and display labels
save – save new disk/partition definitions
inquiry – show vendor, product and revision
volname – set 8-character volume name
!<cmd> – execute <cmd>, then return
quit
format> format> p                         ===> “P” is used for partition
WARNING – This disk may be in use by an application that has
modified the fdisk table. Ensure that this disk is
not currently in use before proceeding to use fdisk.       ===> if we get this error do fdisk on the disk

format> fdisk
No fdisk table exists. The default partition for the disk is:

a 100% “SOLARIS System” partition

Type “y” to accept the default partition, otherwise type “n” to edit the
partition table.
y
format> p
PARTITION MENU:
0 – change `0′ partition
1 – change `1′ partition
2 – change `2′ partition
3 – change `3′ partition
4 – change `4′ partition
5 – change `5′ partition
6 – change `6′ partition
7 – change `7′ partition
select – select a predefined table
modify – modify a predefined partition table
name – name the current table
print – display the current table
label – write partition map and label to the disk
!<cmd> – execute <cmd>, then return
quit
partition>
partition> print                                     ===> print is used to print the current partition table on the disk available
Current partition table (original):
Total disk cylinders available: 1302 + 2 (reserved cylinders)

Part Tag Flag Cylinders Size Blocks
0 unassigned wm 0 0 (0/0/0) 0
1 unassigned wm 0 0 (0/0/0) 0
2 backup wu 0 – 1301 9.97GB (1302/0/0) 20916630
3 unassigned wm 0 0 (0/0/0) 0
4 unassigned wm 0 0 (0/0/0) 0
5 unassigned wm 0 0 (0/0/0) 0
6 unassigned wm 0 0 (0/0/0) 0
7 unassigned wm 0 0 (0/0/0) 0
8 boot wu 0 – 0 7.84MB (1/0/0) 16065
9 unassigned wm 0 0 (0/0/0) 0

partition>


partition> 0                                           ===> creating 0th partition in this disk
Part Tag Flag Cylinders Size Blocks
0 unassigned wm 0 0 (0/0/0) 0

Enter partition id tag[unassigned]: ?                     ===> ? mark is for help
Expecting one of the following: (abbreviations ok):
unassigned boot root swap
usr backup stand var
home alternates reserved

Enter partition id tag[unassigned]: var
Enter partition permission flags[wm]: ?
Expecting one of the following: (abbreviations ok):
wm – read-write, mountable
wu – read-write, unmountable
rm – read-only, mountable
ru – read-only, unmountable

Enter partition permission flags[wm]: wm
Enter new starting cyl[1]: ?
Expecting an integer from 0 to 1301
Enter new starting cyl[1]: 1
Enter partition size[0b, 0c, 1e, 0.00mb, 0.00gb]: 4g
partition> print
Current partition table (unnamed):
Total disk cylinders available: 1302 + 2 (reserved cylinders)

Part Tag Flag Cylinders Size Blocks
0 var wm 1 – 523 4.01GB (523/0/0) 8401995               ===> we have var partition created in 0th slice now.
1 unassigned wm 0 0 (0/0/0) 0
2 backup wu 0 – 1301 9.97GB (1302/0/0) 20916630
3 unassigned wm 0 0 (0/0/0) 0
4 unassigned wm 0 0 (0/0/0) 0
5 unassigned wm 0 0 (0/0/0) 0
6 unassigned wm 0 0 (0/0/0) 0
7 unassigned wm 0 0 (0/0/0) 0
8 boot wu 0 – 0 7.84MB (1/0/0) 16065
9 unassigned wm 0 0 (0/0/0) 0

partition>
partition> label                       ===> label is used to write the partition table with the new entry.
Ready to label disk, continue? yes

partition> q                            ===>q is used to quit the menu.
=======================================================================================


5. Now we can format the disk with UFS file system

bash-3.00# newfs /dev/rdsk/c1t0d0s0                      ===>newfs is the command used to create the filesystem. Default filesystem will be UFS.
newfs: construct a new file system /dev/rdsk/c1t0d0s0: (y/n)? y           ===> we can create filesystem only in rawdisks (rdsk).
Warning: 2998 sector(s) in last cylinder unallocated
/dev/rdsk/c1t0d0s0: 8401994 sectors in 1368 cylinders of 48 tracks, 128 sectors
4102.5MB in 86 cyl groups (16 c/g, 48.00MB/g, 5824 i/g)
super-block backups (for fsck -F ufs -o b=#) at:
32, 98464, 196896, 295328, 393760, 492192, 590624, 689056, 787488, 885920,
7472672, 7571104, 7669536, 7767968, 7866400, 7964832, 8063264, 8161696,
8260128, 8358560
bash-3.00#
==========================================================================================
6. Now we can mount the filesystem as /var

bash-3.00# mount /dev/dsk/c1t0d0s0 /var               ===> for mounting you should use “dsk” naming format (/dev/dsk/c1t0d0s0 )
bash-3.00# df -h /var
Filesystem size used avail capacity Mounted on
/dev/dsk/c1t0d0s0 3.9G 4.0M 3.9G 1% /var
bash-3.00#

Now you can make the entry permanent in /etc/vfstab

=============================================================================================

Hope this will help you. For more posts on Solaris you may click here

Thank you!!!