Ubuntu: Modify HDD partitions while using them [duplicate]


This question already has an answer here:

In gparted, why can’t I modify partitions that are in use, like root, but in Windows I can shrink the root partition while booted into it.

Is there any program I can use to modify a partition while it’s in use?


GParted is pretty conservative about this, but it can be done with other tools; however, there are significant differences depending on the partitioning method (conventional partitions vs. LVM) and the filesystem in use. For instance:

  • You must distinguish between resizing partitions (which are simple data structures that identify the start and end points of filesystems or other data structures) and filesystems (which are much more complex data structures to help identify files, directories, and associated metadata). GParted presents one user interface for performing both actions, thus masking this important distinction.
  • When resizing a conventional partition that's used "raw," you need to either resize, or delete and re-create, the partition you want to change. You do this before increasing the size of a filesystem from its end or after shrinking the filesystem if you want to decrease the size of a partition. Because of the way the Linux kernel handles these things, it may be difficult to get the kernel to recognize the changed partition size of a partition with an active filesystem, so you may need to reboot after making a change to the size of a partition.
  • Moving a partition or resizing it by changing its start point requires re-writing significant amounts of the filesystem it contains. This is because filesystems are defined, in part, by data structures that are located relative to the start of the partition (or other container). GParted handles this more-or-less transparently, but if you want to do it with other tools, you'll need to be very careful to move the filesystem data structures and to get the resized partition to start at exactly the right point. This is a task for experts with nerves of steel; do not attempt such things unless you know what you're doing! Even an expert would do this only after making a backup.
  • Another data structure type is Logical Volume Manager (LVM), which is an alternative way to identify filesystems. LVM is typically layered atop ordinary partitions -- that is, you designate one or more partitions as physical volumes (PVs) in an LVM volume group (VG), then subdivide that VG into logical volumes (LVs), which are what hold filesystems. Despite the added complexity, LVM makes it easier to resize filesystems because LVs are more like files -- you don't need to be concerned with where they begin or end; you just need to know the LV's name and size.
  • If you use LVM, logical volumes (LVs) hold filesystems, and so you're normally concerned with resizing LVs, not partitions. Resizing LVs while they're in use is easy, and is done via lvresize; but the filesystems they contain may still need to be resized separately. (Sometimes the --resizefs option to lvresize will help with this task, though.)
  • If you want to increase the total space used by LVM, you can add a new partition as a PV and then resize LV(s) within the VG. If your LVM setup contains multiple PVs, you can reverse this process (although you may need to explicitly clear out one or more PVs with pvmove followed by vgreduce). If you've got one big PV, you'll need to resize it with pvresize and then adjust the containing partition's size with fdisk, parted, gdisk, or some other tool.
  • Different partitions support different methods of resizing. For the popular ext4fs (and its predecessors, ext3fs and ext2fs), you can use resize2fs to do this job. This tool supports increasing or decreasing the size of unmounted filesystems; but for mounted filesystems, the size can be increased but not decreased. For other filesystems, you must consult the filesystem's own sizing tools.
  • GUI tools like system-config-lvm can help with LV resizing when using LVM.

I don't use the Windows tools very often; however, my vague recollection is that, when you resize an in-use Windows partition, its tools require a reboot as part of the operation. If this recollection is correct, then the Linux and Windows tools actually aren't all that different from a low-level point of view; it's just that Windows helps automate the different steps involved, whereas you must explicitly do the different things yourself under Linux. In some cases, such as when increasing the size of an LV, Linux may actually be more flexible than Windows.

The why of it has to do with the way the data structures are created and otherwise manipulated by the kernel; the kernel tends to assume that partitions that are in use won't change in size. That said, I'm not familiar with the internal kernel details, just some of the system calls that partitioning tools can use to tell the kernel to update its partition-table data. There have also been changes to how the kernel handles this from time to time; but such changes require changes to userspace tools that may take a while to propagate. GParted is itself built on numerous other levels (most notably libparted, but also GUI libraries, the X Window System, etc.), so it can be slow to catch up to new features at the kernel level.

There are numerous questions and answers on this site that cover numerous specific resizing scenarios. Covering them all in a single answer would be tedious and impractical.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »