Меню Рубрики

Linux resize root partition

How to resize ext4 root partition live without umount on Linux

Resizing any live partition without unmout comes with a tremendous risk of loosing data thus it is not recommended. If you a have a sensitive data stored on your system, it is always recommended to take the system down make a backup and resize the partition while it is not mounted.

In the following example we are going to resize a partition of a fresh single partition AWS Linux instance. The current partition size is 7.8GB: However, the disk size reports 20GB: The above partition is mounted as root and thus the partition cannot be unmount: All what needs to be done is take a note of a starting sector of a current partition, which in our case is 4096. Remove the partition and recreate a new, larger partition starting from sector 4096. For this we are simply going to use fdisk command. Let’s start by printing again our current partition table: Once again please take a note of the starting sector 4096. Still in fdisk ‘s interactive mode remove partition:

Источник

How to resize a Root Partition in Linux

Resizing a root partition is tricky. In Linux, there isn’t a way to actually resize an existing partition. One should delete the partition and re-create a new partition again with the required size in the same position.

I have come across this process when I had to use a custom Debian image and boot up a VM on Azure. The size of the image is around 3GB and the size of the root device (/dev/sda) would be configured to be about 10GB. I preferred extending the existing partition to make use of 10GB on the root device.

Resizing a Linux Root File System partition involves three main steps

Recreating the Partition with the desired size:

Understanding the fdisk Commands: The below snippet is the output of the help text that fdisk prints out when m is typed in the prompt. It also includes the mapping of the steps with the commands to be used.

The process involves these 5 steps as seen below

  • Print the existing partition table ( Make a note of boot bit and the sector number of the start column)
  • Delete the existing partition
  • Create a new partition with the same first sector number
  • Specify the last sector if needed. It defaults to creating the partition till the end. If you only want to have a single partition on the disk, choosing the default option is the best idea
  • Print the newly created partition table
  • Write the partition table

The Kernel should be informed of the updated Partition table:

After writing the partition table there would be a warning, which mentions that the kernel is still using the old partition table. To update the kernel with the new partition table, we could either use partprobe or kpartx. I have personally used partprobe and prefer that. It can be installed using the below command.

After installing run partprobe on the command line. partprobe utility is used to make sure the kernel is aware of the new partition. If everything about re-creating the partition is correct, it shouldn’t error out anything. If the below error occurs, it’s possible that you could have specified the wrong start sector, and the next time the system boots, it will not boot correctly since the start sector is not correct. partprobe doesn’t give a constructive message in this situation, but when you get an error like below, it’s worth checking if the start sector is the same as the previously existing partition that was deleted.

Resizing the File System

After the partprobe run is successful, the final step is to resize the root file system. This is the step to write in the file system to the newly added blocks into the partition. You can first start with checking the filesystem on the partition using the e2fsck command and then resize it.

These steps would let you resize the root file system as per your requirement without requiring to reboot the system.

Источник

How can I resize an ext root partition at runtime?

How can I increase the size of the root partition of a system at runtime?

I have a partition that is not allocated after the root partition (which is also ext4), how can I add that unallocated space to the space allocated to the root partition without having to shutdown the server?

10 Answers 10

GUI (Ubuntu 14.04 and later): GParted v0.17 and later provide a nice GUI for this. (Older versions will refuse to resize a mounted partition).

Command line (any Ubuntu version): There are three steps to this.

Step 1. The partition must first be resized. If you’re using LVM, it’s easy, and you presumably know how to proceed. If you’re using classic partitions, it’s a bit more complicated, and may require a reboot (though you never have to boot another system or live CD).

This is how I do it: Use fdisk to first delete the partition (the idea is that the data on disk will be preserved), then carefully recreate it with a larger size at the same position.

Again, it is critical that the new partition starts at the same block as the old. The Id should also match (83 for Linux systems). Be prepared to lose all your data at the slightest typo.

To be on the safe side, you may also restore the boot flag (which according to Wikipedia is still required on some computers) by pressing a .

See the comment section for what to do if your swap partition is in the way.

