topjohnwu / Magisk

The Magic Mask for Android
GNU General Public License v3.0
47.66k stars 12.1k forks source link

Way to build Magisk into a ROM via aosp build process #509

Closed robsee closed 5 years ago

robsee commented 6 years ago

It would be great if there were a way to build Magisk into a rom as part of the compilation process. Minimally, if you could patch the boot.img on a separate host (instead of on phone), that would work.

topjohnwu commented 6 years ago

I do plan to add support to build Magisk along with AOSP, including system-based like bundling into GSIs

robseesuny commented 6 years ago

I've kind of gotten it working now, but it requires patching system/core/init to end up in the ramdisks as init.orig. Then I hacked magiskinit to do its stuff, and then rename init.orig to init, and continue

Black-Seraph commented 5 years ago

Any news on this yet?

9e3fce0a commented 5 years ago

I'm also keen to get this working with something like https://github.com/dan-v/rattlesnakeos-stack

See reddit discussion here

Any tips?

androidacy-user commented 5 years ago

Any progress on this?

My team developing lunaOS (https://github.com/lunaOS-ROM) wishes to ship our ROM (and preferably our GSI variant) with magisk as our root solution. I personally love the whole systemless mods (less chance of bricking a device) and the wealth of module available

However, we wish to know if it's even possible and if so how

rootinchase commented 5 years ago

Has there been any update on this?

Is it in progress? Are there things tripping you up? Is it on a back burner?

topjohnwu commented 5 years ago

The Magisk building system is complicated and I'm not so sure about integrating directly into AOSP. Once Magisk actually support being installed with GSI images, I might investigate on how to embed prebuilt Magisk binaries easily with the build system

Geofferey commented 4 years ago

In regards to this even tho it is old and has been closed. I have successfully integrated magisk with my builds so I could enable DM-VERITY.

Here are the changes you need to make...

A modification to build/core/Makefile : https://github.com/Geofferey/omni_android_build/commit/d60958780e6b26d7cb0cec5939b82df3df74a68f

A repo to copy magiskinit to PRODUCT_OUT : https://github.com/Geofferey/android_vendor_magisk

This will not build Magisk init from source with ROM, it only gets a prebuilt binary inside root of recovery RAMDISK during the build. The result is the same, a ROM with Magisk capable of re-locking and retaining AVB/DM-VERITY. It should work for devices that use recovery as boot, system as root. As for GSI's, I am building what I call hybrid GSI's, main difference being custom kernel so this won't be possible for full GSI.

The commit below may be useful for those trying to include prebuilt patched boot.imgs which you may want if building full GSI. You will need to append an AVB hashtree to end of patched image with avbtool to passthrough if enabling AVB.

https://github.com/Geofferey/omni_android_build/commit/78286a9c1dde3818b5f6e3276b4a53930cfe738b

A couple notes:

If building ROM with Magisk integrated and AVB fully enabled with hash/hashtree verification you may experience bowls of bootloops. In my experience the trick is to initially flash your build with AVB enabled, re-locked and Magisk excluded from ROM. After first boot attempt to perform an OTA update with it included and you may be good to go. AFAIK I'm one of the first people to attempt this method and others have tried but seemingly failed. Hopefully this is the key for you folks. If you do try this and succeed please comment on the commits so I know that it's possible for others and not just some extravagant miracle.

This solution is completely deprecated on Android 11 and no longer works. Use steadfasterX's solution instead, it's amazing!

steadfasterX commented 3 years ago

I wanted to have an almost fully compatible solution which is hopefully future proof so Geofferey's solution was not sufficient for my needs (no offense! just its not what I needed).

So I made a complete different approach and the result has been integrated into my extendrom project where you can easily (once you got the details) root your ROM within the AOSP build process.

It will always download Magisk itself, unpacks it, and use the binaries within to patch automatically the boot.img with official(!) Magisk scripts (not just replacing init).

All the details and how it needs to be setup can be found here:

  1. setup extendrom: https://github.com/sfX-android/android_vendor_extendrom/
  2. setup pre-rooting: https://github.com/sfX-android/android_vendor_extendrom/wiki/extendrom_flags#extendrom_preroot_boot

It may look overwhelming first but it is flexible and fits all my needs while staying at close as possible to Magisk itself.

The main goal for me was having a rooted ROM while keeping full AVB / dm-verity support on my bootloader locked OnePlus device.

maybe that's useful for someone else so I wanted to add it here.

corrmaan commented 3 years ago

Thanks @steadfasterX for posting your solution. I worked on a similar task of getting Magisk integrated to a LineageOS build with OpenGApps and AVB/dm-verity support. Not to hijack your post, but if anyone is interested please check it out here: https://github.com/corrmaan/docker-lineage-cicd.

It's forked from https://github.com/lineageos4microg/docker-lineage-cicd with other code from https://github.com/dan-v/rattlesnakeos-stack, and is mainly tailored to my Pixel 2, but maybe others will find it useful.

mohrezaei commented 2 years ago

I just documented another solution which was inspired by @steadfasterX, with the following differences:

corrmaan commented 2 years ago

@mohrezaei That's a cool way to do it!

steadfasterX commented 2 years ago

I just documented another solution which was inspired by @steadfasterX, with the following differences:

  • No need to use modified Magisk scripts
  • Works during signing, so no need to add/change anything inside the build tree

nice stuff! unfortunately it does not work inside the AOSP build process as when the patch script gets executed the binaries get denied on newer Android versions (https://sx.ix5.org/info/post/android-q-changes/ -> PATH restrictions)

and just for info: my approach has changed and uses the magisk scripts completely untouched now ;)

lxy0797 commented 2 years ago

I just documented another solution which was inspired by @steadfasterX, with the following differences:

  • No need to use modified Magisk scripts
  • Works during signing, so no need to add/change anything inside the build tree

nice stuff! unfortunately it does not work inside the AOSP build process as when the patch script gets executed the binaries get denied on newer Android versions (https://sx.ix5.org/info/post/android-q-changes/ -> PATH restrictions)

and just for info: my approach has changed and uses the magisk scripts completely untouched now ;)

Could u tell me the new method? I want to have a try.thx

steadfasterX commented 2 years ago

I just documented another solution which was inspired by @steadfasterX, with the following differences:

  • No need to use modified Magisk scripts
  • Works during signing, so no need to add/change anything inside the build tree

nice stuff! unfortunately it does not work inside the AOSP build process as when the patch script gets executed the binaries get denied on newer Android versions (https://sx.ix5.org/info/post/android-q-changes/ -> PATH restrictions) and just for info: my approach has changed and uses the magisk scripts completely untouched now ;)

Could u tell me the new method? I want to have a try.thx

if that q was for me then read: https://github.com/topjohnwu/Magisk/issues/509#issuecomment-911720167

akash07k commented 1 year ago

My Device is Virtual A/B and launched with Android 12. It doesn't have separate boot partition. Any way to make your solution work with my device? I wan't to integrate magisk right in the rom

I just documented another solution which was inspired by @steadfasterX, with the following differences:

  • No need to use modified Magisk scripts
  • Works during signing, so no need to add/change anything inside the build tree

nice stuff! unfortunately it does not work inside the AOSP build process as when the patch script gets executed the binaries get denied on newer Android versions (https://sx.ix5.org/info/post/android-q-changes/ -> PATH restrictions) and just for info: my approach has changed and uses the magisk scripts completely untouched now ;)

Could u tell me the new method? I want to have a try.thx

if that q was for me then read: #509 (comment)

steadfasterX commented 1 year ago

My Device is Virtual A/B and launched with Android 12. It doesn't have separate boot partition. Any way to make your solution work with my device? I wan't to integrate magisk right in the rom

I see no reason why it should not work but I cannot test it as I do not have a device using virtual A/B