LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-08-2009, 02:40 PM   #1
SlowCoder
Senior Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Debian based
Posts: 1,250

Rep: Reputation: 164Reputation: 164
Question about swap "file"


I saw the following post in another thread and thought it was interesting:
Quote:
welll here is a tip to increase the size of swap file manually.
#dd if=/dev/zero of=swap bs=1000000 count=1
#chmod 0 swap
make the swap file
#mkswap swap
add to current swap file
#swapon -v swap
chk if it works..
#cat /proc/swap
In Linux you create swap partitions, not files. how can you simply create a swap file and use it? I could see this as a Windows trick, but it doesn't seem logical in Linux, based on what I know. Can someone clarify for me?
 
Old 01-08-2009, 02:44 PM   #2
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Quote:
Originally Posted by SlowCoder View Post
I saw the following post in another thread and thought it was interesting:


In Linux you create swap partitions, not files. how can you simply create a swap file and use it? I could see this as a Windows trick, but it doesn't seem logical in Linux, based on what I know. Can someone clarify for me?
A bit old...but still good...

http://www.redhat.com/docs/manuals/l...ap-adding.html

Apparently Red Hat knows about it...

-C
 
Old 01-08-2009, 02:57 PM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Code:
man -k swapon
swapon (2)           - start/stop swapping to file/device
swapon (8)           - enable/disable devices and files for paging and swapping
So you don't even need to use losetup to attach a loop device to the swap file.
 
Old 01-08-2009, 03:18 PM   #4
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Linux 11 (Bullseye)
Posts: 3,407

Rep: Reputation: 141Reputation: 141
Quote:
Originally Posted by SlowCoder View Post
I saw the following post in another thread and thought it was interesting:


In Linux you create swap partitions, not files. how can you simply create a swap file and use it? I could see this as a Windows trick, but it doesn't seem logical in Linux, based on what I know. Can someone clarify for me?
A partition is just an entry in the device table, isn't it? One disk device is pretty much the same as another, regardless of what hardware/software it's instantiated in.
 
Old 01-08-2009, 07:22 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Linux has supported swap files (as well as partitions) for as long as I've been playing with it.
Prior to 2.5, the swap file entailed more overhead because it was a file, and needed additional pathlengh (VFS ??? - I dunno, I never looked). For anybody on 2.6 kernels this is no longer a concern - I/O to swap is now handled directly by the blocklayer driver(s). Same for files and partitions regardless.
 
Old 01-09-2009, 09:28 AM   #6
SlowCoder
Senior Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Debian based
Posts: 1,250

Original Poster
Rep: Reputation: 164Reputation: 164
Thank you all for your assistance in expansion of my knowledge.
 
Old 01-09-2009, 10:10 AM   #7
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
You can create a file for swap space but its not the best solution in many cases because files have a tendancy, on a well used partition, to not end up sequential which generally slows down access time.

Now that I think about it however, I'd like to see a compile time comparison on a machine with little ram and plenty of swap using various file systems, ext2, ext3, ext4, jfs, xfs, reiserfs, etc.
 
Old 01-09-2009, 03:26 PM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Swap files ain't "files" - they won't fragment with use. Last I looked at the mkswap code it wasn't very friendly, and made no allowance for non-contiguous allocation. IIRC (nowadays) the space map only has the length, and marks any bad blocks within that range. I don't think I'd allow (subsequent) fragmentation within the originally allocated extent that mkswap knew about.

But then, I don't use swap files, so I don't need to worry about that ...
 
Old 01-09-2009, 04:02 PM   #9
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
Quote:
Originally Posted by syg00 View Post
Swap files ain't "files" - they won't fragment with use. Last I looked at the mkswap code it wasn't very friendly, and made no allowance for non-contiguous allocation. IIRC (nowadays) the space map only has the length, and marks any bad blocks within that range. I don't think I'd allow (subsequent) fragmentation within the originally allocated extent that mkswap knew about.

But then, I don't use swap files, so I don't need to worry about that ...
A true swap "file" won't fragment with use but it may be fragmented to begin with. I've done a good bit of work doing file recovery on ext2/3 file systems so I'm pretty familiar with how files are allocated when using a program like dd... especially in the case of a large file (say a gig) and a heavily used file system (been in production for a long time) you very well may end up with a non-contiguous swap file especially if its very large (>1g). I would have to dig around in the code of mkswap to be absolutely positive, but I can't imagine it even caring if every single block used was non-contiguous.... that becomes something the file system has to deal with. Look at the process slow coder is using to generate the swap file... that *IS* a true file on the file system.

It's a moot point for me too, if i'm dipping into the swap on the system I've got more significant issues than how the swap files are function, but shrug, might as well keep it as accurate as possible.

Last edited by rweaver; 01-09-2009 at 04:05 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 06:58 AM
newbie question: whats the difference between "su root", "su" and "su -&quo mojarron Slackware 9 12-07-2009 04:08 PM
swap file instead of swap disk "dd" error rezwits Linux - Newbie 5 12-16-2007 12:34 AM
Swap file "drive" or partition for new PC/speed questions? robbow52 Linux - Hardware 4 01-08-2004 04:59 PM
"E297: Write error in swap file" :-( sudhir_gunda Linux From Scratch 2 12-29-2003 01:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration