I only have access to the server via a terminal and I can't use graphical tools such as GParted!
I want to create a new partition from a part of the root (about 768mb) for swap.
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda 20G 1.3G 18G 7% /
udev 10M 0 10M 0% /dev
tmpfs 199M 4.9M 194M 3% /run
tmpfs 100M 12K 100M 1% /run/user
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
3 Answers
You cannot shrink/edit a partition if any of the partition on the storage device is mounted. So in order to unmount and edit the root filesystem, the OS need to be shutdown. Then boot into a live system and edit the partition as described in other answers.
Alternative solution : swap file
As an alternative to creating an entire partition, a swap file offers the ability to vary its size on-the-fly, and is more easily removed altogether. Swap file can be hot plugable. i.e can be added and removed without unmounting/turning off the OS.
Create a 512 MB file called
/swapfile. This will be our swap file.fallocate -l 512M /swapfileOR
dd if=/dev/zero of=/swapfile bs=1M count=512Set the right permissions (because a world-readable swap file is a huge local vulnerability):
chmod 600 /swapfileAfter creating the correctly sized file, format it to swap:
mkswap /swapfileActivate the swap file:
swapon /swapfileEdit
/etc/fstaband add an entry for the swap file:/swapfile none swap defaults 0 0
More details at arch linux wiki.
First of all is important to know that you cannot resize to shrink your root partition if you are using it (This is called online shrinking). You can only grow it online. This is supported by the resize2fs command. I will assume the following:
- You don't want to loose your information on the root partition.
- You don't have physical access to the hard drive in order to use a LiveCD. This can apply to a virtual environment or a remote one. In the case of a virtual one you can still manage to boot from a LiveCD if you set the VM to boot from a LiveCD. This is assumming the VM supports outputting the Desktop GUI from where you would run the Gparted app to resize easily. But since this is less likely I assume you can not.
There are 2 type of partitions that you can resize, the LVM partitions or Logical Volume Manager partitions which support Online resizing (Shrinking/Growing) since the creation of the galaxy and the standard partitions most of us use. Right now the only one that has almost 100% support of complete online resizing (Shrink/Grow) is the btrfs filesystem (Which is still in development). I will explain how to do the normal partitions most of us use in the ext4 filesystem.
Resizing (Growing) the Partition
To grow your partition you can do it with the root mounted. To do this simply do:
sudo resize2fs /dev/sda1
Provided you already have the empty space ready to be merged. Afterwards I recommend rebooting for the changes to take effect correctly. The command above would resize to the maximum permitted. If you wish to resize to a particular size then simply add the size at the end:
sudo resize2fs /dev/sda1 25G
Note that if you want to specify 25.4 GB, you can not use the ".". you would need to go down one unit of measure. In this case from GB to MB, so it would look like this:
sudo resize2fs /dev/sda1 25400M
This way you will have a partition of 25.4G