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

DietPi-Backup | Add Option to Automatically Restore on First Boot #4112

Merged
merged 36 commits into from
Feb 27, 2021

Conversation

ravenclaw900
Copy link
Collaborator

@ravenclaw900 ravenclaw900 commented Feb 15, 2021

Status: Ready

Reference: #4066

Commit list/description:

  • DietPi-Software | Add way to automatically restore from backup on first boot
  • DietPi-Software | Make mawk more specific
  • DietPi-Software | Various fixes
  • DietPi-Software | Boot into normal DietPi after restoring backup
  • DietPi-Software | Don't change install state and sync filesystem
  • DietPi-Software | Switch from sed to G_CONFIG_INJECT
  • DietPi-Backup | Fix search function

Add restore dietpi.txt option
+ DietPi-Software | Add way to automatically restore from backup on first boot
@ravenclaw900 ravenclaw900 added this to the v7.1 milestone Feb 15, 2021
@ravenclaw900 ravenclaw900 self-assigned this Feb 15, 2021
@ravenclaw900 ravenclaw900 changed the title Restore boot DietPi-Backup | Add Option to Automatically Restore on First Boot Feb 15, 2021
+ DietPi-Software | CodeFactor fixes
+ DietPi-Software | Make mawk more specific
@MichaIng
Copy link
Owner

Awesome! I think we can add this to v7.0 already.

@MichaIng MichaIng modified the milestones: v7.1, v7.0 Feb 15, 2021
@Joulinar
Copy link
Collaborator

will this work already if I do fresh installation and use this branch as target?

@MichaIng
Copy link
Owner

Yes, since the update is performed first 🙂.

dietpi/dietpi-software Outdated Show resolved Hide resolved
dietpi/dietpi-software Outdated Show resolved Hide resolved
dietpi/dietpi-software Outdated Show resolved Hide resolved
@MichaIng
Copy link
Owner

Let's also unmount the auto-mounted drives and remove the mount points:

umount /mnt/dietpi-backup[0-9]*
rmdir /mnt/dietpi-backup[0-9]*

+ DietPi-Software | Various fixes
+ DietPi-Software | Remove other instance of $BACKUP_STATS_FILENAME
+ DietPi-Software | Syntax Fixes
+ DietPi-Software | Fix alist syntax error
+DietPi-Software | Boot into normal DietPi after restoring backup
+ DietPi-Software | Fix booting into normal DietPi
dietpi/dietpi-software Outdated Show resolved Hide resolved
@MichaIng MichaIng linked an issue Feb 15, 2021 that may be closed by this pull request
@MichaIng
Copy link
Owner

MichaIng commented Feb 15, 2021

Testing procedure on an existing system:

dietpi-backup 1
G_DEV_BRANCH restore-boot
G_CONFIG_INJECT 'AUTO_SETUP_BACKUP_RESTORE=' 'AUTO_SETUP_BACKUP_RESTORE=1' /boot/dietpi.txt # Respectively "=2"
systemctl enable dietpi-firstboot # To simulate a full fresh install
reboot

@MichaIng
Copy link
Owner

MichaIng commented Feb 15, 2021

Dammit that we log to /var/log/dietpi-backup.log (RAMlog) so it cannot be reviewed. Probably we should move it to /var/tmp/dietpi/logs/dietpi-backup.log 🤔.

@Joulinar
Copy link
Collaborator

probably a good idea to check what happen. but I can try to catch it. 🤔

@Joulinar
Copy link
Collaborator

Joulinar commented Feb 16, 2021

ok I login during initial setup trying to catch what happen. Right after restore finished, some commands are missing

root@DietPi:/var/log# ls
-bash: /usr/bin/ls: No such file or directory
root@DietPi:/var/log#

attached the log file. Really strange. It goes to delete entire modules and firmware 😭

dietpi-backup.zip

@ravenclaw900
Copy link
Collaborator Author

ravenclaw900 commented Feb 16, 2021

That is very strange. Did you try it with a different backup/normal DietPi-Backup?

@MichaIng
Copy link
Owner

MichaIng commented Feb 16, 2021

I got it, it's two issues. First is the merge of /bin /lib etc. with /usr/bin /usr/lib etc.

Background on this:

  • https://www.freedesktop.org/wiki/Software/systemd/TheCaseForTheUsrMerge/
  • Debian and many other distributions aim to merge /lib and /bin with /usr/lib and /usr/bin since this separation has historical reasons that do no apply anymore and is not always compatible with other UNIX-like OS'es.
  • To achieve best compatibility, /lib and /bin (and alike) are created as symlinks to /usr/lib and /usr/bin (and alike).

