# Move Swap to increase UFS



## MariusPeschke (Feb 11, 2014)

Hi,

```
gpart show
=>      34  41942973  ada0  GPT  (20G)
        34       128     1  freebsd-boot  (64k)
       162  19920768     2  freebsd-ufs  (9.5G)
  19920930   1048576     3  freebsd-swap  (512M)
  20969506  20973501        - free -  (10G)
```
I recently increased the virtual disc size of my VM, because I had not enough space to compile a new kernel and install it. Now I can't increase the the size of slice 2 because the is no more available. Since there is no GUI-Based partition tool for FreeBSD :-( I have to use the shell. And this is the part where I need your help:
How do I move swap so that I can increase freebsd-ufs?
bsdlabel does not seem to work:

```
bsdlabel -e ada0
bsdlabel: /dev/ada0: no valid label found
```
Do I still need to use 
	
	



```
fdisk -u
```
since my gpart shows 10G free?

```
fdisk -s
/dev/ada0: 41610 cyl 16 hd 63 sec
Part        Start        Size Type Flags
   1:           1    41943039 0xee 0x80
```

growfs() needs a bigger space, since after freebsd-ufs is no more space left.

```
growfs -s 19G ada0p2 
growfs: requested size 19GB is larger than the available 9.5GB
```
I hope I have given enough information, so that I can be guided through this problem.
Thanks in advance
Sincerly Yours

Marius


----------



## wblock@ (Feb 11, 2014)

There is a new section in the Handbook just for this issue: Resizing and Growing Disks.

Use gpart(8), avoid the old fdisk(8) and bsdlabel(8) commands.


----------



## SirDice (Feb 11, 2014)

Stop using bsdlabel(8) and fdisk(8), they're both for MBR and you have GPT. You should use gpart(8), it can handle both MBR and GPT. 

This is not tested, no guarantees, make backups, danger of destroying everything, etc!! 

First boot from an install CD or memory stick, mfsBSD or something else. Use gpart(8) to remove the freebsd-swap partition. Use `gpart resize` to enlarge the freebsd-ufs partition. Create a new freebsd-swap partition at the end (leave some room when resizing!) with `gpart add -t freebsd-swap -s 512M ada0`. Boot the machine and use growfs(8) to expand the UFS file system.

Edit: Brilliant, I must keep track of the handbook.


----------



## asteriskRoss (Feb 11, 2014)

Since you are using a GPT partition scheme, the tools for this job are gpart(8) and growfs(8).  As we will be altering the system partition, I recommend booting from a live CD, though I have also provided more dangerous alternative if you're feeling brave/foolhardy §e

*Create a new swap partition at the end of disk*
First, let's add a new swap partition at the end of the drive.  We can calculate where this should start, though we'll need to know the sector size of your disk, since gpart gpart(8) deals with sizes in logical blocks.  Your output of `# gpart show ada0` indicates your disk has a sector size of 512 bytes; partition 1 is 64k but has a size of 128 blocks; 0.5k/block, equivalent to 512 bytes/block.  If you didn't know this, you could run `# gpart list ada0` and examine the "Sectorsize". *

So, you want a new 512MB swap partition.  We actually already know the size, since your existing one shows as 1,048,576 blocks.  If you didn't already have a reference, you could calculate it from number of bytes in a megabyte (1024 * 1024), multiplied by the  number of MB (512), divided by the number of bytes per block (512).

The first line of the `# gpart show` output shows the number of blocks available for partitions (41,942,973) beginning at block 34.  The final block number of the disk is the sum of these numbers; 41,943,007.  We therefore want to start our new partition at block 40,894,431 (final block number minus number of blocks needed).  We can do this with the "-b" parameter: `# gpart add -t freebsd-swap -b 40894431 -s 512M`

You might want to satisfy yourself that the new partition is where it should be by running `# gpart show ada0`.

*Changing over the swap partition*
If you're booted into your system (rather than using a live CD) we need to start using the new swap partition, and stop using your old swap partition (note that the call to swapoff(2) could fail if your system started using more than 512MB of swap space between commands):

```
# swapon /dev/ada0p4
# swapoff /dev/ada0p3
```

We need to change your /etc/fstab so the new one is used automatically at the next reboot.  Mount your system partition if you're using a live CD (`# mount /dev/ada0p2 /mnt)`.  Open /mnt/etc/fstab in your favourite editor and change the line relating to /dev/ada0p3 to /dev/ada0p4.  Save it and if you're using a live CD, unmount your system partition (`# umount /dev/ada0p2`).

*Remove the old swap partition*
It is now safe to remove the old swap partition, since it isn't being used.  Use gpart(8), specifying the index of the partition we want to delete: `# gpart delete -i 3 ada0`

*Increase the size of the system partition*
There is now space for the system partition to grow and gpart(8) will even do the calculation for us, so no need to bring your calculator out again     However, messing with a system partition that is in use probably isn't a good idea.  I recommend that (if you haven't already) you reboot using a live CD then run: `# gpart resize -i 2 ada0`

The more dangerous alternative would be to drop in to single user mode (`# shutdown now`), remount the system partition read-only (`# mount -u -o ro /dev/ada0p2`) and "allow foot shooting" (`# sysctl kern.geom.debugflags=0x10`, see the geom(4) man page) before running the command.

Check everything is as you want it by running `# gpart show ada0`.

*Grow the system partition's file system*
Having resized the partition, we need to tell the file system to grow into it with a straightforward command: `# growfs /dev/ada0p2`

