swap to a sparse file

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

swap to a sparse file

Victor Sudakov-3
Dear Colleagues,

I've inherited a swapless FreeBSD 11.2 host with no unused disk space.
I would like to add some swap to be on the safe side: in case there is
a memory leak etc.

On the other hand, I don't like to waste several gigabytes on a
precious SSD because perhaps the system will never need this swap
space anyway.

The FreeBSD Handbook prescribes creating a swap file with "dd if=/dev/zero ..."
which would waste the space.

Is there any good reason I can't just "truncate -s2G /swap0" and
make the swap a sparse file?

Thanks in advance for any input.

--
Victor Sudakov,  VAS4-RIPE, VAS47-RIPN
2:5005/49@fidonet http://vas.tomsk.ru/
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: swap to a sparse file

Polytropon
On Thu, 11 Oct 2018 08:47:05 +0700, Victor Sudakov wrote:
> I've inherited a swapless FreeBSD 11.2 host with no unused disk space.
> I would like to add some swap to be on the safe side: in case there is
> a memory leak etc.
>
> On the other hand, I don't like to waste several gigabytes on a
> precious SSD because perhaps the system will never need this swap
> space anyway.

Exactly my thoughts. :-)



> The FreeBSD Handbook prescribes creating a swap file with "dd if=/dev/zero ..."
> which would waste the space.

A regular file as swap space also places additional
load into the virtual file system, whereas one of the
key advantages of a swap _partition_ is that no file
access (through a filesystem) has to take place.



> Is there any good reason I can't just "truncate -s2G /swap0" and
> make the swap a sparse file?

I'm using a "pseudo-file" approach which hasn't shown any
problems so far. I'm using dd, but no actual disk space
will be allocated for the target file. That file then is
used like a regular swap partition via mdconfig's virtual
node facility, and also deleted.

Sure, it probably isn't the ideal solution, but it works.

Maybe you can use this for further inspiration?



In /etc/rc.local:

SWAP="/swap0"
/bin/rm -f $SWAP
/bin/dd if=/dev/zero of=$SWAP bs=16m seek=500 count=0
/sbin/mdconfig -a -t vnode -u 0 -f $SWAP || /bin/sh
/bin/chflags nodump $SWAP
/bin/rm $SWAP
/sbin/swapctl -a /dev/md0
echo -n " swap"



In /etc/rc.shutdown.local:

/sbin/swapctl -d /dev/md0 > /dev/null 2>&1
/sbin/mdconfig -d -u 0
echo -n " swap"




--
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: swap to a sparse file

freebsd-questions mailing list
> On Thu, 11 Oct 2018 08:47:05 +0700, Victor Sudakov wrote:
>
> > I've inherited a swapless FreeBSD 11.2 host with no unused disk space.
> > I would like to add some swap to be on the safe side: in case there is
> > a memory leak etc.
> > On the other hand, I don't like to waste several gigabytes on a
> > precious SSD because perhaps the system will never need this swap
> > space anyway.
>
> Exactly my thoughts. :-)
>
> > The FreeBSD Handbook prescribes creating a swap file with "dd if=/dev/zero ..."
> > which would waste the space.
>
> A regular file as swap space also places additional
> load into the virtual file system, whereas one of the
> key advantages of a swap partition is that no file
> access (through a filesystem) has to take place.
>
> > Is there any good reason I can't just "truncate -s2G /swap0" and
> > make the swap a sparse file?
>
> I'm using a "pseudo-file" approach which hasn't shown any
> problems so far. I'm using dd, but no actual disk space
> will be allocated for the target file. That file then is
> used like a regular swap partition via mdconfig's virtual
> node facility, and also deleted.
>
> Sure, it probably isn't the ideal solution, but it works.
>
> Maybe you can use this for further inspiration?
>
> In /etc/rc.local:
>
> SWAP="/swap0"
> /bin/rm -f $SWAP
> /bin/dd if=/dev/zero of=$SWAP bs=16m seek=500 count=0
> /sbin/mdconfig -a -t vnode -u 0 -f $SWAP || /bin/sh
> /bin/chflags nodump $SWAP
> /bin/rm $SWAP
> /sbin/swapctl -a /dev/md0
> echo -n " swap"
>
> In /etc/rc.shutdown.local:
>
> /sbin/swapctl -d /dev/md0 > /dev/null 2>&1
>
> /sbin/mdconfig -d -u 0
> echo -n " swap"

Another solution if you have a zfs filesystem is to create a new
filesystem for swap that would contain only a swapfile. That way
the dedicated filesystem would use space only when needed and
shrink when unneeded, but you could also set a minimal amount
of space and a maximal amount.

I do not know what is your exact situation with this host you
inherited, but maybe it would not be a bad idea to just reinstall
a brand new FreeBSD on it with the partitioning scheme you want
(of course, you might need to backup something).

