How to boot system-as-root device always as rooted?


Question

I have a Samsung S10 5G device that is actually a system-as-root device. I have rooted it by following the instructions given here.



As the system-as-root devices have no ramdisk in boot.img but in recovery.img so, I have to always boot the device with a recovery combination to get the root access.



Is there any way to achieve the root on a normal reboot?



I have a full ROM image of the device so, I believe there should be some way. Please share your thoughts.


Answer

If you go through How Magisk works and How to manually root a phone it would be easy for you to figure out why it's impossible to achieve what you are looking for.



init is the very first process started by kernel, which at its early stage sets SELinux enforcing (if it's not already enforcing). Before that happens, we need to modify SELinux policy so that it doesn't restrict the root daemon (magiskd) from running. Also init.rc needs to be patched to inject magiskd service.



A simple option is to patch /sepolicy and modify /init.rc when rooting the device and save the files on device. But on system-as-root devices both files are on system.img (and possibly vendor.img), not on boot.img. So modifying them would break "systemless" approach and other related things, in particular dm-verity and OTAs.



Another option is to run a process even before init which patches SELinux policy and changes init.rc on the go. And the easy way to achieve this is to replace original init file with Magisk init so that kernel executes the latter. However on SAR devices init is also on system.img, so we can't replace it. But we can always boot to recovery mode which does have a ramdisk where Magisk's init can be placed and executed before any other process. On A/B SAR devices recovery ramdisk is in boot.img, so Magisk always boots to recovery by forcing kernel to ignore skip_initramfs cmdline parameter received from bootloader.




Is there any way to achieve the root on a normal reboot?




On non-A/B SAR devices (like yours) there is no ramdisk in boot.img, so the only option to retain systemless approach is to place Magisk init in recovery ramdisk on recovery partition, and always boot to recovery mode. From there system.img is mounted at /system_root by Magisk init, contents of ramdisk are then copied to / cleaning everything previously existing, files are added / modified in rootfs /, /system_root/system is bind-mounted to /system, and finally [/system]/init is executed for normal boot.



However things have changed with Android 10, now system.img is mounted at / but the files to be added / modified like /init.rc and /sbin are overlaid with bind mounts as tweeted by Magisk developer.


Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes