Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep filesystem labels #16

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

matthijskooijman
Copy link

@matthijskooijman matthijskooijman commented Apr 5, 2024

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

Previously, e2label was used, but that only works for ext partitions.
Using lsblk allows showing the filesystem labels for all filesystems
supported by the kernel.
Now labels are retrieved for all destination partitions, there is no
longer any point in separately retrieving the destination root label,
just use the previously retrieved label.
Instead of only setting FS labels on ext partitions when specified with
the --label-partitions option and leave all other partitions unlabeled,
this tries to copy the source filesystem labels to the destination where
possible.

Setting labels requires filesystem-specific commands or mkfs options, so
not all filesystems are supported. For changing labels on existing
partitions, only ext and fat partitions are supported. For mkfs a few
more are supported, though these are probably not used in practice.

This also refactors some of the code, introducing a `mkfs_label()` and
`change_label()` function to prevent having to duplicate the
filesystem-type checking code.

This fixes billw2#100.
This makes it non-interactive and prevents the script from silently
hanging when parted finds something weird, e.g.:

    $ sudo rpi-clone /dev/sda
    Warning: The driver descriptor says the physical block size is 2048 bytes, but Linux says it is 512 bytes.

This just hangs, because parted is waiting for an answer:

    $ sudo parted -m /dev/sda unit s print
    Warning: The driver descriptor says the physical block size is 2048 bytes, but Linux says it is 512 bytes.
    Ignore/Cancel?
It seems that parted can force a partition scan (observed on a removable
USB disk), which temporaly clears the filesystem labels from lsblk
output. To prevent this, call udevadm settle to wait until all udev
events are processed before continuing.
@matthijskooijman matthijskooijman changed the title Keep fs labels Keep filesystem labels Apr 5, 2024
@matthijskooijman
Copy link
Author

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
Copy link

exetico commented Jun 14, 2024

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: billw2#122)

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Minor issue - e2label
2 participants