I just verified that the new RPi images contain this merged directories as well, which is quite new, AFAIK. For new Debian images is was the case for years already, but for RPi I see it the first time (doesn't mean something 😄). So this is what I guess happens:

  • You try to restore a backup from an older image, with non-merged directories.
  • DietPi-Backup restores hence the original actual files to /bin and /lib (removing the symlinks they were before). You see all those >f+++++++++ entries in the log, stating that new files are created.
  • Then, inside /usr/bin and /usr/lib, those files, which were only placed there due to the symlink, are accordingly removed.
  • hash -d should however fix the case that for that reason the hashed /usr/bin commands are not found. Finally a reboot ultimately solves it.

The second more problematic thing: One can only restore a backup to a new image where UUIDs and PARTUUIDs do still match, as otherwise fstab and boot configs contain invalid entries. To allow more, we'd need to either manually change fstab and boot configs (including grub update etc), or, probably easier, change the UUIDs/PARTUUIDs of the current root drive partitions to match the ones from the restored backup: https://www.tecmint.com/change-uuid-of-partition-in-linux/
tune2fs works on ext4 only. For the vfat boot partition:

But now there can be F2FS, GPT partition tables etc where things become more complicated 🤔. It looks like it can only be safely used when the original image/archive that was once flashed is kept... But not practically when it's very old.

It becomes quite complicated but we could loop through the drives partitions UUIDs and PARTUUIDs, check whether one of the two is present in the backups fstab (if it matches, assume that the other one does as well), otherwise check the partition table type and file system type to know whether we can adjust it, and if not print that to the user to deal with it manually. Auto-adjusting fstab and boot config is probably less save since we cannot know where else those IDs are used.

Ah another practical issue: Where do we get the old IDs from? fstab contains UUIDs but usually not PARTUUIDs. For the latter we again need to check and scrape boot configs. Arrg sadly this will never work failsafe...

@Joulinar
Copy link
Collaborator

Joulinar commented Feb 16, 2021

uhh probably you are on the right track @MichaIng

basically I have a master backup on my USB stick that I use to restore/reset the same RPi again and again. This backup will be updated time by time. Usually after larger apt package updates, to avoid long apt installations in front of a software install. Means, the initial backup was create some month ago and refreshed last time on the 12th of Feb. A restore on the current live system is working without issues. However it's gonna fail if I restore on a fresh image.

Just for reference, backup status log attached.

dietpi-backup_stats.log

@MichaIng
Copy link
Owner

I think this is something that needs to be added to dietpi-backup restore code directly, as a pre-check before restoring the backup, since users might also manually restore the backup on a new image. But I'm not sure how to effectively deal with it:

  • We can scrape the IDs from the backups fstab/cmdline.txt/boot.ini/armbianEnv.txt, but it is not failsafe, since only either UUID or PARTUUID is used while on the new image it might be different.
  • ... rethought: I think it's safer to adjust fstab and boot configs instead of adjusting the UUID and PARTUUID where i don't even know if it's possible with all relevant file systems and how the (flashed) bootloader behaves, U-Boot in particular.
  • So we check the current root and boot partitions UUID and PARTUUID, then search and replace them in the above list of files + update-grub + update-initramfs -u / update-tirfs after the backup has been restored. A warning + G_WHIP_YESNO is required in any case when partitions/IDs have been changed.

@MichaIng
Copy link
Owner

Okay guys, I'll port the few unrelated little fixes and enhancements to the dev branch now but won't merge this feature into the first beta. We need to handle drive ID issue first. Should be possible to have it until next week for 2nd or 3rd beta.

+ Update restore-boot branch
@MichaIng
Copy link
Owner

Coincidence: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=563746
An update of dosfstools that allows to update the FAT volumes UUIDs via fatlabel has just been merged into Bullseye branch 😄. But not relevant anymore as of (my) current plan to mitigate the issue.

+ DietPi-Backup | Do not stop services before we definitely run the backup or restore. The rsync process check still needs to be done after stopping the services, which include the rsync daemon. But do not kill a running rsync process afterwards, better error out.
+ DietPi-Backup | Implement a first function when restoring a backup, to check whether the backup's UUIDs match the current drive's UUIDs, so that we do not restore a system that is not booting, because the bootloader and fstab do not find the configured drives. If the UUIDs seem to not match, check whether we are able to reliably make the system bootable by adjusting known bootloader configurations. Add function to restore UUIDs, in case, but content is on ToDo for now :).
+ DietPi-Globals | G_WHIP_YESNO: If the default is set to "yes", return "true" as well in case of non-interactive calls
+ DietPi-Backup | Restore now obsolete functions
+ DietPi-Backup | Satisfy shellcheck
+ DietPi-Backup | Add whiptail yes/no dialogues when storing a backup to a system with different UUIDs. Default to "yes", if we did find the boot configuration method, else default to "no", which is especially relevant for non-interactive first run setups with AUTO_SETUP_BACKUP_RESTORE=1 set.
+ DietPi-Backup | Full function to update UUID entries in fstab and boot configs after restore, if required
+ DietPi-Backup | Satisfy shellcheck
@MichaIng
Copy link
Owner

Ready for testing.

+ DietPi-Drive_Manager | Fix grabbing input argument
+ DietPi-Backup | Fine tune output, coding and order of service start/stop and interactive parts
+ DietPi-Backup | Tiny
@Joulinar
Copy link
Collaborator

Ok will run some test on weekends

+ DietPi-Backup | Fix SBC boot config estimation and avoid doubled fstab rootfs edit as the loop includes root as well
@MichaIng
Copy link
Owner

Tested with all boot configs, and works fine now. I'll merge it to dev branch, so it can be tested together with other recent changes.

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

Successfully merging this pull request may close these issues.

DietPi-Backup | Restore on First Boot
3 participants