Operating System - Linux
1820645 Members
1999 Online
109626 Solutions
New Discussion юеВ

adding different swap files in one swap file

 
Beginner_2
Occasional Advisor

adding different swap files in one swap file

i have /tmpswap1 and /tmpswap2 swap files . i want to have one swap file like /tmpswap.
Can anybody suggest how to do it?
2 REPLIES 2
Stephen P. Schaefer
Frequent Advisor

Re: adding different swap files in one swap file

Are the swap files already in use? That is, do they appear in the output of

cat /proc/swaps

? I'll assume they do.

Is the amount of free disk space on the root partition larger than the sum of the two swap areas? For simplicity, I'm going to assume it is. For the sake of illustration, I'll assume that the two existing files are 2Gbyte, so you want a single swap area of 4Gbyte.

Create the new swap area and make it available:

dd if=/dev/zero of=/tmpswap bs=1M count=4096
mkswap /tmpswap
swapon /tmpswap

Now remove the two old swap files; it may take a while for the kernel to move pages from the old swap area to the new; I'd probably have "top" running in a separate terminal to watch the progress.

swapoff /tmpswap1 # wait for finish
swapoff /tmpswap2 # wait for finish

Once the swap areas are no longer reported in

cat /proc/swaps

you can delete them

rm /tmpswap[12]

Of course, disk partitions are preferable to files for swap areas. I assume you don't have a partition available. One relatively cheap way to add disk, at least temporarily, is with a thumb USB drive - SSD drives are particularly well suited to the random disk access pattern of a swap area. If you don't have enough space for the new and the old files, such a USB addition may be particularly appropriate, even if temporary: determine the name of the USB drive (e.g., /dev/sdb); partition the USB drive

fdisk /dev/sdb

(read the fdisk manual page); set up the partition as swap

mkswap /dev/sdb1
swapon /dev/sdb1
swapoff /tmpswap1
swapoff /tmpswap2
cat /proc/swaps # old swap files are no longer there
rm /tmpswap[12]

at which point, if you don't want to continue using the USB drive, you can create a new swap file as described above.
Beginner_2
Occasional Advisor

Re: adding different swap files in one swap file

Thanks a lot..
it was really good description assuming many things. it is a good solution but i just wanted to know is there any way to add existing swap files or partitions after swapping them off and them turn the resultant file or partition on ?
Here we are creating new swap file/partition and then removing existing partition.
Is there any way to add existing swap file/partition?