Menu

[Solution] Fully dynamic USB auto-restore/save wizard — no /dev/sdb1 hardcoding (GRUB + UUID + script)

2026-05-30
2026-05-30
  • Ace Ventura

    Ace Ventura - 2026-05-30

    I built a clean, hardware-agnostic Recovery Wizard that works from the USB itself — both RESTORE and SAVE — without any hardcoded device names.

    How it works (the flow)

    1. GRUB dynamically finds the USB with search --set -f /live/vmlinuz and captures its permanent FAT32 UUID with probe -u $root --set=usb_uuid.
    2. The UUID is injected into ocs_prerun1 so Clonezilla can temporarily mount the USB, copy script.sh locally, then unmount.
    3. The bash script parses the exact same UUID from /proc/cmdline using a simple sed one-liner, runs fsck.fat, mounts the USB to /mnt/usb, and creates the required symlink /home/partimag → /mnt/usb/zBackup.
    4. ocs_live_run then sees the images in the exact place Clonezilla expects → fully automated restore or save.

    No LABELs, no static UUIDs in grub.cfg, no device names. Works even with extra hard drives plugged in.

    1. GRUB EFI menu entries (add to /boot/grub/grub.cfg on the USB)

    menuentry "RESTORE: macmini-preinstall -> AUTO-DISK " --hotkey=r {
        set img="macmini-preinstall"
        search --set -f /live/vmlinuz
        probe -u $root --set=usb_uuid
        linux /live/vmlinuz \
            boot=live union=overlay config noswap nolocales edd=on nomodeset noprompt \
            keyboard-layouts=NONE locales=en_US.UTF-8 vga=788 toram=live,syslinux,EFI,boot,.disk,utils \
            net.ifnames=0 nosplash i915.blacklist=yes radeonhd.blacklist=yes nouveau.blacklist=yes \
            vmwgfx.enable_fbdev=1 ocs_1_cpu_udev scsi_mod.use_blk_mq=0 nvme.poll_queues=1 \
            ocs_prerun="mkdir -p /mnt" \
            ocs_prerun1="mount -U ${usb_uuid} /mnt" \
            ocs_prerun2="mkdir -p /live" \
            ocs_prerun3="cp /mnt/boot/grub/script.sh /live" \
            ocs_prerun4="umount /mnt" \
            ocs_prerun5="sleep 2" \
            ocs_prerun6="bash /live/script.sh" \
            ocs_prerun7="ls /home/partimag" \
            ocs_prerun8="sleep 7" \
            ocs_live_batch="yes" \
            ocs_live_run="ocs-sr -batch -g auto -e1 auto -e2 -r -j2 -p reboot restoredisk ${img} sda"
        initrd /live/initrd.img
    }
    
    menuentry "SAVE: AUTO-DISK -> macmini-preinstall" --hotkey=c {
        set img="macmini-preinstall"
        search --set -f /live/vmlinuz
        probe -u $root --set=usb_uuid
        linux /live/vmlinuz \
            boot=live union=overlay config noswap nolocales edd=on nomodeset noprompt \
            keyboard-layouts=NONE locales=en_US.UTF-8 vga=788 toram=live,syslinux,EFI,boot,.disk,utils \
            net.ifnames=0 nosplash i915.blacklist=yes radeonhd.blacklist=yes nouveau.blacklist=yes \
            vmwgfx.enable_fbdev=1 ocs_1_cpu_udev scsi_mod.use_blk_mq=0 nvme.poll_queues=1 \
            ocs_prerun="mkdir -p /mnt" \
            ocs_prerun1="mount -U ${usb_uuid} /mnt" \
            ocs_prerun2="mkdir -p /live" \
            ocs_prerun3="cp /mnt/boot/grub/script.sh /live" \
            ocs_prerun4="umount /mnt" \
            ocs_prerun5="sleep 2" \
            ocs_prerun6="bash /live/script.sh" \
            ocs_prerun7="ls /home/partimag" \
            ocs_prerun8="sleep 7" \
            ocs_live_batch="yes" \
            ocs_live_run="ocs-sr -q2 -batch -j2 -edio -z9p -i 4096 -scs -senc -plu -rm-win-swap-hib -p reboot savedisk ${img} sda"
        initrd /live/initrd.img
    }
    

    2. Bash script (/boot/grub/script.sh on the USB — make executable with chmod +x)

    #!/bin/bash
    # =============================================
    # USB Mount folder script - UUID from ocs_prerun
    # =============================================
    
    # dynamically read the USB UUID that GRUB passed us from the ocs_prerun1 mount line in /proc/cmdline
    usb_uuid=$(cat /proc/cmdline | sed -E 's/.*mount -U ([0-9A-Fa-f-]+).*/\1/' | head -n1)
    echo "Detected USB UUID from ocs_prerun: $usb_uuid"
    
    if [ -z "$usb_uuid" ] || [[ ! "$usb_uuid" =~ ^[0-9A-Fa-f-]+$ ]]; then
        echo "ERROR: Could not extract UUID from ocs_prerun line!"
        cat /proc/cmdline | grep -o 'mount -U [^ ]*'
        sleep 10
        exit 1
    fi
    
    # create mount folder to use
    mkdir -p /mnt/usb
    
    # fix common FAT32 'dirty' errors silently before mounting
    fsck.fat -a /dev/disk/by-uuid/${usb_uuid} || true
    
    # Mount as FAT32 (vfat) - 'flush' helps prevent data loss if the USB is pulled early
    mount -U ${usb_uuid} -t vfat -o rw,flush,umask=000 /mnt/usb
    
    # create a symbolic link to the mmb folder as /home/partimag
    cd /home
    rm -r /home/partimag >/dev/null 2>&1 # this is safe even if already mounted as only symbolic link, also -r switch is safe
    mmbdir="/mnt/usb/zBackup"
    linkmmbdir=(ln -fs "${mmbdir}" partimag)
    "${linkmmbdir[@]}"
    
    # validation
    if [ -d "/home/partimag" ]; then
        echo "SUCCESS: found folders on FAT32 drive."
        ls -F /home/partimag
    else
        echo "ERROR: folders not found – check mount below:"
        mount | grep -E 'usb|partimag'
    fi
    
    echo "=== Script finished ==="
    sleep 3
    

    Quick setup

    1. Burn normal Clonezilla Live to USB (Etcher/Rufus).
    2. Add the two menuentries to /boot/grub/grub.cfg.
    3. Copy the script to /boot/grub/script.sh and chmod +x.
    4. Put your images in the zBackup folder on the USB.
    5. Boot → pick RESTORE or SAVE → fully automatic.

    Tested on a Mac Mini (EFI) with extra drives plugged in — works perfectly every time.

     

    Last edit: Ace Ventura 2026-05-30
  • Steven Shiau

    Steven Shiau - 2026-05-30

    Great. Thanks for sharing that.
    Another way to do that is, if you accept the have a label for your FAT32 partition on USB stick, say "OCS-REPO", then:
    root@cl-20260525-s:~$ blkid /dev/sdd1
    /dev/sdd1: LABEL_FATBOOT="OCS-REPO" LABEL="OCS-REPO" ...
    root@cl-20260525-s:~$ mount -L OCS-REPO /home/partimag/

     
  • ski-777

    ski-777 - 2026-05-30

    I apologize if I'm writing off-topic information, because in this case we need: GRUB and a USB drive... but then it's also simpler and almost automatic: saving a system backup and restoring from the grub menu. More: https://github.com/ski007/grubzilla

     
    👍
    1
  • Steven Shiau

    Steven Shiau - 2026-05-30

    Cool. Thanks for sharing that.

     

Log in to post a comment.

Auth0 Logo