# Using an image file for swap space



## PeterSteele (Sep 8, 2009)

Are there any advantages to using mdconfig and using an image file for swap space as opposed to having a designated swap partition? For example, I could do something like this:


```
dd if=/dev/zero of=/var/swap0 bs=1024 count=4m
mdconfig -a -t swap -f /var/swap0 -s 4g
swapon â€“a /dev/md0
```

to add 4G to the system swap space backed by the file /var/swap0 (through the special device /dev/md0). How would this compare to repartitioning my hard drive and adding a new 4GB swap partition?


----------



## fronclynne (Sep 9, 2009)

It won't be a big deal at all.

/etc/rc.conf offers the functionality in the form of 
	
	



```
swapfile="/var/swap0"
```
(in your case here).  Though I believe that you can only have one in rc.conf.

Note that this may not be the best option for RAID or zfs.


Edit: & it will be a lot less of a headache than repartitioning, in most cases.


----------



## PeterSteele (Sep 9, 2009)

What about the comments at this link:

http://forums.freebsd.org/archive/index.php/t-1946.html

This guy indicates I/O performance is impacted when using a mdconfig based file system. I realize this isn't directly related to using mdconfig for hosting swap space, but I thought the same concerns might apply.


----------



## fronclynne (Sep 9, 2009)

As a (non-scientific) test (this is my lap-top just browsing the web & all):

```
> dd if=Video/IT_CONQUERED_THE_WORLD.AVI of=/dev/null bs=1m
700+1 records in
700+1 records out
734490624 bytes transferred in 51.388414 secs (14292923 bytes/sec)
```
So that gives us a sort-of baseline (although it seems really slow).
Then we make a file & such:

```
> dd if=/dev/zero of=tmp/trash.file bs=4m count=250
250+0 records in
250+0 records out
1048576000 bytes transferred in 23.669224 secs (44301241 bytes/sec)
> sudo mdconfig -a -t vnode -f tmp/trash.file 
md0
```
Now, let us shuffle some stuff around:

```
> dd if=Video/Aaltra.avi of=/dev/null bs=1m
661+1 records in
661+1 records out
693546428 bytes transferred in 16.508841 secs (42010607 bytes/sec)
```
That was 3x as fast . . . weird (really, what?  this is closer to the speed I was expecting from experience, but prefetching?  did I spill beer on it?).
& again, while it's read into memory, send it to the md0:

```
> sudo dd if=Video/Aaltra.avi of=/dev/md0 bs=1m
dd: /dev/md0: Invalid argument
661+1 records in
661+0 records out
693108736 bytes transferred in 29.082156 secs (23832784 bytes/sec)
```
Now, what happens when we put a filesystem on the memory device and write (the already cached movie) to it?

```
> sudo bsdlabel -w /dev/md0
> sudo newfs /dev/md0a
/dev/md0a: 1000.0MB (2047984 sectors) block size 16384, fragment size 2048
        using 6 cylinder groups of 183.72MB, 11758 blks, 23552 inodes.
super-block backups (for fsck -b #) at:
 160, 376416, 752672, 1128928, 1505184, 1881440
> sudo mount /dev/md0a /mnt/temp
> sudo dd if=Video/Aaltra.avi of=/mnt/temp/aaltra.avi bs=1m
661+1 records in
661+1 records out
693546428 bytes transferred in 26.833470 secs (25846319 bytes/sec)
```
Doesn't seem to be a lot of overhead in ufs . . . ?!
Okay, well, away with the md(4) and a couple of more passes with already cached information:

```
> sudo umount /mnt/temp/
> sudo mdconfig -d -u 0
> dd if=Video/Aaltra.avi of=tmp/aaltra.avi bs=1m
661+1 records in
661+1 records out
693546428 bytes transferred in 15.745293 secs (44047857 bytes/sec)
```
4G of memory is nice:

```
> dd if=Video/IT_CONQUERED_THE_WORLD.AVI of=/dev/null bs=1m
700+1 records in
700+1 records out
734490624 bytes transferred in 1.167399 secs (629168492 bytes/sec)
> dd if=Video/Aaltra.avi of=/dev/null bs=1m
661+1 records in
661+1 records out
693546428 bytes transferred in 0.947022 secs (732344609 bytes/sec)
```
And, you know, I deleted the md0 file before I remembered to test reading from it.  [red]So much for SCIENCE.[/red]

Anyway, it looks to me like the md(4) is slow, but not horrible.  It doesn't seem to me to affect system I/O by dint of existing, though.

Anyway, it's swap, and I'm not sure what you're doing that's causing so much swapping that the I/O performance is important, but I'll not judge.


----------



## PeterSteele (Sep 9, 2009)

Thanks for your analysis. I/O performance is important to us since we're developing a storage device based on FreeBSD, but I won't bore you with the details...


----------



## chavez243ca (Sep 14, 2009)

PeterSteele said:
			
		

> Thanks for your analysis. I/O performance is important to us since we're developing a storage device based on FreeBSD, but I won't bore you with the details...



it might be worthwhile to look into FreeNAS or Openfiler before you re-invent the wheel.  Could save you much time / headache.


----------



## SirDice (Sep 14, 2009)

PeterSteele said:
			
		

> Thanks for your analysis. I/O performance is important to us since we're developing a storage device based on FreeBSD, but I won't bore you with the details...



If performance is an issue think about setting up multiple swap partitions on different drives. Which is how I've set up mine.

Don't be tempted to use a hardware raid 0 configuration to store swap. If one of the drives dies the raid 0 will die taking all your swap with it. If you spread it around multiple disks there will always be at least some swap available.


----------