Lorenzo Salvadore.
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: swap to a sparse file

Steve O'Hara-Smith
In reply to this post by Victor Sudakov-3
On Thu, 11 Oct 2018 08:47:05 +0700
Victor Sudakov <[hidden email]> wrote:

> Is there any good reason I can't just "truncate -s2G /swap0" and
> make the swap a sparse file?

        Just one - if the space isn't there when the system needs it there
will be a panic.

--
Steve O'Hara-Smith <[hidden email]>
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: swap to a sparse file

freebsd-questions mailing list
On Thu, 11 Oct 2018 08:51:14 +0100
Steve O'Hara-Smith wrote:

> On Thu, 11 Oct 2018 08:47:05 +0700
> Victor Sudakov <[hidden email]> wrote:
>
> > Is there any good reason I can't just "truncate -s2G /swap0" and
> > make the swap a sparse file?  
>
> Just one - if the space isn't there when the system needs it
> there will be a panic.

Is that really the only reason? I've not paid much attention, but the
warnings I've heard about this have made it sound a lot worse.
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: swap to a sparse file

Steve O'Hara-Smith
On Fri, 12 Oct 2018 19:36:33 +0100
RW via freebsd-questions <[hidden email]> wrote:

> On Thu, 11 Oct 2018 08:51:14 +0100
> Steve O'Hara-Smith wrote:
>
> > On Thu, 11 Oct 2018 08:47:05 +0700
> > Victor Sudakov <[hidden email]> wrote:
> >
> > > Is there any good reason I can't just "truncate -s2G /swap0" and
> > > make the swap a sparse file?  
> >
> > Just one - if the space isn't there when the system needs it
> > there will be a panic.
>
> Is that really the only reason? I've not paid much attention, but the
> warnings I've heard about this have made it sound a lot worse.

        There's not much worse the system can do than panic, other than
deadlock which might happen if memory needs to be allocated in order to add
blocks to the file. IIRC swap on a zvol can deadlock that way.

--
Steve O'Hara-Smith <[hidden email]>
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: swap to a sparse file

freebsd-questions mailing list
On Sat, 13 Oct 2018 08:11:18 +0100
Steve O'Hara-Smith wrote:

> On Fri, 12 Oct 2018 19:36:33 +0100
> RW via freebsd-questions <[hidden email]> wrote:
>
> > On Thu, 11 Oct 2018 08:51:14 +0100
> > Steve O'Hara-Smith wrote:
> >  
> > > On Thu, 11 Oct 2018 08:47:05 +0700
> > > Victor Sudakov <[hidden email]> wrote:
> > >  
> > > > Is there any good reason I can't just "truncate -s2G /swap0" and
> > > > make the swap a sparse file?    
> > >
> > > Just one - if the space isn't there when the system needs
> > > it there will be a panic.  
> >
> > Is that really the only reason? I've not paid much attention, but
> > the warnings I've heard about this have made it sound a lot worse.  
>
> There's not much worse the system can do than panic, other
> than deadlock which might happen if memory needs to be allocated in
> order to add blocks to the file. IIRC swap on a zvol can deadlock
> that way.


Yes, but filling up a drive is relatively avoidable - particularly if
there's space reserved for root.
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: swap to a sparse file

Steve O'Hara-Smith
On Sat, 13 Oct 2018 14:52:47 +0100
RW via freebsd-questions <[hidden email]> wrote:

> On Sat, 13 Oct 2018 08:11:18 +0100
> Steve O'Hara-Smith wrote:
>
> > On Fri, 12 Oct 2018 19:36:33 +0100
> > RW via freebsd-questions <[hidden email]> wrote:
> >
> > > On Thu, 11 Oct 2018 08:51:14 +0100
> > > Steve O'Hara-Smith wrote:
> > >  
> > > > On Thu, 11 Oct 2018 08:47:05 +0700
> > > > Victor Sudakov <[hidden email]> wrote:
> > > >  
> > > > > Is there any good reason I can't just "truncate -s2G /swap0" and
> > > > > make the swap a sparse file?    
> > > >
> > > > Just one - if the space isn't there when the system needs
> > > > it there will be a panic.  
> > >
> > > Is that really the only reason? I've not paid much attention, but
> > > the warnings I've heard about this have made it sound a lot worse.  
> >
> > There's not much worse the system can do than panic, other
> > than deadlock which might happen if memory needs to be allocated in
> > order to add blocks to the file. IIRC swap on a zvol can deadlock
> > that way.
>
>
> Yes, but filling up a drive is relatively avoidable - particularly if
> there's space reserved for root.

        True enough, although if you are counting on having the space
available then pre-allocating it is a strong guarantee and prevents the
performance penalty of adding a block to a file as an overhead of swapping.

--
Steve O'Hara-Smith <[hidden email]>
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"