U_S_A
asked on
Shrink Linux Swap File Size CentOS
Hi,
I've had my server running for 6 years without any real need for a large swap file. The partition with the swap file is running out of space. I'd like to free up space by reducing the size of the swap file. Could someone give me the steps and specific commands to shrink or make a new smaller swap file? I'd like to have a 1GB swap file - Not a 32GB swap file. -- you can see from my top command that in the last 93 days I've only used 80K of the swap.
I'm running CentOS
Here is information that I've gathered which may be helpful. Let me know if you need any other information.
Thank you in advance for your help.
The swap file is in the /home directory
In preparation , I made a copy of my /etc/fstab file. This is what it looks like
I've had my server running for 6 years without any real need for a large swap file. The partition with the swap file is running out of space. I'd like to free up space by reducing the size of the swap file. Could someone give me the steps and specific commands to shrink or make a new smaller swap file? I'd like to have a 1GB swap file - Not a 32GB swap file. -- you can see from my top command that in the last 93 days I've only used 80K of the swap.
I'm running CentOS
Here is information that I've gathered which may be helpful. Let me know if you need any other information.
Thank you in advance for your help.
The swap file is in the /home directory
root@cv2 [~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/sda6 partition 2097144 80 0
/home/swap/systemswap file 31999992 0 -1
root@cv2 [/home/swap]# ls -lh
total 31G
-rw-r--r-- 1 root root 31G Oct 30 2011 systemswap
root@cv2 [/etc]# grep Swap /proc/meminfo
SwapCached: 80 kB
SwapTotal: 34097136 kB
SwapFree: 34097056 kB
root@cv2 [/etc]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.9G 4.0G 5.4G 43% /
tmpfs 56G 5.8G 50G 11% /dev/shm
/dev/sda1 99M 71M 23M 76% /boot
/dev/sda8 198G 186G 2.0G 99% /home
/dev/sda7 1008M 117M 840M 13% /tmp
/dev/sda3 9.9G 9.4G 15M 100% /usr
/dev/sda5 9.9G 4.7G 4.8G 50% /var
/dev/sdb1 1.1T 970G 74G 93% /disk1
top - 22:40:38 up 93 days, 6:27, 2 users, load average: 8.03, 5.80, 5.03
Tasks: 1929 total, 5 running, 1924 sleeping, 0 stopped, 0 zombie
Cpu(s): 8.1%us, 2.9%sy, 0.0%ni, 85.5%id, 2.8%wa, 0.0%hi, 0.8%si, 0.0%st
Mem: 115595148k total, 88334512k used, 27260636k free, 316568k buffers
Swap: 34097136k total, 80k used, 34097056k free, 7282052k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
6604 mysql 20 0 95.0g 69g 11m S 200.4 62.6 191014:01 mysqld
8685 nobody 20 0 1882m 614m 2396 S 62.3 0.5 174:29.26 memcached
46512 nobody 20 0 220m 81m 6088 S 0.0 0.1 0:58.35 httpd
55380 nobody 20 0 198m 59m 5996 S 0.0 0.1 0:08.45 httpd
35678 nobody 20 0 176m 36m 5616 S 0.3 0.0 0:12.33 httpd
1181 nobody 20 0 172m 32m 6120 S 2.0 0.0 0:18.90 httpd
11305 nobody 20 0 170m 30m 6008 S 1.6 0.0 0:32.45 httpd
In preparation , I made a copy of my /etc/fstab file. This is what it looks like
# /etc/fstab
# Created by anaconda on Thu Oct 27 00:34:52 2011
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=974d8ad5-7b3b-4e76-a15e-883a26e069b0 / ext4 defaults,noatime 1 1
UUID=b5e08c39-eb88-44c1-aa67-6f4873ef616e /boot ext2 defaults,noatime 1 2
UUID=30b03ace-e109-4c17-b7f5-d65d9844d9db /home ext4 defaults,noatime 1 2
UUID=126a480d-e7e7-4620-b28e-b95c0bc17dab /tmp ext4 defaults,noatime 1 2
UUID=4858ba50-62db-45ae-860b-9df020a1abfd /usr ext4 defaults,noatime 1 2
UUID=92df8c83-6892-4d14-ad7b-04f173053cd9 /var ext4 defaults,noatime 1 2
UUID=4923b91a-942c-4283-8597-85d63514e29e swap swap pri=0,defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/tmp /var/tmp ext3 defaults,bind,noauto 0 0
/dev/sdb1 /disk1 ext4 defaults,noatime 1 2
/home/swap/systemswap swap swap defaults,noatime 0 0
# //10.10.10.99/SL999999-1 /mnt/nas cifs rw,username=SL999999-1,password=XXXXXXXX 0 0
#/dev/sdb1 /disk1 ext4 defaults,atime,strictatime,norelatime 1 2
#
# mysql_cache is in RAM used for file based sql caching
#tmpfs /disk1/mysql_cache tmpfs size=1500m 0 0
root@cv2 [/dev]# free -m
total used free shared buffers cached
Mem: 112885 88629 24255 4268 2626 9617
-/+ buffers/cache: 76386 36499
Swap: 33297 0 33297
root@cv2 [/home/swap]# fdisk -l
Disk /dev/sdb: 1198.3 GB, 1198295875584 bytes
255 heads, 63 sectors/track, 145684 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000a25cb
Device Boot Start End Blocks Id System
/dev/sdb1 * 1 145684 1170206698+ 83 Linux
Disk /dev/sda: 250.8 GB, 250808893440 bytes
255 heads, 63 sectors/track, 30492 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0009164a
Device Boot Start End Blocks Id System
/dev/sda1 * 1 14 104448 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 14 1319 10485760 83 Linux
/dev/sda3 1319 2624 10485760 83 Linux
/dev/sda4 2624 30493 223853568 5 Extended
/dev/sda5 2625 3930 10485760 83 Linux
/dev/sda6 3930 4191 2097152 82 Linux swap / Solaris
/dev/sda7 4191 4322 1048576 83 Linux
/dev/sda8 4322 30493 210217984 83 Linux
root@cv2 [/home/swap]#
ASKER
@Natty,
I think my top command screen shot, in my question answers the question of how much RAM I have - I could be wrong though.
Mem: 115595148k total
How do I know if my Swap had been used in the last 90 days? I think top command also gives me that information, unless I don't understand that too.
Swap: 34097136k total, 80k used, 34097056k free, 7282052k cached
Is it 80K?
top also shows typical load on my machine 24x7 ,,, in fact it typically is less load.
I think my top command screen shot, in my question answers the question of how much RAM I have - I could be wrong though.
Mem: 115595148k total
How do I know if my Swap had been used in the last 90 days? I think top command also gives me that information, unless I don't understand that too.
Swap: 34097136k total, 80k used, 34097056k free, 7282052k cached
Is it 80K?
top also shows typical load on my machine 24x7 ,,, in fact it typically is less load.
Make sense in my little hast to skim the info
Here you go sir
Let’s begin but first turning off swap on the Swap Logical Volume
# swapoff /dev/VolGroup00/LogVol01
Once swap is off let’s take the disk space we require.
# lvresize -L -4GB /dev/VolGroup00/LogVol01
WARNING: Reducing active logical volume to 896.00 MB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce LogVol01? [y/n]: y
Reducing logical volume LogVol01 to 896.00 MB
Logical volume LogVol01 successfully resized
Now let’s add what we removed to the main Logical Volume.
# lvresize -L +4GB /dev/VolGroup00/LogVol00
Extending logical volume LogVol00 to 9.00 GB
Logical volume LogVol00 successfully resized
Resize the File System
# resize2fs -p /dev/VolGroup00/LogVol00
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/VolGroup00/LogVol00 is mounted on /; on-line resizing required
Performing an on-line resize of /dev/VolGroup00/LogVol00 to 2359296 (4k) blocks.
The filesystem on /dev/VolGroup00/LogVol00 is now 2359296 blocks long.
Rebuild the swap partition.
# mkswap /dev/VolGroup00/LogVol01
Setting up swapspace version 1, size = 939520 kB
Turn swap on.
# swapon /dev/VolGroup00/LogVol01
Let’s check the amount of disk space available and LVM attributes to see if our changes took effect.
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-Log Vol00
8.8G 2.4G 6.0G 28% /
/dev/xvda1 99M 23M 72M 25% /boot
tmpfs 4.0G 0 4.0G 0% /dev/shm
You can get the full scoup here if you want to double check how to do the swap space shrink
http://www.how2centos.com/centos-lvm-resizing-guide/
Let’s begin but first turning off swap on the Swap Logical Volume
# swapoff /dev/VolGroup00/LogVol01
Once swap is off let’s take the disk space we require.
# lvresize -L -4GB /dev/VolGroup00/LogVol01
WARNING: Reducing active logical volume to 896.00 MB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce LogVol01? [y/n]: y
Reducing logical volume LogVol01 to 896.00 MB
Logical volume LogVol01 successfully resized
Now let’s add what we removed to the main Logical Volume.
# lvresize -L +4GB /dev/VolGroup00/LogVol00
Extending logical volume LogVol00 to 9.00 GB
Logical volume LogVol00 successfully resized
Resize the File System
# resize2fs -p /dev/VolGroup00/LogVol00
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/VolGroup00/LogVol00 is mounted on /; on-line resizing required
Performing an on-line resize of /dev/VolGroup00/LogVol00 to 2359296 (4k) blocks.
The filesystem on /dev/VolGroup00/LogVol00 is now 2359296 blocks long.
Rebuild the swap partition.
# mkswap /dev/VolGroup00/LogVol01
Setting up swapspace version 1, size = 939520 kB
Turn swap on.
# swapon /dev/VolGroup00/LogVol01
Let’s check the amount of disk space available and LVM attributes to see if our changes took effect.
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-Log
8.8G 2.4G 6.0G 28% /
/dev/xvda1 99M 23M 72M 25% /boot
tmpfs 4.0G 0 4.0G 0% /dev/shm
You can get the full scoup here if you want to double check how to do the swap space shrink
http://www.how2centos.com/centos-lvm-resizing-guide/
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@Natty, I was hoping for exact commands that I would use in my setup. Not a copy/paste from someone's guide. I found many guides, but was to afraid to make guesses as to what my commands would look like.
It is for Centos and you're using Centos am I right. so that the step to resize the swap, its a similar for ubuntu, redhat, fedora, mandrake I have checked and everyone uses the same command a little diferent cyntax for different os. This is the one for you.
ASKER
This is what I did
root@cv2 [/home]# cp /etc/fstab /etc/fstab.2017.04.19
root@cv2 [/home]# swapoff /home/swap/systemswap
root@cv2 [/home]# swapoff -a
root@cv2 [/home]# rm /home/swap/systemswap
root@cv2 [/home]# dd if=/dev/zero of=/home/swap/systemswap bs=1024 count=1048576
1048576+0 records in
1048576+0 records out
1073741824 bytes (1.1 GB) copied, 3.31458 s, 324 MB/s
root@cv2 [/home]# mkswap /home/swap/systemswap
mkswap: /home/swap/systemswap: warning: don't erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=d4786501-508d-4e88-9e 3b-406e9d4 7c828
#in fstab, commented out the UUID line and here is what it looks like now <--
#UUID=3923b91a-942c-4283-8 597-85d635 14e29e swap swap pri=0,defaults 0 0
#left this line alone
/home/swap/systemswap swap swap defaults,noatime 0 0
root@cv2 [/home]# swapon /home/swap/systemswap
root@cv2 [/home]# grep SwapTotal /proc/meminfo
SwapTotal: 1048568 kB
root@cv2 [/home]# free -m
total used free shared buffers cached
Mem: 112885 93393 19491 5839 3542 13127
-/+ buffers/cache: 76723 36161
Swap: 1023 0 1023
all done
Thanks for the help
root@cv2 [/home]# cp /etc/fstab /etc/fstab.2017.04.19
root@cv2 [/home]# swapoff /home/swap/systemswap
root@cv2 [/home]# swapoff -a
root@cv2 [/home]# rm /home/swap/systemswap
root@cv2 [/home]# dd if=/dev/zero of=/home/swap/systemswap bs=1024 count=1048576
1048576+0 records in
1048576+0 records out
1073741824 bytes (1.1 GB) copied, 3.31458 s, 324 MB/s
root@cv2 [/home]# mkswap /home/swap/systemswap
mkswap: /home/swap/systemswap: warning: don't erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=d4786501-508d-4e88-9e
#in fstab, commented out the UUID line and here is what it looks like now <--
#UUID=3923b91a-942c-4283-8
#left this line alone
/home/swap/systemswap swap swap defaults,noatime 0 0
root@cv2 [/home]# swapon /home/swap/systemswap
root@cv2 [/home]# grep SwapTotal /proc/meminfo
SwapTotal: 1048568 kB
root@cv2 [/home]# free -m
total used free shared buffers cached
Mem: 112885 93393 19491 5839 3542 13127
-/+ buffers/cache: 76723 36161
Swap: 1023 0 1023
all done
Thanks for the help
ASKER
Thank you.
Without limiting the swap file size, the growth of the swap file you had before will occur again.
More is better, less could be trouble