# put stock boot.img into /sdcard/boot.img
# get sha1
adb shell
su
SHA1=$(cat $(magisk --path)/.magisk/config | grep SHA1 | cut -d '=' -f 2)
# repack boot.img.gz
gzip -9f /sdcard/boot-stock.img
# restore backup
mkdir /data/magisk_backup_${SHA1}
mv /sdcard/boot-stock.img.gz /data/magisk_backup_${SHA1}/boot.img.gz
chmod -R 755 /data/magisk_backup_${SHA1}
chown -R root.root /data/magisk_backup_${SHA1}
-
-
Save pexcn/71d7d242c5e805d9346d2dc9db17fb90 to your computer and use it in GitHub Desktop.
drshahinshaan
commented
Apr 5, 2022
via email
Thanks for your quick response! Can i use termus for execute command?
It should work, but I haven't tried it
You can also just re-patch the stock boot image in the magisk app, and then reboot. The stock boot img is backed up when you patch it.
Hi, can u detail how to do it , please
I really dont know how
Huge thanks for this gist, mate!
You saved a lot of time for me
The first command should be
adb push boot.img /sdcard/boot-stock.img
not /sdcard/boot.img
If you already have stock boot.img file in /sdcard
you can do the do following and let Magisk do the backup
cd /data/adb/magisk
cp /sdcard/boot.img stock_boot.img
./magiskboot cleanup
. ./util_functions.sh
run_migrations
Line 2 should be cp /sdcard/boot.img stock_boot.img
Ref: https://github.com/topjohnwu/Magisk/blob/master/scripts/util_functions.sh#L625
You are correct, I was responding from memory and was wrong.
It is underscore not dash (corrected).
Thanks
Thanks!
I installed LineageOS 20 on OnePlus 9 with the instructions on official wiki. Then I root my phone with Magisk. I install Magisk by sideloading the Magisk ZIP file and then install the apk. Now I can't update the OS with updater in system settings. I find a guide that explained I need to uninstall and restore images in magisk then update my phone and reinsatall magisk before reboot. But magisk didn't create a backup during installation.
I use your guide to fix magisk stock backup does not exist
I create a backup with this guide and lineageos boot.img file and I can now restore images in magisk but I get the same error when trying to install the updates. I noticed that there is also dtbo in backup function (https://github.com/topjohnwu/Magisk/blob/master/scripts/util_functions.sh#L625) and I did the same thing for backing up dtbo.img and it doesn't solve the problem. Is there anything I'm missimg. Should I use vendor-boot.img instead of boot.img?
You can also just re-patch the stock boot image in the magisk app, and then reboot. The stock boot img is backed up when you patch it.
thank you! definitely the simplest way
So it's stock_boot.img
not boot_stock.img
?
@asheroto
Depends on which script you are running, if using badabing2005's script starting with existing boot image in /sdcard/boot.img then yes. If using the original pexcn script then he started with existing boot image called /sdcard/boot-stock.img
Easiest way is definitely repatching the stock boot image with magisk app though
thanks everyone for the hint, repatching the stock with magisk then reboot saved me from interfering with command line
you are god
Note that Pixel 7 Pro (and possibly other newer Pixel phones) are using init_boot.img
-- so you need to repack this file (not boot.img
which is also present in the OTA image!) and put /data/magisk_backup_${SHA1}/boot.img.gz
(yes, the latter path still is boot.img.gz
)
Otherwise great manual, thanks!
mark
It appears that Android 14 uses init_boot.img and boot.img if I'm not mistaken? At least one OnePlus phones...
In case of init_boot.img
# put stock init_boot.img into /sdcard/init_boot.img
# get sha1
adb shell
su
SHA1=$(cat $(magisk --path)/.magisk/config | grep SHA1 | cut -d '=' -f 2)
# repack init_boot.img.gz
gzip -9f /sdcard/init_boot.img
# restore backup
mkdir /data/magisk_backup_${SHA1}
mv /sdcard/init_boot.img.gz /data/magisk_backup_${SHA1}/boot.img.gz
chmod -R 755 /data/magisk_backup_${SHA1}
chown -R root.root /data/magisk_backup_${SHA1}
Everyone, there's no need to calculate sha or write your own script, just use util_function.sh from the magisk folder in adb. Topjohnwu has updated it for init_boot months ago.
Follow this comment for instruction. https://gist.github.com/pexcn/71d7d242c5e805d9346d2dc9db17fb90?permalink_comment_id=4330260#gistcomment-4330260
@giser have you tested this? Looks like it might work if when extracted it just overwrites the init_boot.img partition.
@stanleylhs indeed, but this fix doesn't always work as expected.
my phone is rooted but no /data/magisk_backup_* directories exist, so i tried to get magisk to create one. i tried using the app to patch a file. a patched boot image was created but no /data/magisk_backup_* directory. the path to the boot image was /sdcard/Documents/boot_images/init_boot_14_0_0_304.img. i tried copying it and giving it the name /sdcard/Documents/boot_images/init_boot.img and patching it. a patched boot image was created but still no /data/magisk_backup_* directory.
my phone is a US oneplus 11(cph2451) running android 14 and magisk 27.0 stable. how do i get the magisk app to patch a file and create a /data/magisk_backup_* directory?
i tried using util_functions.sh and run_migrations and that worked.
Thanks a lot for the script worked perfectly with init_boot @giser
My Pixel 4a 5G aka bramble runs LineageOS for microG. I've installed Magisk via patched boot.img. Therefore Magisk ought to have backuped stock boot.image already on that occasion. But it did not. Additionally I let Magisk patch stock boot.img several times after installation, in the hope it would then create a backup, which it did never.
The magisk_install.log from last installation is this:
- Device platform: arm64-v8a
- Installing: 27.0 (27000)
- Copying image to cache
- Unpacking boot image
Parsing boot image: [/data/user_de/0/com.topjohnwu.magisk/install/boot.img]
HEADER_VER [3]
KERNEL_SZ [11744698]
RAMDISK_SZ [2559738]
OS_VERSION [14.0.0]
OS_PATCH_LEVEL [2024-05]
PAGESIZE [4096]
CMDLINE []
KERNEL_FMT [lz4]
RAMDISK_FMT [lz4_legacy]
unexpected ASN.1 DER tag: expected SEQUENCE, got APPLICATION [1] (primitive)
VBMETA
- Checking ramdisk status
Loading cpio: [ramdisk.cpio]
- Stock boot image detected
- Patching ramdisk
- Pre-init storage partition: sda15
Loading cpio: [ramdisk.cpio]
Add file [init] (100750)
Create directory [overlay.d] (0750)
Create directory [overlay.d/sbin] (0750)
Add file [overlay.d/sbin/magisk32.xz] (100644)
Add file [overlay.d/sbin/magisk64.xz] (100644)
Add file [overlay.d/sbin/stub.xz] (100644)
Patch with flag KEEPVERITY=[true] KEEPFORCEENCRYPT=[true]
Loading cpio: [ramdisk.cpio.orig]
Backup [init] -> [.backup/init.xz]
Record new entry: [overlay.d] -> [.backup/.rmlist]
Record new entry: [overlay.d/sbin] -> [.backup/.rmlist]
Record new entry: [overlay.d/sbin/magisk32.xz] -> [.backup/.rmlist]
Record new entry: [overlay.d/sbin/magisk64.xz] -> [.backup/.rmlist]
Record new entry: [overlay.d/sbin/stub.xz] -> [.backup/.rmlist]
Create directory [.backup] (0000)
Add file [.backup/.magisk] (100000)
Dumping cpio: [ramdisk.cpio]
- Repacking boot image
Parsing boot image: [/data/user_de/0/com.topjohnwu.magisk/install/boot.img]
HEADER_VER [3]
KERNEL_SZ [11744698]
RAMDISK_SZ [2559738]
OS_VERSION [14.0.0]
OS_PATCH_LEVEL [2024-05]
PAGESIZE [4096]
CMDLINE []
KERNEL_FMT [lz4]
RAMDISK_FMT [lz4_legacy]
unexpected ASN.1 DER tag: expected SEQUENCE, got APPLICATION [1] (primitive)
VBMETA
Repack to boot image: [new-boot.img]
HEADER_VER [3]
KERNEL_SZ [11744698]
RAMDISK_SZ [2496732]
OS_VERSION [14.0.0]
OS_PATCH_LEVEL [2024-05]
PAGESIZE [4096]
CMDLINE []
****************************
Output file is written to
/storage/emulated/0/Download/magisk_patched-27000_Dx8Mh.img
****************************
cp: can't preserve ownership of 'busybox': Operation not permitted
cp: can't preserve ownership of 'magisk32': Operation not permitted
cp: can't preserve ownership of 'magisk64': Operation not permitted
cp: can't preserve ownership of 'magiskboot': Operation not permitted
cp: can't preserve ownership of 'magiskinit': Operation not permitted
cp: can't preserve ownership of 'magiskpolicy': Operation not permitted
- All done
I wonder, if these not permitted operations are causing this failing to backup the stock boot.img and what I might do about it.
bro I don't have SD card. Can I done with internal storage??
This script helped me. Thank you!
bro I don't have SD card. Can I done with internal storage??
"/sdcard" is your internal storage