# gpart: add swap first?



## dave (Feb 8, 2013)

When building a bootable image for SD card or other such flash memory, it would be nice to add the swap slice first, setting its size, and subsequently add the freebsd slice, letting gpart just use whatever space is left on the device.  Is this possible?  Does it affect booting?


----------



## wblock@ (Feb 8, 2013)

It should not be a problem, but why bother?  Capacity - swapsize = filesystem size.  Allocate that much, then the rest to swap.


----------



## dave (Feb 8, 2013)

Doesn't seem that simple in my experience.  For instance, consider your online example.  There is first required space for the boot slice.  Then, the second slice has to start at 1MB boundary.  So, if I start with a 2GB SD card, then I guess that if I...


```
gpart add -t freebsd-ufs -l gprootfs -b 1M -s 2G da0
```

...then there won't be enough space, right?

So I guess what I am asking is - how do you calculate this stuff so as to use the most space possible on the card?

It would just be easier to add the smaller items first, and let the / slice take up the rest, no?  Especially if you were trying to script it?


----------



## wblock@ (Feb 8, 2013)

The first meg is used for the boot partition and unused space.  So calculate the disk capacity in meg (diskinfo(8) will show it) and subtract one meg.  Actually, I'd subtract two meg, to allow for rounding and leave 1M or less unused at the end of the drive.

The remainder is the size to use for the main partition.

For example: 8G drive is truly 8G, 8192M.  Swap is desired to be 512M, so:

8192-2= 8190
8190-512= 7678

In a shorter form: capacity in M - (swapsize in M + 2M) = main partition size in M:
8192 - (512 + 2) = 7678


```
# gpart add -t freebsd-ufs  -l gprootfs -b1M -s7678M da0
# gpart add -t freebsd-swap -l gpswap -a1M da0
```

The size values can be specified in blocks, K, M, or G.
That -a1M is to make sure the swap partition is an even multiple of 1M.  That will also make it an even multiple of 4K for Advanced Format drives, and does no harm on 512-byte block drives.


----------



## dave (Feb 8, 2013)

Super helpful, thanks!


----------



## dave (Mar 1, 2013)

What I really needed here was a way to find out the true size of my SD card, and diskinfo(8) is what I was looking for.


----------

