https://www.marcelpost.com/wiki/index.php?title=Resize_Root_Filesystem&feed=atom&action=historyResize Root Filesystem - Revision history2024-03-28T10:35:00ZRevision history for this page on the wikiMediaWiki 1.39.5https://www.marcelpost.com/wiki/index.php?title=Resize_Root_Filesystem&diff=3496&oldid=prevAdmin at 09:21, 20 January 20212021-01-20T09:21:35Z<p></p>
<p><b>New page</b></p><div>This procedure details how to copy the current filesystem to a new disk. This is a preferred scenario when:<br />
<br />
*the current root filesystem is becoming too small<br />
*you wish to shrink the existing root filesystem (think vm's)<br />
<br />
<br />
<br />
Steps to achive the copy are:<br />
<br />
* create or attach a new disk<br />
* create a root (and swap partition) on the disk (fdisk /dev/...)<br />
* format the disk with EXT2<br />
* mount the disk to a directory on the live system<br />
* copy all files to the new disk<br />
* copy the special device files (?)<br />
* chroot into the new disk<br />
* install the bootloader to the MBR of the new disk<br />
<br />
<br />
Copy command:<br />
<br />
<pre><br />
(cd / ; tar -cf - . --one-file-system) | (cd /mnt/ ; tar xvfpB -)<br />
</pre><br />
<br />
<br />
Find out the new disk's UUID:<br />
<br />
<pre><br />
ls -l /dev/disk/by-uuid/<br />
</pre><br />
<br />
<br />
* Update the new UUID in /etc/fstab<br />
<br />
* Update the new UUID in /boot/grub/grub.cfg<br />
<br />
<br />
<br />
<br />
Install bootloader:<br />
<br />
There are (as far as I know of) three ways of installing GRUB onto the new disk.<br />
<br />
1 * After copying all files, remove the old drive and boot off a live Linux CD (e.g. Knoppix) (the idea here is that hd0 is the new drive and issuing the grub-install command will simply install on hd0.<br />
<br />
2 * Swap the drives (master becomes slave, slave becomes master) and in the BIOS designate to boot of the second disk. (again, grub will install on hd0 which is the new disk)<br />
<br />
3 * with the new disk attached as /dev/sdb, instruct GRUB to install there but to refer to it as /dev/sda (or hd0 as we want). (my preference)<br />
<br />
<br />
<br />
<br />
Method 1 and 2<br />
<br />
http://www.linuxjournal.com/content/copy-your-linux-install-different-partition-or-drive<br />
<br />
<br />
Method 3<br />
<br />
(with the new disk (/dev/sdb) still mounted at /mnt/)<br />
<br />
<pre><br />
grub-install -d /mnt /dev/sdb<br />
</pre><br />
<br />
* shutdown<br />
* take out old disk, and replace with the new disk as the primary boot disk<br />
* start the machine<br />
<br />
<br />
<br />
<br />
GRUB v1 only:<br />
<br />
When you're back in the O/S update the menu.lst to make sure the correct boot device (e.g. /dev/sda) is used.<br />
<br />
<pre><br />
grub-install /dev/sda<br />
update-grub<br />
</pre><br />
<br />
* reboot and all should be fine<br />
<br />
<br />
<br />
===Resize Filesystem in an image===<br />
<br />
So now you have a dd image of your disk and found out that the device you wish to restore it to is smaller in size...<br />
<br />
All good, what we do next is to resize the filesystem inside the dd image.<br />
<br />
Just to be sure we understand the terminology:<br />
<br />
* a block device is the physical capacity of a storage device (e.g. an 8GB Flash drive)<br />
<br />
* a partition is a logical storage container that is created on the block device. A block device can hold multiple partitions.<br />
<br />
* a filesystem is a logical arrangement of data that is used by an operating system to save and read files. A partition usually holds only one filesystem.<br />
<br />
<br />
If we want to make a larger partition fit on a smaller partition we need to make sure that the used space inside the larger partition doesn't exceed the size of the smaller partition. Once we know this, the process is relatively simple:<br />
<br />
* attach a loop device to the disk-image (using losetup)<br />
<br />
* run a filesystem-check on the partition we wish to resize (using e2fsck)<br />
<br />
* shrink the filesystem to the desired size (using resize2fs)<br />
<br />
* shrink the partition to a size that is (slightly) larger than the filesystem<br />
<br />
After this, we can just write the disk image to the (smaller) block device without running into disk space issues<br />
<br />
<br />
The Process:<br />
<br />
Attach a loop device to the various partitions inside the disk image:<br />
<pre><br />
losetup --find --partscan imagefile.dd<br />
</pre><br />
<br />
Reveal the partition devices that were assigned:<br />
<pre><br />
root@pi:/data# cat /proc/partitions <br />
major minor #blocks name<br />
<br />
8 0 976762584 sda<br />
8 1 53938206 sda1<br />
8 2 51210240 sda2<br />
8 3 48828416 sda3<br />
8 4 822784000 sda4<br />
11 0 1048575 sr0<br />
7 0 7822336 loop0 <--- imagefile.dd<br />
259 0 42811 loop0p1 <--- partition 1 inside imagefile.dd<br />
259 1 7281664 loop0p2 <--- partition 2 inside imagefile.dd<br />
</pre><br />
<br />
<br />
Run a filesystem check:<br />
<pre><br />
root@pi:/data# e2fsck /dev/loop0p2<br />
e2fsck 1.44.5 (15-Dec-2018)<br />
/dev/loop0p2 contains a file system with errors, check forced.<br />
Pass 1: Checking inodes, blocks, and sizes<br />
Inode 391641, i_blocks is 16, should be 8. Fix<y>? yes<br />
Inode 391642, i_blocks is 16, should be 8. Fix<y>? yes<br />
Pass 2: Checking directory structure<br />
Pass 3: Checking directory connectivity<br />
Pass 4: Checking reference counts<br />
Pass 5: Checking group summary information<br />
Block bitmap differences: +(1393664--1393838) -(1394673--1394890) -(1612165--1612167) -1612891 -1612982 -(1615784--1615789) -1622693 -1622836 -1622855 -(1626336--1626342) -(1638720--1638734) -(1638841--1638851) -(1639248--1639258) -1665037 -1666053 -(1668096--1668097)<br />
Fix<y>? yes<br />
Free blocks count wrong for group #49 (4771, counted=4801).<br />
Fix<y>? yes<br />
Free blocks count wrong for group #50 (14121, counted=14151).<br />
Fix<y>? yes<br />
Free blocks count wrong (576554, counted=598442).<br />
Fix<y>? yes<br />
<br />
/dev/loop0p2: ***** FILE SYSTEM WAS MODIFIED *****<br />
/dev/loop0p2: 262820/441728 files (0.3% non-contiguous), 1221974/1820416 blocks<br />
</pre><br />
(so a few errors there, but you may not see any errors)<br />
<br />
<br />
Shrink the filesystem to the desired size:<br />
<pre><br />
root@pi:/data# resize2fs /dev/loop0p2 7000M<br />
resize2fs 1.44.5 (15-Dec-2018)<br />
Resizing the filesystem on /dev/loop0p2 to 1792000 (4k) blocks.<br />
The filesystem on /dev/loop0p2 is now 1792000 (4k) blocks long.<br />
</pre><br />
<br />
<br />
The filesystem should now be small enough to fit on our target block device. Since we no longer need to access the partitions inside the disk-image we can disconnect the loop devices from the disk image:<br />
<pre><br />
losetup --detach /dev/loop0<br />
</pre><br />
<br />
<br />
<br />
Next, we shrink the partition to a size that is (slightly) larger than the filesystem we just shrunk.<br />
<br />
First, inspect the target block device and determine our start sector:<br />
<pre><br />
root@pi:/data# fdisk -l /dev/sdb<br />
Disk /dev/sdb: 7.5 GiB, 8010072064 bytes, 15644672 sectors<br />
Disk model: STORAGE DEVICE <br />
Units: sectors of 1 * 512 = 512 bytes<br />
Sector size (logical/physical): 512 bytes / 512 bytes<br />
I/O size (minimum/optimal): 512 bytes / 512 bytes<br />
Disklabel type: dos<br />
Disk identifier: 0xed429e5a<br />
<br />
Device Boot Start End Sectors Size Id Type<br />
/dev/sdb1 8192 93813 85622 41.8M c W95 FAT32 (LBA)<br />
<br />
</pre><br />
<br />
Orginally, the start sector of the 2nd partition began at 94208 so we'll leave it at that.<br />
<br />
Then we need to calculate the end sector.<br />
<br />
The resize2fs command used a value of 7000M. In Bytes, this is:<br />
<br />
7,000 * 1024 (k) * 1024 (B) = 7340032000 Bytes<br />
<br />
In 512-Byte blocks, this is: 7340032000 / 512 = 14336000<br />
<br />
So the End-sector where this 7,000MB block needs to end is 94208 + 14336000 = 14430208<br />
<br />
Let's add one more 512Byte block to give the filesystem enough room to fit in: 14430209<br />
<br />
<br />
Then we can simply delete the partition from the block device and re-add it with the smaller size:<br />
<br />
<pre><br />
root@pi:/data# cat /proc/partitions <br />
major minor #blocks name<br />
<br />
8 0 976762584 sda <---- the disk of the system that we're currently working on<br />
8 1 53938206 sda1<br />
8 2 51210240 sda2<br />
8 3 48828416 sda3<br />
8 4 822784000 sda4<br />
11 0 1048575 sr0<br />
7 0 7822336 loop0 <---- the disk-image dd file<br />
259 0 42811 loop0p1<br />
259 1 7281664 loop0p2 <---- the partition containing the filesystem we have just shrunk<br />
8 16 7822336 sdb <---- the target block device (an 8GB Flash drive)<br />
8 17 42811 sdb1 <---- the first partition (we'd like to keep as-is)<br />
root@pi:/data# fdisk /dev/sdb <---- running fdisk on the target block device<br />
<br />
Welcome to fdisk (util-linux 2.33.1).<br />
Changes will remain in memory only, until you decide to write them.<br />
Be careful before using the write command.<br />
<br />
<br />
Command (m for help): n <------ create a new partition (partition 2 was already gone)<br />
Partition type<br />
p primary (1 primary, 0 extended, 3 free)<br />
e extended (container for logical partitions)<br />
Select (default p): p<br />
Partition number (2-4, default 2): 2<br />
First sector (2048-15644671, default 2048): 94208 <----- where the original 2nd partition started<br />
Last sector, +/-sectors or +/-size{K,M,G,T,P} (94208-15644671, default 15644671): 14430209 <---- our calculated end sector<br />
<br />
Created a new partition 2 of type 'Linux' and of size 6.9 GiB.<br />
Partition #2 contains a ext4 signature.<br />
<br />
Do you want to remove the signature? [Y]es/[N]o: y<br />
<br />
The signature will be removed by a write command.<br />
<br />
Command (m for help): w<br />
The partition table has been altered.<br />
Calling ioctl() to re-read partition table.<br />
Syncing disks.<br />
<br />
root@pi:/data# <br />
</pre><br />
<br />
One final check:<br />
<pre><br />
root@pi:/data# fdisk -l /dev/sdb<br />
Disk /dev/sdb: 7.5 GiB, 8010072064 bytes, 15644672 sectors<br />
Disk model: STORAGE DEVICE <br />
Units: sectors of 1 * 512 = 512 bytes<br />
Sector size (logical/physical): 512 bytes / 512 bytes<br />
I/O size (minimum/optimal): 512 bytes / 512 bytes<br />
Disklabel type: dos<br />
Disk identifier: 0xed429e5a<br />
<br />
Device Boot Start End Sectors Size Id Type<br />
/dev/sdb1 8192 93813 85622 41.8M c W95 FAT32 (LBA)<br />
/dev/sdb2 94208 14430209 14336002 6.9G 83 Linux<br />
root@pi:/data# <br />
</pre><br />
<br />
Now we have created a smaller partition on the target block device that is just large enough to hold the smaller filesystem.<br />
<br />
We can now write the disk-image dd file to the block device without running into disk space issues.<br />
<br />
<pre><br />
dd if=diskimage.dd of=/dev/sdb bs=1M<br />
</pre></div>Admin