Difference between revisions of "Resize Root Filesystem"

From wikipost
Jump to navigationJump to search
Line 122: Line 122:
 
Reveal the partition devices that were assigned:
 
Reveal the partition devices that were assigned:
 
<pre>
 
<pre>
 +
root@pi:/data# cat /proc/partitions
 +
major minor  #blocks  name
  
 +
  8        0  976762584 sda
 +
  8        1  53938206 sda1
 +
  8        2  51210240 sda2
 +
  8        3  48828416 sda3
 +
  8        4  822784000 sda4
 +
  11        0    1048575 sr0
 +
  7        0    7822336 loop0    <--- imagefile.dd
 +
259        0      42811 loop0p1  <--- partition 1 inside imagefile.dd
 +
259        1    7281664 loop0p2  <--- partition 2 inside imagefile.dd
 +
</pre>
 +
 +
 +
Run a filesystem check:
 +
<pre>
 +
root@pi:/data# e2fsck /dev/loop0p2
 +
e2fsck 1.44.5 (15-Dec-2018)
 +
/dev/loop0p2 contains a file system with errors, check forced.
 +
Pass 1: Checking inodes, blocks, and sizes
 +
Inode 391641, i_blocks is 16, should be 8.  Fix<y>? yes
 +
Inode 391642, i_blocks is 16, should be 8.  Fix<y>? yes
 +
Pass 2: Checking directory structure
 +
Pass 3: Checking directory connectivity
 +
Pass 4: Checking reference counts
 +
Pass 5: Checking group summary information
 +
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)
 +
Fix<y>? yes
 +
Free blocks count wrong for group #49 (4771, counted=4801).
 +
Fix<y>? yes
 +
Free blocks count wrong for group #50 (14121, counted=14151).
 +
Fix<y>? yes
 +
Free blocks count wrong (576554, counted=598442).
 +
Fix<y>? yes
  
 +
/dev/loop0p2: ***** FILE SYSTEM WAS MODIFIED *****
 +
/dev/loop0p2: 262820/441728 files (0.3% non-contiguous), 1221974/1820416 blocks
 
</pre>
 
</pre>
 +
(so a few errors there, but you may not see any errors)
 +
 +
 +
Shrink the filesystem to the desired size:
 +
<pre>
 +
root@chew:/data# resize2fs /dev/loop0p2 7000M
 +
resize2fs 1.44.5 (15-Dec-2018)
 +
Resizing the filesystem on /dev/loop0p2 to 1792000 (4k) blocks.
 +
The filesystem on /dev/loop0p2 is now 1792000 (4k) blocks long.
 +
</pre>
 +
 +
 +
 +
Shrink the partition to a size that is (slightly) larger than the filesystem
 +
<pre>
 +
 +
</pre>
 +
 +
 +
After this, we can just write the disk image to the (smaller) block device without running into disk space issues

Revision as of 18:24, 20 January 2021

This procedure details how to copy the current filesystem to a new disk. This is a preferred scenario when:

  • the current root filesystem is becoming too small
  • you wish to shrink the existing root filesystem (think vm's)


Steps to achive the copy are:

  • create or attach a new disk
  • create a root (and swap partition) on the disk (fdisk /dev/...)
  • format the disk with EXT2
  • mount the disk to a directory on the live system
  • copy all files to the new disk
  • copy the special device files (?)
  • chroot into the new disk
  • install the bootloader to the MBR of the new disk


Copy command:

(cd / ; tar -cf - . --one-file-system) | (cd /mnt/ ; tar xvfpB -)


Find out the new disk's UUID:

ls -l /dev/disk/by-uuid/


  • Update the new UUID in /etc/fstab
  • Update the new UUID in /boot/grub/grub.cfg



Install bootloader:

There are (as far as I know of) three ways of installing GRUB onto the new disk.

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.

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)

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)



Method 1 and 2

http://www.linuxjournal.com/content/copy-your-linux-install-different-partition-or-drive


Method 3

(with the new disk (/dev/sdb) still mounted at /mnt/)

grub-install -d /mnt /dev/sdb
  • shutdown
  • take out old disk, and replace with the new disk as the primary boot disk
  • start the machine



GRUB v1 only:

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.

grub-install /dev/sda
update-grub
  • reboot and all should be fine


Resize Filesystem in an image

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...

All good, what we do next is to resize the filesystem inside the dd image.

Just to be sure we understand the terminology:

  • a block device is the physical capacity of a storage device (e.g. an 8GB Flash drive)
  • a partition is a logical storage container that is created on the block device. A block device can hold multiple partitions.
  • 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.


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:

  • attach a loop device to the disk-image (using losetup)
  • run a filesystem-check on the partition we wish to resize (using e2fsck)
  • shrink the filesystem to the desired size (using resize2fs)
  • shrink the partition to a size that is (slightly) larger than the filesystem

After this, we can just write the disk image to the (smaller) block device without running into disk space issues


The Process:

Attach a loop device to the various partitions inside the disk image:

losetup --find --partscan imagefile.dd

Reveal the partition devices that were assigned:

root@pi:/data# cat /proc/partitions 
major minor  #blocks  name

   8        0  976762584 sda
   8        1   53938206 sda1
   8        2   51210240 sda2
   8        3   48828416 sda3
   8        4  822784000 sda4
  11        0    1048575 sr0
   7        0    7822336 loop0     <--- imagefile.dd
 259        0      42811 loop0p1   <--- partition 1 inside imagefile.dd
 259        1    7281664 loop0p2   <--- partition 2 inside imagefile.dd


Run a filesystem check:

root@pi:/data# e2fsck /dev/loop0p2
e2fsck 1.44.5 (15-Dec-2018)
/dev/loop0p2 contains a file system with errors, check forced.
Pass 1: Checking inodes, blocks, and sizes
Inode 391641, i_blocks is 16, should be 8.  Fix<y>? yes
Inode 391642, i_blocks is 16, should be 8.  Fix<y>? yes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
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)
Fix<y>? yes
Free blocks count wrong for group #49 (4771, counted=4801).
Fix<y>? yes
Free blocks count wrong for group #50 (14121, counted=14151).
Fix<y>? yes
Free blocks count wrong (576554, counted=598442).
Fix<y>? yes

/dev/loop0p2: ***** FILE SYSTEM WAS MODIFIED *****
/dev/loop0p2: 262820/441728 files (0.3% non-contiguous), 1221974/1820416 blocks

(so a few errors there, but you may not see any errors)


Shrink the filesystem to the desired size:

root@chew:/data# resize2fs /dev/loop0p2 7000M
resize2fs 1.44.5 (15-Dec-2018)
Resizing the filesystem on /dev/loop0p2 to 1792000 (4k) blocks.
The filesystem on /dev/loop0p2 is now 1792000 (4k) blocks long.


Shrink the partition to a size that is (slightly) larger than the filesystem



After this, we can just write the disk image to the (smaller) block device without running into disk space issues