Linux resize root partition ext4

Steps

Before we can resize the root file system, we need the OS to detect the new size of the disk.

In this case, we use the fdisk command to show the current disk size. Which in this case is 120GB. (I know a 120GB root file system is not a good idea, but this is a lab.)

[email protected]:~# fdisk -l /dev/sda Disk /dev/sda: 120 GiB, 128849018880 bytes, 251658240 sectors Disk model: Virtual disk Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: gpt Disk identifier: DDC91865-B329-443D-AFFF-0898C341D65C  Device Start End Sectors Size Type /dev/sda1 2048 4095 2048 1M BIOS boot /dev/sda2 4096 4198399 4194304 2G Linux filesystem /dev/sda3 4198400 251656191 247457792 118G Linux filesystem [email protected]:~# 

We now need to tell the OS to scan all the disks for changes. We do this by running a script I created called DiskScan. This script walks through all the disks and looks for changes.

curl https://raw.githubusercontent.com/mattmattox/DiskScan/master/rescan_disks.sh | bash 
[email protected]:~# curl https://raw.githubusercontent.com/mattmattox/DiskScan/master/rescan_disks.sh | bash  % Total % Received % Xferd Average Speed Time Time Time Current  Dload Upload Total Spent Left Speed 100 363 100 363 0 0 1553 0 --:--:-- --:--:-- --:--:-- 1551 Scan for new disk(s). host0 host1 host10 host11 host12 host13 host14 host15 host16 host17 host18 host19 host2 host20 host21 host22 host23 host24 host25 host26 host27 host28 host29 host3 host30 host31 host32 host4 host5 host6 host7 host8 host9  Scan for disk size change. sda [email protected]:~# 

We can now see that the disk size has changed. Note, we can ignore the GPT error as we’ll be fixing it as part of the next step.

[email protected]:~# fdisk -l /dev/sda GPT PMBR size mismatch (251658239 != 419430399) will be corrected by write. The backup GPT table is not on the end of the device. Disk /dev/sda: 200 GiB, 214748364800 bytes, 419430400 sectors Disk model: Virtual disk Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: gpt Disk identifier: DDC91865-B329-443D-AFFF-0898C341D65C  Device Start End Sectors Size Type /dev/sda1 2048 4095 2048 1M BIOS boot /dev/sda2 4096 4198399 4194304 2G Linux filesystem /dev/sda3 4198400 251656191 247457792 118G Linux filesystem [email protected]:~# 

Resize the root partition

At this point, the disk has the available space we need. We can now resize the root partition.

[email protected]:~# parted /dev/sda GNU Parted 3.4 Using /dev/sda Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) p Warning: Not all of the space available to /dev/sda appears to be used, you can fix the GPT to use all of the space (an extra 167772160 blocks) or continue with the current setting? Fix/Ignore? Fix Model: VMware Virtual disk (scsi) Disk /dev/sda: 215GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags:  Number Start End Size File system Name Flags  1 1049kB 2097kB 1049kB bios_grub  2 2097kB 2150MB 2147MB ext4  3 2150MB 129GB 127GB  (parted) resizepart 3 -1 (parted) p Model: VMware Virtual disk (scsi) Disk /dev/sda: 215GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags:  Number Start End Size File system Name Flags  1 1049kB 2097kB 1049kB bios_grub  2 2097kB 2150MB 2147MB ext4  3 2150MB 215GB 213GB  (parted) q Information: You may need to update /etc/fstab.  [email protected]:~# 

Note: The -1 is the size of the partition. This tell parted to use the remaining space on the disk.

Читайте также:  Linux завершить процесс pid

Resize the file system

At this point, we have expanded the root partition to the new size. We can now resize the file system.

[email protected]:~# resize2fs /dev/sda3 resize2fs 1.46.5 (30-Dec-2021) Filesystem at /dev/sda3 is mounted on /; on-line resizing required old_desc_blocks = 2, new_desc_blocks = 13 The filesystem on /dev/sda3 is now 51428620 (4k) blocks long. 

Force a filesystem check on next reboot

At this point, we have expanded the file system to the new size. We can now force a filesystem check on the next reboot.

Note: This step is optional but is recommended.

Источник

How to resize ext4 root partition live without umount on Linux

This article will focus on how to resize EXT4 root partition without unmount. This is an easy way for some system where you are unable to unmount root partition and the system can be recovered easily if something goes wrong like for example AWS instance.

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:

# df -h . Filesystem Size Used Avail Use% Mounted on /dev/xvda1 7.8G 642M 6.8G 9% /

However, the disk size reports 20GB:

# fdisk -l Disk /dev/xvda: 20 GiB, 21474836480 bytes, 41943040 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0xd7f2e0e8 Device Boot Start End Sectors Size Id Type /dev/xvda1 * 4096 16773119 16769024 8G 83 Linux

The above partition is mounted as root and thus the partition cannot be unmount:

