Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 11 Oct 2018 07:47:15 +0000
From:      Lorenzo Salvadore <phascolarctos@protonmail.ch>
To:        FreeBSD Questions <freebsd-questions@freebsd.org>
Subject:   Re: swap to a sparse file
Message-ID:  <TmVhvYYFMip6ATUlnJkKj2jq4FtJzspncxgcet4keU1-6m58EJpZaJkzKzxuQ4jry_by7SdUjdzZ4YJzjoMT-K0trnJ7_8OdgeoM3fbKirs=@protonmail.ch>
In-Reply-To: <20181011055236.138279b2.freebsd@edvax.de>
References:  <20181011014705.GA17798@admin.sibptus.ru> <20181011055236.138279b2.freebsd@edvax.de>

next in thread | previous in thread | raw e-mail | index | archive | help
> 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=3D/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=3D"/swap0"
> /bin/rm -f $SWAP
> /bin/dd if=3D/dev/zero of=3D$SWAP bs=3D16m seek=3D500 count=3D0
> /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.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?TmVhvYYFMip6ATUlnJkKj2jq4FtJzspncxgcet4keU1-6m58EJpZaJkzKzxuQ4jry_by7SdUjdzZ4YJzjoMT-K0trnJ7_8OdgeoM3fbKirs=>