Online Resize Filesystem

Published Date: 2019/11/04 by: DaVieS

Online Resize Filesystem

Risky but working solution to shrink filesystem without any bootCD or pendrive under Centos, Fedora, Redhat based system.
Its very useful when you have no option to boot into rescue system.

The idea not mine, but I let it archive here.

 
  1. Ensure the system is in a stable state, stop all services

    systemctl stop httpd
    systemctl stop nfs-server
    # and so on....
    
  2. Unmount all unused filesystems

    umount -a
    

    If you see any on-disk filesystems still mounted, then something is still running that shouldn't be. Check what it is using fuser:

    # if necessary:
    yum install psmisc
    # then:
    fuser -vm 
    systemctl stop 
    umount -a
    # repeat as required...
    
  3. Make the temporary root

    mkdir /tmp/tmproot
    mount -t tmpfs none /tmp/tmproot
    mkdir /tmp/tmproot/{proc,sys,dev,run,usr,var,tmp,oldroot}
    cp -ax /{bin,etc,mnt,sbin,lib,lib64} /tmp/tmproot/
    cp -ax /usr/{bin,sbin,lib,lib64} /tmp/tmproot/usr/
    cp -ax /var/{account,empty,lib,local,lock,nis,opt,preserve,run,spool,tmp,yp} /tmp/tmproot/var/
    

    This creates a very minimal root system, which breaks (among other things) manpage viewing (no /usr/share), user-level customizations (no /root or /home) and so forth. This is intentional, as it constitutes encouragement not to stay in such a jury-rigged root system any longer than necessary.

    At this point you should also ensure that all the necessary software is installed, as it will also assuredly break the package manager. Glance through all the steps, and make sure you have the necessary executables.

  4. Pivot into the root

    mount --make-rprivate / # necessary for pivot_root to work
    pivot_root /tmp/tmproot /tmp/tmproot/oldroot
    for i in dev proc sys run; do mount --move /oldroot/$i /$i; done
    

    systemd causes mounts to allow subtree sharing by default (as with mount --make-shared), and this causes pivot_root to fail. Hence, we turn this off globally with mount --make-rprivate /. System and temporary filesystems are moved wholesale into the new root. This is necessary to make it work at all; the sockets for communication with systemd, among other things, live in /run, and so there's no way to make running processes close it.

  5. Ensure remote access survived the changeover

    systemctl restart sshd
    systemctl status sshd
    

    After restarting sshd, ensure that you can get in, by opening another terminal and connecting to the machine again via ssh. If you can't, fix the problem before moving on.

    Once you've verified you can connect in again, exit the shell you're currently using and reconnect. This allows the remaining forked sshd to exit and ensures the new one isn't holding /oldroot.

  6. Close everything still using the old root

    fuser -vm /oldroot
    

    This will print a list of processes still holding onto the old root directory. On my system, it looked like this:

                 USER        PID ACCESS COMMAND
    /oldroot:    root     kernel mount /oldroot
                 root          1 ...e. systemd
                 root        549 ...e. systemd-journal
                 root        563 ...e. lvmetad
                 root        581 f..e. systemd-udevd
                 root        700 F..e. auditd
                 root        723 ...e. NetworkManager
                 root        727 ...e. irqbalance
                 root        730 F..e. tuned
                 root        736 ...e. smartd
                 root        737 F..e. rsyslogd
                 root        741 ...e. abrtd
                 chrony      742 ...e. chronyd
                 root        743 ...e. abrt-watch-log
                 libstoragemgmt    745 ...e. lsmd
                 root        746 ...e. systemd-logind
                 dbus        747 ...e. dbus-daemon
                 root        753 ..ce. atd
                 root        754 ...e. crond
                 root        770 ...e. agetty
                 polkitd     782 ...e. polkitd
                 root       1682 F.ce. master
                 postfix    1714 ..ce. qmgr
                 postfix   12658 ..ce. pickup
    

    You need to deal with each one of these processes before you can unmount /oldroot. The brute-force approach is simply kill -9 $PID $PID2 $PID3 .... for each, but this can break things. To do it more softly:

    If the root drive you want to resize is an LVM drive, you may also need to restart some other running services, even if they do not show up in the list created by fuser -vm /oldroot. If you find you are unable to resize an LVM drive under Step 7, try systemctl restart systemd-udevd.

    Some processes can't be dealt with via simple systemctl restart. For me these included auditd (which doesn't like to be killed via systemctl, and so just wanted a kill -15). These can be dealt with individually.

    The last process you'll find, usually, is systemd itself. For this, run systemctl daemon-reexec.

    Once you're done, the table should look like this:

                 USER        PID ACCESS COMMAND
    /oldroot:    root     kernel mount /oldroot
    
  7. Unmount the old root

    umount /oldroot
    

    At this point, you can carry out whatever manipulations you require. If its not succeed and you have a console view (KVM) try killall -9 -u root.

  8. Resize the Filesystem

    resize2fs /dev/sda2 40G
    

     

  9. Allign the partition table

    fdisk /dev/sda
    

    The last step is deleting the partition, but remember the start sector, and re-create with new size + 1GB for safety, and if the fdisk asks remove any metadata or signature tell NO.
    Optionally create here a new partition with all available free space.
    Sync by pressing (w) enter then reboot.

Credit and Reference: https://unix.stackexchange.com/questions/226872/how-to-shrink-root-filesystem-without-booting-a-livecd/227318#227318





Secure your webserver

Warning! Seems like webdevelopment went crazy!If you would like to avoid any hacking or destructive operations by attackers see below, you may found this little article interesting.To recover ... Click and Read More!

Windows 10 Upgrade Fails? NX not supported by CPU? Solution!

Nothing new but Windows 7 is end-of-life. Many system administrators or end-users maybe facing a problem that title and image says about.There is a solution for sure but at least 99%. But first ... Click and Read More!

nPulse.net is yet under active development

Happy 2020 to everyone!This year is going to be awesome we already prepared some killing tech stuff for you. - We continuing to improvement modbus-online toolset up to enterprise class ... Click and Read More!

sysAdmin ToolBox v2.0 Released

We are happily announce that our second version of sysAdmin ToolBox for Adnroid mobile phones published into live.The first version of this application has been created at 2017 and we reached ... Click and Read More!

What should I use for virtualization?

We have some recent (very short) comparison with VMWARE, HYPERV, ProxMox, SolusVM, VirtualBoxVirtualization technology is a very powerfull tehcnology and helps IT members running different ... Click and Read More!

Microsoft Surprises Linux Users

Well in the past, if someone tells me Microsoft officially supports running a linux under Windows... well :) Microsoft already changed their mind and Im like it! Started with *Windows 10*, Microsoft ... Click and Read More!

ZFS Filesystem Review

ZFS is a transactional filesystem by Sun Microsystems (ORACLE) This means: data corruption never should be. This can be achieved by: checksuming, duplications, and various raid-levels that built-in ... Click and Read More!

Welcome back BSD

Finally I can introduce nPulse.net is running on FreeBSD again. Two years ago we have forced a little bit into linux I mean Debian. It was awesome, linux-based oses are great and "seems" fast but ... Click and Read More!

Sorry for the Mess and Noise, Just launched a new site.

Yes, we have a new site with most advanced toolset nPulse.net is always tried to be sysadmin friendly, we always sharing tools and our resources, so we had improved all systems we have to enable most ... Click and Read More!

All rights reserved nPulse.net 2009 - 2020
Powered by: MVCP / ASPF / PHP 7.2 / NGINX / FreeBSD