By now it should be apparent why people recommend using a live CD. 😉

Step 2. As fdisk helpfully reminds you, you must reload the partition table before proceeding. The safest way is to simply reboot; but you can also use partprobe or kpartx (more information).

Step 3. Once the partition is resized and the partition table reloaded, it’s a simple matter of running resize2fs on the file system, and you can do this even when it’s mounted as the root partition.

Источник

Increase the Size of a Linux Root Partition Without Rebooting

Table of Contents

Introduction

A typical Linux server deployed from a ProfitBricks supplied image has a single storage volume, /dev/vda . If we take a look at that using fdisk we will see that the disk is divided into two partitions. The first one, /dev/vda1 , is the boot partition where the OS resides. The second partition, /dev/vda2 , is configured as swap space.

In the example above, the total 50 GB storage volume is split into 46 GB usable space and 4 GB for swap. What if after provisioning we find that 46 GB of disk space is not enough but we prefer not to add an additional storage volume? The goal is to increase the amount of disk space available on the root filesystem. This can be accomplished with minimal disruption. We do not need to reboot the server!

Requirements

  • A Linux Server created from a ProfitBricks supplied image.
  • SSH or console access (via the DCD) to the Linux Server.
  • The partprobe command. Can be installed from the parted package on most Linux distributions.
  • The fdisk , swapoff , mkswap , swapon , and resize2fs commands. Likely available by default.
  • A current snapshot or other backup of the system you are working on. Just to be safe!

Provision Additional Space

To increase the size of the hard drive, go into the DCD (Data Center Designer) and upscale the drive.

After you have increased the amount of drive space, click on «provisioning» to process the change. Takes around a minute and your hard drive is bigger.

We then need to switch off the swap — so make sure you can live a few minutes without swap space:

Once swap has been disabled, we need to reconfigure the partitions using fdisk :

We will delete the two existing partitions first. We run fdisk /dev/vda and then use the d command to delete partition 2, and then delete partition 1. Follow the example below:

Now we recreate our partitions. In the example we have 4 GB of swap space. So we need to keep at least that much space available for the new swap partition.

Recreate /dev/vda1 first. Press n to create a new partition. Enter p to create a primary partition. We can press Enter to accept the default value of 2048 for the first sector. Then enter a size for the partition. You can enter a value in GB, so if we are increasing the disk to 100 GB, we subtract our 4 GB for swap, and enter +96G for 96 GB.

Now we recreate the swap partition following a simalar process. Press n and then p to create a new primary partition. Press Enter to accept the default value for «First sector». We can also press Enter again to accept the default value for «Last sector».

Since this second partition is going to be used for swap space, we need to change the partition type. This can be done by pressing t at the fdisk prompt. We then press 2 to select the second partition. If you want to see the list of available partition types, press L, otherwise enter 82 to select «Linux swap / Solaris».

fdisk helpfully informs us that we have changed the partition type with the message:

After that, we save using the w command and are returned to a shell prompt:

You may get a message like this before the shell prompt:

Lets forgo rebooting and instead tell the kernel about the new partitions using partprobe :

We should be returned to the shell prompt with no output from partprobe .

Now we need to resize our filesystem on /dev/vda1 :

The filesystem on /dev/vda1 is now 25165824 (4k) blocks long.

Initialize the new swap location of /dev/vda2 :

Finally we edit /etc/fstab and replace the old UUID with the new one returned in the output of the mkswap command. The line to change has no value for «mount point» and has «type» set to swap.

After editing /etc/fstab , we need to enable swap again:

Verify

We can verify the new larger disk size by utilizing df and/or fdisk .

We can also confirm that the server was not rebooted by looking at the output of uptime :

Summary

We have successfully increased the amount of disk space available on our Linux server. If you have any questions or comments, please leave them here, or in the DevOps Community section of this site.

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

  • Shutter counter mac os
  • Shutter count viewer mac os
  • Shrew vpn client mac os
  • Showexif для mac os
  • Show hidden files finder mac os