Moving Linux to a smaller hard disk

When building my NAS I’ve picked a spare laptop hdd (spinny type) for the OS to boot from, but I’ve reached to a point where I wanted my NAS os to boot from a SDD.

Copy data

Since a stock Fedora 20 doesn’t use so much disk space, I wanted to switch from a 250Gb SATA hdd to a smaller but faster 80Gb SSD (an old Intel X25-M which I have for few years now).

Since the existing disk was partitioned automatically, I’ve decided to create similar partitions on the new disk. Existing partitions were:

/dev/sde3 on / type ext4 (rw,relatime,seclabel,data=ordered)
/dev/sde1 on /boot type ext4 (rw,relatime,seclabel,data=ordered)
/dev/sde5 on /home type ext4 (rw,relatime,seclabel,data=ordered)

One thing not to forget is to set the new boot partition to be bootable. Also, not mentioned is the swap partition, it should be created too (and later have the new UUID updated in /etc/fstab).
After creating the new partitions (I’m copying /dev/sde to /dev/sda) was to mount the new partitions, so that I can proceed to copy the data:

/dev/sda1 on /media/ssd/boot type ext4 (rw,relatime,seclabel,data=ordered)
/dev/sda3 on /media/ssd/root type ext4 (rw,relatime,seclabel,data=ordered)
/dev/sda5 on /media/ssd/home type ext4 (rw,relatime,seclabel,data=ordered)

Copying the data was done with dump and restore similar to:

/usr/sbin/dump 0f -  | (cd /mnt/newdrive(/folder) && /usr/sbin/restore xf - )

So here goes:

/usr/sbin/dump 0f - /dev/sde1 | (cd /media/ssd/boot && /usr/sbin/restore xf - )
/usr/sbin/dump 0f - /dev/sde3 | (cd /media/ssd/root && /usr/sbin/restore xf - )
/usr/sbin/dump 0f - /dev/sde5 | (cd /media/ssd/home && /usr/sbin/restore xf - )

A detailed output of copying the root partition:

[root@biggie ssd]# /usr/sbin/dump 0f - /dev/sde3 | (cd /media/ssd/root && /usr/sbin/restore xf - )
  DUMP: Date of this level 0 dump: Fri Apr 17 22:52:47 2015
  DUMP: Dumping /dev/sde3 (/) to standard output
  DUMP: Label: none
  DUMP: Writing 10 Kilobyte records
  DUMP: mapping (Pass I) [regular files]
  DUMP: mapping (Pass II) [directories]
  DUMP: estimated 25369248 blocks.
  DUMP: Volume 1 started with block 1 at: Fri Apr 17 22:52:51 2015
  DUMP: dumping (Pass III) [directories]
  DUMP: dumping (Pass IV) [regular files]
/usr/sbin/restore: ./lost+found: File exists
  DUMP: 18.57% done at 15704 kB/s, finished in 0:21
  DUMP: 53.97% done at 22821 kB/s, finished in 0:08
  DUMP: 69.82% done at 19680 kB/s, finished in 0:06
  DUMP: 86.61% done at 18310 kB/s, finished in 0:03
  DUMP: Volume 1 completed at: Fri Apr 17 23:17:15 2015
  DUMP: Volume 1 26316040 blocks (25699.26MB)
  DUMP: Volume 1 took 0:24:24
  DUMP: Volume 1 transfer rate: 17975 kB/s
  DUMP: 26316040 blocks (25699.26MB)
  DUMP: finished in 1464 seconds, throughput 17975 kBytes/sec
  DUMP: Date of this level 0 dump: Fri Apr 17 22:52:47 2015
  DUMP: Date this dump completed:  Fri Apr 17 23:17:15 2015
  DUMP: Average transfer rate: 17975 kB/s
set owner/mode for '.'? [yn] y

Prepare new disk for boot

Two remaining things: update grup as well as update UUIDs in /etc/fstab. To update grub, we need to chroot into the new root partition, but before that, we need to mount the new boot partition onto the boot folder into the new root. Sounds complicated but it is not:

cd /media/ssd
mount /dev/sda3  root/
mount -o bind /dev dev/
mount /dev/sda1 boot/
chroot /media/ssd/root

Now, the /media/ssh/root points to the new root partition, and /media/ssh/root/boot points to the new boot partition. This is done so that grup utilities will actually write the files correctly onto the new hard disk.

Install grub: (first command is to create the proper configuration file, second command is to set the bootloader:)

grub2-install /dev/sda

Now we can exit the chroot environment (with exit) and the only remaining thing is to replace the UUIDs in /media/ssd/root/etc/fstab with the new UUIDs: For this I’ve relied on a perl oneliner.

[root@biggie ssd]# blkid
/dev/sda1: UUID="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" TYPE="ext4" PARTUUID="aaaaaaaa-aa"
/dev/sde1: UUID="bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb" TYPE="ext4" PARTUUID="bbbbbbbb-bb"

Since I am copying from sde to sda the ‘bbbb…’ UUID has to be found and replaced with the ‘aaaa…’ one.

[root@biggie etc]# pwd
perl -pi -w -e 's/bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa/g;' fstab

The perl one liner has to be called for all 4 partitions (root, swap, home and boot).

Leave a Reply