# umount / umount: /: target is busy. (In some cases useful info about processes that use the device is found by lsof(8) or fuser(1))

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:

# fdisk /dev/xvda Welcome to fdisk (util-linux 2.25.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): p Disk /dev/xvda: 20 GiB, 21474836480 bytes, 41943040 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0xd7f2e0e8 Device Boot Start End Sectors Size Id Type /dev/xvda1 * 4096 16773119 16769024 8G 83 Linux

Once again please take a note of the starting sector 4096. Still in fdisk ‘s interactive mode remove partition:

Command (m for help): d Selected partition 1 Partition 1 has been deleted.

Next, create a new partition right on the top of the previous and ensure that you use same starting sector:

Command (m for help): n Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): p Partition number (1-4, default 1): First sector (2048-41943039, default 2048): 4096 Last sector, +sectors or +size (4096-41943039, default 41943039): Created a new partition 1 of type 'Linux' and of size 20 GiB.

Make the partition 1 bootable and print new partition table:

Command (m for help): a Selected partition 1 The bootable flag on partition 1 is enabled now. Command (m for help): p Disk /dev/xvda: 20 GiB, 21474836480 bytes, 41943040 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0xd7f2e0e8 Device Boot Start End Sectors Size Id Type /dev/xvda1 * 4096 41943039 41938944 20G 83 Linux

Confirm all new details and write new partition table:

Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Re-reading the partition table failed.: Device or resource busy The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).

At this point the system needs to be rebooted in order to remount our root partition with a new size. Force fsck on next reboot to ensure that the partition is checked before it is mounted. To do so just create an empty file called forcefsck in the root of your / partition:

Читайте также:  Kali linux смена графической оболочки

Reboot your system. Once the system is up again check the partition size:

df -h . Filesystem Size Used Avail Use% Mounted on /dev/xvda1 20G 644M 19G 4% /

Comments and Discussions

NEWSLETTER

Subscribe to Linux Career Newsletter to receive latest news, jobs, career advice and featured configuration tutorials.

WRITE FOR US

LinuxConfig is looking for a technical writer(s) geared towards GNU/Linux and FLOSS technologies. Your articles will feature various GNU/Linux configuration tutorials and FLOSS technologies used in combination with GNU/Linux operating system.

When writing your articles you will be expected to be able to keep up with a technological advancement regarding the above mentioned technical area of expertise. You will work independently and be able to produce at minimum 2 technical articles a month.

TAGS

  • VIM tutorial for beginners
  • How to install the NVIDIA drivers on Ubuntu 20.04 Focal Fossa Linux
  • Bash Scripting Tutorial for Beginners
  • How to check CentOS version
  • How to find my IP address on Ubuntu 20.04 Focal Fossa Linux
  • Ubuntu 20.04 Remote Desktop Access from Windows 10
  • Howto mount USB drive in Linux
  • How to install missing ifconfig command on Debian Linux
  • AMD Radeon Ubuntu 20.04 Driver Installation
  • Ubuntu Static IP configuration
  • How to use bash array in a shell script
  • Linux IP forwarding – How to Disable/Enable
  • How to install Tweak Tool on Ubuntu 20.04 LTS Focal Fossa Linux
  • How to enable/disable firewall on Ubuntu 18.04 Bionic Beaver Linux
  • Netplan static IP on Ubuntu configuration
  • How to change from default to alternative Python version on Debian Linux
  • Set Kali root password and enable root login
  • How to Install Adobe Acrobat Reader on Ubuntu 20.04 Focal Fossa Linux
  • How to install the NVIDIA drivers on Ubuntu 18.04 Bionic Beaver Linux
  • How to check NVIDIA driver version on your Linux system
  • Nvidia RTX 3080 Ethereum Hashrate and Mining Overclock settings on HiveOS Linux
Читайте также:  Есть ли тех поддержка linux

LATEST TUTORIALS

  • Easy Steps to Update Your Raspberry Pi
  • Connecting Your Raspberry Pi to Wi-Fi: A How-To
  • How to install RealVNC viewer on Linux
  • How to check Raspberry Pi RAM size and usage
  • How to check Raspberry Pi model
  • Understanding UEFI and BIOS in Relation to Linux Nvidia Driver Installation
  • How to orchestrate Borg backups with Borgmatic
  • How to monitor filesystem events on files and directories on Linux
  • Debian USB Firmware Loader Script
  • How to install and self host an Ntfy server on Linux
  • How to backup your git repositories with gickup
  • How to bind an SSH public key to a specific command
  • Creating a Bootable USB for Windows 10 and 11 on Linux
  • How to list all displays on Linux
  • List of QR code generators on Linux
  • How to extract text from image
  • Linux EOF explained
  • How to use xclip on Linux
  • List of window managers on Linux
  • What is zombie process on Linux

Источник

Оцените статью
Adblock
detector