geerlingguy / rpi-clone

A shell script to clone a booted disk on a Raspberry Pi.
https://rpi-clone.jeffgeerling.com/
BSD 3-Clause "New" or "Revised" License
172 stars 10 forks source link

Keep filesystem labels #16

Open matthijskooijman opened 3 months ago

matthijskooijman commented 3 months ago

This makes some changes to display and preserve all filesystem labels rather than just for ext filesystems.

Displaying of labels should be supported for any filesystem supported by the kernel (using lsblk), while copying labels is supported for a limited set of filesystems (just ext and fat for existing filesystems, a few more for new filesystems).

See the commit messages for some more details.

These changes were previously submitted at https://github.com/billw2/rpi-clone/pull/101 and have been in use in my project since that PR was submitted four years ago. Now I've just rebased and reviewed them, the changes still applied without issue.

This also fixes #7.

matthijskooijman commented 3 months ago

As a side effect, this no longer assumes the rootfs is ext, which fixes incorrectly running e2label on non-ext rootfs (e.g. f2fs as reported in #7).

exetico commented 3 weeks ago

Just spotted your PR, after my Pi didn't boot after a clone

So I did the thing:

git clone https://github.com/matthijskooijman/rpi-clone.git
cd rpi-clone
git checkout keep-fs-labels

And finally ended with a sudo ./rpi-clone sda.

Booted disk: mmcblk0 127.9GB               Destination disk: sda 500.1GB
---------------------------------------------------------------------------
Part               Size    FS     Label           Part   Size    FS     Label  
1 /boot/firmware   512.0M  fat32  bootfs          1      512.0M  fat32  --     
2 root             118.6G  ext4   rootfs          2      465.3G  ext4   --     
---------------------------------------------------------------------------
== SYNC mmcblk0 file systems to sda ==
/boot/firmware        (63.0M used)   : SYNC to sda1 (512.0M size)
/                     (102.9G used)  : SYNC to sda2 (465.3G size)
---------------------------------------------------------------------------
Run setup script       : no.
Verbose mode           : no.
-----------------------:

Ok to proceed with the clone?  (yes/no): yes

Syncing file systems (can take a long time)
Syncing mounted partitions:
     e2label /dev/sda2 rootfs
  Mounting /dev/sda2 on /mnt/clone
mount: (hint) your fstab has been modified, but systemd still uses
       the old version; use 'systemctl daemon-reload' to reload.
  => rsync // /mnt/clone with-root-excludes ...
     fatlabel /dev/sda1 bootfs
fatlabel: warning - lowercase labels might not work properly on some systems
  Mounting /dev/sda1 on /mnt/clone/boot/firmware
mount: (hint) your fstab has been modified, but systemd still uses
       the old version; use 'systemctl daemon-reload' to reload.
  => rsync /boot/firmware/ /mnt/clone/boot/firmware  ...

Editing /mnt/clone/boot/firmware/cmdline.txt PARTUUID to use 41300809
Editing /mnt/clone/etc/fstab PARTUUID to use 41300809
===============================
Done with clone to /dev/sda
   Start - 21:41:31    End - 21:41:59    Elapsed Time - 0:28

Cloned partitions are mounted on /mnt/clone for inspection or customizing.

Hit Enter when ready to unmount the /dev/sda partitions ...
  unmounting /mnt/clone/boot/firmware
  unmounting /mnt/clone
===============================

I didn't really think about rsync should be able to get things sorted, without having to copy everything over again.

It managed to fix it in a few seconds, and everyting worked as expected. Thanks!

Also, here's the result of the first clone, with the good old branch: ``` sudo rpi-clone sda -f Booted disk: mmcblk0 127.9GB Destination disk: sda 500.1GB --------------------------------------------------------------------------- Part Size FS Label Part Size FS Label 1 /boot/firmware 512.0M fat32 -- 1 100.0M fat32 -- 2 root 118.6G ext4 rootfs 2 16.0M -- -- 3 465.1G ntfs -- 4 508.0M ntfs -- --------------------------------------------------------------------------- == Initialize: IMAGE partition table - forced by option == 1 /boot/firmware (63.0M used) : MKFS SYNC to sda1 2 root (102.9G used) : RESIZE MKFS SYNC to sda2 --------------------------------------------------------------------------- Run setup script : no. Verbose mode : no. -----------------------: ** WARNING ** : All destination disk sda data will be overwritten! -----------------------: Initialize and clone to the destination disk sda? (yes/no): yes Optional destination ext type file system label (16 chars max): Initializing Imaging past partition 1 start. => dd if=/dev/mmcblk0 of=/dev/sda bs=1M count=8 ... Resizing destination disk last partition ... Resize success. Changing destination Disk ID ... => mkfs -t vfat -F 32 /dev/sda1 ... => mkfs -t ext4 /dev/sda2 ... Syncing file systems (can take a long time) Syncing mounted partitions: Mounting /dev/sda2 on /mnt/clone mount: (hint) your fstab has been modified, but systemd still uses the old version; use 'systemctl daemon-reload' to reload. => rsync // /mnt/clone with-root-excludes ... Mounting /dev/sda1 on /mnt/clone/boot/firmware mount: (hint) your fstab has been modified, but systemd still uses the old version; use 'systemctl daemon-reload' to reload. => rsync /boot/firmware/ /mnt/clone/boot/firmware ... Editing /mnt/clone/etc/fstab PARTUUID to use 41300809 =============================== Done with clone to /dev/sda Start - 18:21:37 End - 19:08:11 Elapsed Time - 46:34 Cloned partitions are mounted on /mnt/clone for inspection or customizing. Hit Enter when ready to unmount the /dev/sda partitions ... unmounting /mnt/clone/boot/firmware unmounting /mnt/clone =============================== ```

Thank you for making this! (Also mentioned in the old branch, where I noticed it first: https://github.com/billw2/rpi-clone/issues/122)

@geerlingguy Let's get this into the very kind and friendly fork 😄 !