From owner-freebsd-questions@freebsd.org Thu Oct 11 07:47:29 2018 Return-Path: Delivered-To: freebsd-questions@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 5FF3410D57FE for ; Thu, 11 Oct 2018 07:47:29 +0000 (UTC) (envelope-from phascolarctos@protonmail.ch) Received: from mail2.protonmail.ch (mail2.protonmail.ch [185.70.40.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "*.protonmail.ch", Issuer "QuoVadis Global SSL ICA G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id F00B58BA49 for ; Thu, 11 Oct 2018 07:47:28 +0000 (UTC) (envelope-from phascolarctos@protonmail.ch) Date: Thu, 11 Oct 2018 07:47:15 +0000 To: FreeBSD Questions From: Lorenzo Salvadore Reply-To: Lorenzo Salvadore Subject: Re: swap to a sparse file Message-ID: In-Reply-To: <20181011055236.138279b2.freebsd@edvax.de> References: <20181011014705.GA17798@admin.sibptus.ru> <20181011055236.138279b2.freebsd@edvax.de> Feedback-ID: X6az_D2smWSR8MT5MHqXnWF0upxehDyHia7Id1cbayHNBUkRu3CIeusDsZHiivIIjmaKB1_OofpALrRUYjNz3w==:Ext:ProtonMail MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.1 required=7.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on mail.protonmail.ch X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 11 Oct 2018 07:47:29 -0000 > 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.