[Toybox] New toys: swapon and swapoff

Rob Landley rob at landley.net
Sun Feb 19 15:33:29 PST 2012


On 02/18/2012 08:38 AM, Elie De Brauwer wrote:
> Hi all,
> 
> In attach you can find initial implementations of swapon and swapoff,
> swapon supports passing the priority, there is however no support for -a.

Cool.  Applied.

Possibly swap.h belongs in portability.h? (I don't see that one in susv4
and it strikes me as something android and macosx might not have.  If
they can't #include toys.h they can't build anything.)

Do we still need to & with the the priority mask if we're already
limiting the input range?  I think I'll take that out...  (The shift
value is 0?  Compiler will optimize that out but: dude, _what_?)

Regarding -a support, I note lib/getmountlist, which I grabbed ages and
ages ago as "a function I 100% wrote from my old mount code" in
preparation for doing a new mount implementation in toybox.

Hmmm...  except that /proc/mounts hasn't got swap in it?  Ah,
/proc/swaps.  In a slightly different format.  With a gratuitous header
line. (Oh yeah, /proc is disgusting...)

Sigh.

Anyway, swapon -a needs to parse /etc/fstab (I.E. code shared with
mount), and swapoff needs to parse /proc/swaps (code not really shared
with anything, only the first field of second and later lines is
interesting here, although you run it through the equivalent of "echo
-e" to undo the octal escapes the kernel does for embedded spaces and
newlines and backslashes and such so maybe that code's shared somehow...)

> Toybox only swapping snippet:
> edb at lapedb:~/toybox$ dd if=/dev/zero of=swapfile bs=10M count=3
> 3+0 records in
> 3+0 records out
> 31457280 bytes (31 MB) copied, 0.02503 s, 1.3 GB/s
> edb at lapedb:~/toybox$ ./toybox mkswap swapfile
> Swapspace size: 30716k
> edb at lapedb:~/toybox$ sudo ./toybox swapon swapfile
> edb at lapedb:~/toybox$ cat /proc/swaps
> Filename            Type        Size    Used Priority
> /dev/sda2                       partition    9445508    0    -1
> /home/edb/toybox/swapfile      file        30716    0    -2
> edb at lapedb:~/toybox$ sudo ./toybox swapoff swapfile
> edb at lapedb:~/toybox$ cat /proc/swaps
> Filename            Type        Size    Used Priority
> /dev/sda2                       partition    9445508    0    -1
> edb at lapedb:~/toybox$

Very nice,

Rob

 1329694409.0


More information about the Toybox mailing list