*Have a beer*
We're done!  Remove the live CD, reboot into your more roomy system (or if you chose the dangerous option, disallow foot shooting with `# sysctl kern.geom.debugflags=0` and `# exit` to multi-user) and enjoy a beer  :beergrin  


Ad(*): Note that many "Advanced Format" disks which internally use a 4k sector size actually choose to report a 512 byte sector size; this isn't important for our calculations but does matter for efficient partition alignment (beyond the scope of this answer)

Update: Gah, @wblock@ and @SirDice got in first (note to self: browser refresh) and I hadn't checked the handbook for updates either  :\  An excellent addition though


----------



## MariusPeschke (Feb 12, 2014)

Hi,
I am using a 10.0 live CD.
`gpart show`
showed me two drives:
1. my "hdd" with name ada0.
2. my "hdd" with name diskid/DISK-VB5d******-******** with identical values. Is this normal while on a live CD?

Then I started:
At first I had problems using `swapoff`.  It gave me "invalid argument" when i typed:`swapoff /dev/ada0p3`.  So I used `swapoff -a`.  Then I used `gpart delete -i 3 ada0` which worked.

Next I wanted to resize the partition: `gpart resize -i 2 -a 4k -s 19.5G ada0` where it responded gpart: Invalid size param: invalid argument.
When I changed my cmd to `gpart resize -i 2 -a 4k -s 20G ada0` I got gpart size '41943038': Invalid argument.
Now i will try creating the swap at the end first and then the resize without a sizeparam (which to my knowledge should fill the remaining space.
Greets

Edit: creating the swap first worked for me, the `swapon` also worked and the `gpart resize` as well.

```
34 41942973 ada0 GPT (20G)
34 128 1 freebsd-boot 64K
162 40894269 2 freebsd-ufs 19G
40894431 1048576 3 freebsd-swap 512M
```

Thanks @wblock@ for showing me the manual entry.
Thanks @SirDice & @wblock@ for noting the obsoleteness of `fdisk` and `bsdlabel`.
And many thanks to @asteriskRoss for the fantastic guide, it worked well.  Tou gave a lot of effort guiding and explaining the steps. <3

Final questions: do i still need to edit /etc/fstab ?
And do I really need to use `growfs` on my running system since I am running 9.2? "Growing a live UFS file system is only possible in FreeBSD 10.0-RELEASE" so do I need to do it on a Live CD?
Greets


----------



## SirDice (Feb 13, 2014)

MariusPeschke said:
			
		

> Final questions: do I still need to edit /etc/fstab ?


That shouldn't be needed, the 'index' numbers are still the same so it still refers to ada0p2 and ada0p3.



> And do I really need to use `growfs` on my running system since I am running 9.2? "Growing a live UFS file system is only possible in FreeBSD 10.0-RELEASE" so do I need to do it on a Live CD?
> Greets


I had never done it on a running system before, and you're right. You have to use growfs(8) from the Live CD.


----------



## asteriskRoss (Feb 13, 2014)

@@SirDice I tested the instructions I gave for live resize with the "allow foot shooting" kern.geom.debugflags setting on a virtual machine running 9.2-RELEASE and growfs(8) worked fine resizing the mounted file system (albeit in single user and mounted read-only; my reasoning was that if you're going to use a risky solution, you're best minimising that risk).

@@MariusPeschke My instructions were intended to be the safest they could if you chose the option without using a live CD and your system was already using some virtual memory.  Echoing @SirDice, you won't need to edit /etc/fstab if you chose to do it a bit differently and removed the existing swap partition before adding the new one.  In that case, you will have deleted /dev/ada0p3 but when you added another one, that will have been allocated /dev/ada0p3 too as gpart(8) chooses the next available number by default when adding a partition.


----------



## wwingate (Apr 27, 2014)

Hi.  I hope someone gets this.  I'm new in here..I think this is the right place to post.

I have successfully used `gpart` to increase the size of my file partition.  Now, I need to use `growfs` to increase the size of the file system.  In order to grow the root file system the it needs to be un-mounted.  How do I boot into FreeBSD with the file system un-mounted so that I can increase the size of the root file system?

I am using FreeBSD 9.2.


----------



## wblock@ (Apr 27, 2014)

Boot from an install CD and select Shell.


----------



## proton1234 (Jul 3, 2021)

SirDice said:


> Stop using bsdlabel(8) and fdisk(8), they're both for MBR and you have GPT. You should use gpart(8), it can handle both MBR and GPT.
> 
> This is not tested, no guarantees, make backups, danger of destroying everything, etc!!
> 
> ...


And if it's a VPS so it's impossible to boot from CD/USB what would be the best tactics? I tried to boot to a single user mode, partition which I need to expand is #6 and it's last on the disk and it's unmounted by default in single user mode.  When I try to perform `gpart resize -i 6 -s 10G -a 4k vtbd0` it throws some errors but say at the end "partition resized". Sending`growfs /dev/vtbd0p6` will grow partition but also with some errors.

But if I boot in a multiuser mode (partition #6 is mounted) and issue the same commands there is no errors. I even not need to send `sysctl kern.geom.debugflags=16` as it was told in handbook  `gpart show` and `df -h` show correct sizes etc.


----------



## grahamperrin@ (Jul 10, 2021)

proton1234 said:


> … When I try to perform `gpart resize -i 6 -s 10G -a 4k vtbd0` it throws some errors …



What errors, exactly? 

(I do see errors, with proper use of gpart(8), in at least one situation.)


----------

