Storage and Filesystems
These are some tricks and hints to common issues/tasks to deal sometimes
Resize root filesystem
This example is based on resizing the filesystem on the raspberryPi2 SDcard, but should apply to other scenarios
Show partition table, and take note of the output, specially start and end sectors of the partition:
# fdisk -l /dev/mmcblk0 Disk /dev/mmcblk0: 7.4 GiB, 7948206080 bytes, 15523840 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: 0xc96ffe28 Device Boot Start End Sectors Size Id Type /dev/mmcblk0p1 2048 249855 247808 121M e W95 FAT16 (LBA) /dev/mmcblk0p2 249856 6291455 6041600 2.9G 83 Linux
Note that mmcblk0 is about 7.5Gb, but the filesystem root (partition 2) is only using 3Gb.
Run interactive fdisk over the disk and do the following:
- Delete the parition
d
- Create new partition
n
, same type, number and… exactly the same start sector (249856 in our example) - Use the remaining space (end sector)
- Write changes to disk
w
Now you have to reboot the system, after that, you should be able to see the new partition size:
# fdisk -l Disk /dev/mmcblk0: 7.4 GiB, 7948206080 bytes, 15523840 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: 0xc96ffe28 Device Boot Start End Sectors Size Id Type /dev/mmcblk0p1 2048 249855 247808 121M e W95 FAT16 (LBA) /dev/mmcblk0p2 249856 15523839 15273984 7.3G 83 Linux
Note that now the end sector of the partition is now 15523839, although filesystem is still the same size:
~# df -Th / Filesystem Type Size Used Avail Use% Mounted on /dev/root ext4 2.8G 771M 1.9G 29% / # resize2fs /dev/mmcblk0p2 resize2fs 1.42.12 (29-Aug-2014) Filesystem at /dev/mmcblk0p2 is mounted on /; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 1 The filesystem on /dev/mmcblk0p2 is now 1909248 (4k) blocks long. # df -Th / Filesystem Type Size Used Avail Use% Mounted on /dev/root ext4 7.2G 774M 6.1G 12% /
Resizing filesystems with unsupported features
Although there are tools to resize any extN filesystems, many times don't work out-of-the-box due the default filesystem settings and features used for Linux distros.
Here explain steps and commands to allow that resizing feature. Please keep in mind the usual warn about 'responsability exemption'. Also take note that the process is long (depending on filesystem size and block device).
Is generally recommended to run a e2fsck after any filesystem change (either in size or in metadata/features).
The unique features supported by parted to do a resizing are:
hasjournal
*
dirindexfiletype
sparsesuper
*
largefile
Will use some advanced commands like tune2fs and debugfs. With tune2fs you can see the fatures used by the filesystem:
tune2fs -l /dev/sda1 | grep features
The following command shoulds remove these faetures:
tune2fs -O ^ext_attr,^resize_inode /dev/sde1 debugfs -w -R "feature -ext_attr -resize_inode -dir_index"
Now, check/fix filesystem
e2fsck -f -y -v $DEV
Do the resize
Check/fix filesystem again
e2fsck -f -y -v $DEV
Restore the features removed before:
debugfs -w -R "feature ext_attr resize_inode dir_index" $DEV
Check/fix filesystem again
e2fsck -f -y -v $DEV
Replicate/mirror huge amount of data
rsync is the best tool to guarantee identic replication (including hardlinks) but may require a huge amount of memory and will be slow on the first initial sync.
Best is to use cpio and just after, sync again:
find . -xdev -depth -print | cpio -pdum $DESTINATION rsync -au -x -H --devices --specials --size-only --stats $SOURCE $DESTINATION
I deveveloped a handy script dataMover.sh for that
Move root filesystem
rootfs.sh
script I made
If you are moving the rootfs from one device to another (for example a new disk), you will need to setup grub loader.
Supposing you already copied all the rootfs filesystem to the new partition, to setup the boot chain follow as:
mkdir -p $TEMPROOT parted $DISK set $PART boot on mount $NEWDEV $TEMPROOT
Mount the some virtual filesystems needed on it:
mount -o bind /dev $TEMPROOT/dev/ mount -t proc none $TEMPROOT/proc/ mount -t sysfs none $TEMPROOT/sys/
Now will run grub inside a chroot environment inside the new ddisk:
chroot $TEMPROOT /usr/sbin/update-grub chroot $TEMPROOT /usr/sbin/grub-install $DISK
To finish umount everything so all data is flushed to disk.