[Q] where to put dynamically loadable modules for SM-N900P? - Galaxy Note 3 Developer Discussion [Developers Onl

Greetings-
I was able to successfully flash my phone with a test dynamic loadable module. I am running into some issues, which I wonder if I can ask the group how to fix. When I run find, I see:
Code:
[email protected]:/ # find / -name "*.ko"
find / -name "*.ko"
/lib/modules/scsi_wait_scan.ko <<<<<<
/lib/modules/hello1.ko <<<<<<<<<
I then tried to load hello1, and get the following:
Code:
1|[email protected]:/ # modprobe /lib/modules/hello1.ko
modprobe /lib/modules/hello1.ko
modprobe: can't change directory to '3.4.0-g7e6fbf7-dirty': No such file or directory
Does anyone know why the kernel version number has -dirty at the end? And if I am using modprobe correctly?
I was able to use insmod successfully it seems. But I can not run the files. How do I run these modules?
Here is the output:
Code:
1|[email protected]:/ # insmod /lib/modules/hello1.ko
insmod /lib/modules/hello1.ko
[email protected]:/ # hello
hello
tmp-mksh: hello: not found
What is tmp-mksh?
I was told insmod uses .ko files as output, but it seems like it want .o file?
Code:
127|[email protected]:/ # insmod
insmod
usage: insmod <module.o>
lsmod was the first real evidence I had that the modules went in ok. I now have to figure out what is 578, and why does say Live 0x00000000? What address is that?
Code:
255|[email protected]:/ # lsmod
lsmod
hello1 578 - - Live 0x00000000

Related

Is there a .bashrc equivalent for android?

See title. Mostly I just want to have some aliases set every time I open terminal emulator or adb shell. Does anyone know if this is possible?
Just dig /init.rc then you may find the way.
biAji said:
Just dig /init.rc then you may find the way.
Click to expand...
Click to collapse
I don't think that works, init.rc looks like it's executed on boot... I'm looking for a file that executes every time a shell is started.
Hi there,
I'm wondering the same thing.
I've tried to create a bashrc
(/.bashrc ; /system/etc/bash.bashrc ; /root/.bashrc ) But none is working.
I'm about to try a different way,
I've cp /system/bin/sh to /system/bin/sh1
then I'll try to replace /system/bin/sh by a shell script that will launch my aliases then sh1
Something like:
Code:
#!/system/bin/sh1
alias lsa='ls -Alh '
[...]
/system/bin/sh1
But i'm not pretty sure It could work properly. Firstly because I remember a gain-super-user manipulation that requires to copy and modify rights on sh. (but it could be ok if the script and the copy of sh have the same rights).
Secondly, because I'm not really OK with this; When I'll remove /system/bin/sh in order to replace it, I'll have no shell for a time (if it crashes, I'll not be able to use sh anymore, even with adb).
So if anybody have an other solution it could be damn cool
Thank you all
EDIT: I've just tried this. It's ok, I've not lost my shell, but the modifications (one alias, and a path export), are not applied.
There is no such file. In fact, the default shell is sh, not bash.
However, there is a solution. Read my post in this thread: http://forum.xda-developers.com/showthread.php?t=518959
lbcoder said:
There is no such file. In fact, the default shell is sh, not bash.
However, there is a solution. Read my post in this thread: http://forum.xda-developers.com/showthread.php?t=518959
Click to expand...
Click to collapse
That is interesting. I'll have to try it. I assume it won't work with ADB? Or am I wrong (please say yes)?
I would have thought that it would work with adb, but when I try this:
adb shell /path/to/bash --rcfile /path/to/bashrc
it seems that its a 1-way shell...
well you can always run bash after connecting....
adb shell
/path/to/bash --rcfile /path/to/bashrc
Alternatively, with a little bit of craftiness, you may be able to replace the sh binary with a script that runs bash.
Sweet, can't wait until I get home.
The best way, and what a lot of the ROM devs are doing now is putting a check/call to /system/init.rc and /data/init.rc so we can add our own customizations. This would include boot time chmod/chowns or aliases if need be. Hell we could put a call to /etc/.profile if we wanted and put all our aliases in there. But we can't do any of that till ROM devs put that call in boot.img->boot.ramdisk->init.rc
Android's default shell /system/bin/sh is a link to mksh in the same directory. It reads /system/etc/mkshrc & ~/.mkshrc
I think it also reads ~/.profile & /etc/profile but I'm not sure & I don't know what order.
See https://www.mirbsd.org/mksh.htm for the documentation.
Also worth noting is that most, if not all, shells read /etc/profile & ~/.profile so anything in there should be very general.
Yes there is
lbcoder said:
There is no such file. In fact, the default shell is sh, not bash.
However, there is a solution. Read my post in this thread: http://forum.xda-developers.com/showthread.php?t=518959
Click to expand...
Click to collapse
Edit /system/etc/mkshrc. You can add your aliases there after the defaults. Type 'alias' from the terminal to see the default aliases that are defined in this file. I believe this file is the master, and when a shell is envoked, the mksh command pipes a hidden copy of this file into the users home directory as .mkshrc for the terminal session.
Also, this is the file to edit to append to your $PATH.
alanthehat said:
Android's default shell /system/bin/sh is a link to mksh in the same directory. It reads /system/etc/mkshrc & ~/.mkshrc
I think it also reads ~/.profile & /etc/profile but I'm not sure & I don't know what order.
See https://www.mirbsd.org/mksh.htm for the documentation.
Also worth noting is that most, if not all, shells read /etc/profile & ~/.profile so anything in there should be very general.
Click to expand...
Click to collapse
/system/etc/mkshrc & ~/.mkshrc
You had the answer right here but you didn't know it
smasraum said:
That is interesting. I'll have to try it. I assume it won't work with ADB?
Click to expand...
Click to collapse
curiously enough my bash aliases work when in the adb shell. I'm running bash on my mac. I guess the alias get expanded by bash before there are sent to adb shell.
Code:
mac $ alias ll
alias ll='\ls -albhFG'
mac $
Code:
mac $ adb shell
[email protected]_a11chl:/ $ ll
drwxr-xr-x root root 2016-10-03 13:10 acct
lrwxrwxrwx root root 2016-10-03 13:24 busybox -> /data/data/com.jrummy.app.managerfree/files/busybox
drwxrwx--- system cache 2016-10-03 14:37 cache
drwxrwx--x system carrier 2016-09-02 15:24 carrier
dr-x------ root root 2016-10-03 13:10 config
Please note this does not work:
Code:
mac $ adb shell ll
/system/bin/sh: ll: not found
On my side, here is what I did :
Code:
adb root
Code:
adb remount
Code:
adb shell
Code:
vim /etc/mkshrc
I added the following line :
Code:
alias ls='ls --color=auto'
Results attached, now I may play with the PS1 to get colors in the prompt....
In nougat, there is a bashrc in
"/system/etc/bash/bashrc"
idk about other versions check and tell me
Code:
uname -a: Linux debian 4.9.0-8-amd64 #1 SMP Debian 4.9.110-3+deb9u5 (2018-09-30) x86_64 GNU/Linux
[email protected]:/# adb root #restart adb with root permissions
[email protected]:/# adb remount #remount partitions on device read-write
remount succeeded
[email protected]:/# adb shell #run remote, interactive shell
device:/ # test0 #execute command test0
/system/bin/sh: test0: not found
127|device:/ # alias test0='date'
device:/ # test0
Wed Oct 10 15:06:16 +00 2018 #SUCCESS
[email protected]:/# adb root
adbd is already running as root
[email protected]:/# adb remount
remount succeeded
[email protected]:/# adb shell
klteusc:/ # test1
/system/bin/sh: test1: not found
127|klteusc:/ # echo "alias test1='date'">>/system/etc/mkshrc
klteusc:/ # test1
/system/bin/sh: test1: not found
127|klteusc:/ # exit
[email protected]:/# adb shell
klteusc:/ # test1
Wed Oct 10 15:10:41 +00 2018
klteusc:/ # exit
[email protected]:/# adb root
adbd is already running as root
[email protected]:/# adb remount
remount succeeded
[email protected]:/# adb shell
klteusc:/ # test2
/system/bin/sh: test2: not found
127|klteusc:/ # echo "alias test2='date'">>/system/etc/bash/bashrc
klteusc:/ # test2
/system/bin/sh: test2: not found
127|klteusc:/ # exit
[email protected]:/# adb shell
klteusc:/ # test2
/system/bin/sh: test2: not found
dirtygardner said:
Code:
uname -a: Linux debian 4.9.0-8-amd64 #1 SMP Debian 4.9.110-3+deb9u5 (2018-09-30) x86_64 GNU/Linux
[email protected]:/# adb root #restart adb with root permissions
[email protected]:/# adb remount #remount partitions on device read-write
remount succeeded
[email protected]:/# adb shell #run remote, interactive shell
device:/ # test0 #execute command test0
/system/bin/sh: test0: not found
127|device:/ # alias test0='date'
device:/ # test0
Wed Oct 10 15:06:16 +00 2018 #SUCCESS
[email protected]:/# adb root
adbd is already running as root
[email protected]:/# adb remount
remount succeeded
[email protected]:/# adb shell
klteusc:/ # test1
/system/bin/sh: test1: not found
127|klteusc:/ # echo "alias test1='date'">>/system/etc/mkshrc
klteusc:/ # test1
/system/bin/sh: test1: not found
127|klteusc:/ # exit
[email protected]:/# adb shell
klteusc:/ # test1
Wed Oct 10 15:10:41 +00 2018
klteusc:/ # exit
[email protected]:/# adb root
adbd is already running as root
[email protected]:/# adb remount
remount succeeded
[email protected]:/# adb shell
klteusc:/ # test2
/system/bin/sh: test2: not found
127|klteusc:/ # echo "alias test2='date'">>/system/etc/bash/bashrc
klteusc:/ # test2
/system/bin/sh: test2: not found
127|klteusc:/ # exit
[email protected]:/# adb shell
klteusc:/ # test2
/system/bin/sh: test2: not found
Click to expand...
Click to collapse
You need to manually edit the mksh file and add your command in for it to persistently work.
Just running the command below will work but will be wiped on reboot.
Code:
alias test1='date'
If you need it to stay persistent over reboots manually edit /system/etc/mkshrc
Code:
## Adding the test0 alias via file editing:
taimen:/ # test0
Wed Aug 5 13:09:46 GMT 2020
## Adding test1 alias via command line:
taimen:/ # alias test1='date'
taimen:/ # test1
Wed Aug 5 13:10:06 GMT 2020
## Reboot Device
taimen:/ # reboot
## Check for persistant changes:
taimen:/ # test0
Wed Aug 5 13:04:24 GMT 2020
taimen:/ # test1
/system/bin/sh: test1: not found
Example file that I used:
Code:
# Copyright (c) 2010, 2012, 2013, 2014
# Thorsten Glaser <[email protected]>
# This file is provided under the same terms as mksh.
#-
# Minimal /system/etc/mkshrc for Android
#
# Support: https://launchpad.net/mksh
: ${HOSTNAME:=$(getprop ro.product.device)}
: ${HOSTNAME:=android}
: ${TMPDIR:=/data/local/tmp}
export HOSTNAME TMPDIR
alias test0="date"
if (( USER_ID )); then PS1='$'; else PS1='#'; fi
PS4='[$EPOCHREALTIME] '; PS1='${|
local e=$?
(( e )) && REPLY+="$e|"
return $e
}$HOSTNAME:${PWD:-?} '"$PS1 "
This will stay persistent on reboot.

Problem with compcache

Im running a userinit file for the cyanogen 3.91 rom and if tried to run it with now success. it just shows 000 on the swap when i run free. and when i try to run cat /proc/ramzswap it says that no such directory exists,
this is the userinit.sh
#!/system/bin/sh
# $Id: userinit.sh,v 1.2 2009/07/17 01:11:25 noah Exp $
# Do not change the #! above and expect that shell to invoke this script at
# boot, because a2sd forcibly calls this with /system/bin/sh.
PATH=/system/xbin/bb:$PATH
# Currently our busybox build uses the "small" modprobe and insmod,
# which don't support parameter arguments. (The usage string lies.)
uname_r=`uname -r`
moddir=`find /system/modules -type d -name $uname_r`
insmod=/system/bin/insmod
if $insmod $moddir/compcache/xvmalloc.ko &&
$insmod $moddir/compcache/ramzswap.ko disksize_kb=32768
then
sysctl -w vm.swappiness=40
mknod /dev/ramzswap0 b 253 0
swapon /dev/ramzswap0
fi
exit;
# eof
yeah dude im getting the same problem. Ive asked everywhere and no one can help. the only difference i notice are the two lines (exit; # eof) at the end of yours.
EDIT:
DUDE, just got it to work, this is what to do:
go here: http://forum.xda-developers.com/showthread.php?t=537236
download the text file.
open the text file delete everything and paste this in:
Code:
#!/system/bin/sh
# $Id: userinit.sh,v 1.2 2009/07/17 01:11:25 noah Exp $
# Do not change the #! above and expect that shell to invoke this script at
# boot, because a2sd forcibly calls this with /system/bin/sh.
PATH=/system/xbin/bb:$PATH
# Currently our busybox build uses the "small" modprobe and insmod,
# which don't support parameter arguments. (The usage string lies.)
uname_r=`uname -r`
moddir=`find /system/modules -type d -name $uname_r`
insmod=/system/bin/insmod
if $insmod $moddir/compcache/xvmalloc.ko &&
$insmod $moddir/compcache/ramzswap.ko disksize_kb=32768
then
sysctl -w vm.swappiness=40
mknod /dev/ramzswap0 b 253 0
swapon /dev/ramzswap0
fi
save it and close it
rename it userinit.sh and type this in adb:
Code:
adb push userinit.sh /system/sd
adb shell chmod 755 /system/sd/userinit.sh (not sure if needed being safe)
adb shell reboot
hope that helps
did you get it to work on 3.91?
ive tried everything and still swap is 0 0 0 0
I'm not an expert in the whole compcache thing I haven't followed it but doesn't "free" in terminal show swap, not compcache? I believe "cat /proc/ramzswap" shows compcache compression..
alritewhadeva said:
I'm not an expert in the whole compcache thing I haven't followed it but doesn't "free" in terminal show swap, not compcache? I believe "cat /proc/ramzswap" shows compcache compression..
Click to expand...
Click to collapse
"free" shows the swap file total, used and free. ramzswap goes more indepth
ubernicholi said:
"free" shows the swap file total, used and free. ramzswap goes more indepth
Click to expand...
Click to collapse
ah ok. Thanks for the clarification.
i tried the cat /proc/ramzswap and it said the file or directory could not be found
Compache DOES NOT WORK ON 3.9.1

Xiaomi Mi A2, auto reboot on usb power on

Hello people,
I need a xiaomi phone to boot when usb power is turned on.
Stock boot image
Android 9 PKQ1.180904.001.V10.0.8.0.PDIMIXM
What I tried so far:
#1 Battery animation file
I found some solutions for samsung smartphones modifing charger animation files.
system/bin/lpd
system/bin/lpod
--> Did not find such file. My /System/bin/ contains:
Code:
acpi dd input mknod rmdir tc-wrapper-1.0
am debuggerd insmod mkswap rmmod tee
app_process dex2oat ionice mktemp run-as telecom
app_process32 dexdiag iorenice modinfo runcon time
app_process64 dexdump ip modprobe schedtest timeout
applypatch dexlist ip-wrapper-1.0 monkey screencap tinycap
appops df ip6tables more screenrecord tinymix
appwidget diff ip6tables-restore mount secdiscard tinypcminfo
atrace dirname ip6tables-save mountpoint secilc tinyplay
awk dmesg ip6tables-wrapper-1.0 move_time_data.sh sed toolbox
base64 dos2unix iptables mv sendevent top
basename dpm iptables-restore ndc sensors_test touch
bcc du iptables-save ndc-wrapper-1.0 sensorservice toybox
blockdev dumpsys iptables-wrapper-1.0 netstat seq tr
bmgr echo iw newfs_msdos service true
bu egrep keystore_cli_v2 nice setenforce truncate
bugreport env kill nl setprop tty
bugreportz expand killall nohup setsid tzdatacheck
bunzip2 expr ld.mc oatdump settings uiautomator
bzcat fallocate linker od sh ulimit
bzip2 false linker64 paste sha1sum umount
cal fgrep linker_asan patch sha224sum uname
cat file linker_asan64 patchoat sha256sum uniq
chcon find ln perfetto sha384sum unix2dos
chgrp flock load_policy pgrep sha512sum update_engine_client
chmod fmt locksettings pidof simg2img uptime
chown free log ping sleep usleep
chroot getenforce logcat ping6 sm uudecode
chrt getevent logname pkill sort uuencode
cksum getprop logwrapper pm split vmstat
clear grep losetup pmap start vr
cmd groups ls ppd stat wc
cmp gunzip lshal printenv stop which
comm gzip lsmod printf strings whoami
content head lsof ps stty wigig_logcollector
cp hid lspci pvclicense_sample svc wigig_remoteserver
cpio host_manager_11ad lsusb pwd swapoff wigig_wiburn
crash_dump32 hostname md5sum readlink swapon wm
crash_dump64 hw media realpath sync xargs
curl hwclock microcom reboot sysctl xxd
cut id mkdir renice tac yes
dalvikvm ifconfig mkfifo requestsync tail zcat
dalvikvm32 ime mkfs.ext2 resize2fs tar
dalvikvm64 incident mkfs.ext3 restorecon taskset
date inotifyd mkfs.ext4 rm tc
#2 Ram Disk modification
I pulled the /System/bin/reboot file using adb shell and extracted the boot.img using android kitchen 3.5 from the forum.
Verified that everything works by unpacking/repacking/flasing boot image.
https://forum.xda-developers.com/showthread.php?t=2073775
- replaced the /ramdisk/sbin/charger file with the reboot file
--> battery animation is still there but nothing happens
- modified the charger file according to this discussion: https://forum.xda-developers.com/ga...-os-auto-boot-charge-connected-t3626364/page2
--> battery animation gone but stuck at google logo
- modified /ramdisk/init.rc and added:
on charger
trigger late-init
--> battery animation gone but stuck at google logo
- deleted /sbin/charger and charger service in init.rc and flashed image to both boot slots
--> changes nothing, battery animation will still show up.
- searched for battery/animation files / images in the ramdisk, but there seems to be nothing.
What else could I try?
Why isnt it working as expected?
Would flashing LineageOS or getting root access change anything?
UPDATE
--> here you can find a solution
[Q] Power on when plugged in (boot when docked)?
Many Greetings & Thanks alot,
himi

How To Guide How to backup the data from the phone using rsync and ssh (including some hints for using sshd on an Android phone)

How to backup the data from the phone using rsync and ssh (including some hints for using sshd on an Android phone)
Like for all computer it's important to have a backup of the data on the phone.
For those who like me don't like to store their private data in one of the suspicious clouds there is a solution with standard Linux tools:
Use rsync and ssh to backup the data from the phone to your local workstation (see the man page for rsync for details regarding rsync and why it is useful for this task)
The neccessary tools for Android for this method can be installed with the Magisk Module MagiskSSH.
Download the Magisk Module with MagiskSSH from here
https://gitlab.com/d4rcm4rc/MagiskSSH_releases
Copy the ZIP file with the Magisk Module to the phone :
Code:
adb push magisk_ssh_v0.14.zip /sdcard/Download/
and install it via the module installation from within the Magisk App or manuell using :
Code:
adb shell su - -c /data/adb/magisk/magisk64 --install-module /sdcard/Download/magisk_ssh_v0.14.zip
Sample output of the installation:
Code:
ASUS_I006D:/ # /data/adb/magisk/magisk64 --install-module /sdcard/Download/magisk_ssh_v0.14.zip
- Current boot slot: _a
- Device is system-as-root
*******************************
OpenSSH for Android
*******************************
[0/7] Preparing module directory
[1/7] Extracting architecture unspecific module files
[2/7] Extracting libraries and binaries for arm64
[3/7] Configuring library path wrapper
[4/7] Recreating symlinks
[5/7] Creating SSH user directories
[6/7] Found sshd_config, will not copy a default one
[7/7] Cleaning up
- Setting permissions
- Done
ASUS_I006D:/ #
A reboot is required now.
Code:
adb shell reboot
For the next tasks open an adb shell and become root user.
Next create the authorized_keys file for the user root :
Code:
touch /data/ssh/root/.ssh/authorized_keys
chmod 600 /data/ssh/root/.ssh/authorized_keys
and add your public ssh key to the file /data/ssh/root/.ssh/authorized_keys.
To make sure that the keys and other data files for the MagiskSSH module are not removed while deinstalling the module you should create the file /data/ssh/KEEP_ON_UNINSTALL:
Code:
touch /data/ssh/KEEP_ON_UNINSTALL
The MagiskSSH module also installs a service to start sshd after each reboot: to disable this start create the file /data/ssh/no-autostart:
Code:
touch /data/ssh/no-autostart
To manually start or stop the sshd use the script /data/adb/modules/ssh/opensshd.init :
Code:
# start the sshd (as user root)
#
/data/adb/modules/ssh/opensshd.init start
# to stop the sshd (as user root)
#
/data/adb/modules/ssh/opensshd.init stop
Now test the access via ssh from your Linux workstation:
Code:
ssh -l root <phone_ip_address> id
Use this command to retrieve the current IP address of the phone:
Code:
PHONE_IP_ADDRESS=$( adb shell ifconfig wlan0 | grep "inet addr:" | sed -e "s/.*inet addr://g" -e "s/[[:space:]]*Bcast.*//g" )
example :
Code:
[[email protected] ~]$ ssh -l root ${PHONE_IP_ADDRESS} id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
[[email protected] ~]$
Now you can use rsync to backup the data from the phone, e.g. to backup the photos from the phone do :
Code:
# on your local Linux workstation do:
# start the sshd on the phone via adb if not already running
#
adb shell su - -c /data/adb/modules/ssh/opensshd.init start
# retrieve the current IP address from the phone
#
PHONE_IP_ADDRESS=$( adb shell ifconfig wlan0 | grep "inet addr:" | sed -e "s/.*inet addr://g" -e "s/[[:space:]]*Bcast.*//g" )
# backup the new photos from the phone to the Linux workstation (rsync only copies new files from the phone)
# to the local directory /data/backup/ASUS_ZENFONE8/DCIM
#
rsync -av --rsync-path /data/adb/modules/ssh/usr/bin/rsync [email protected]${PHONE_IP_ADDRESS}:/sdcard/DCIM/ /data/backup/ASUS_ZENFONE8/DCIM
# optional stop the sshd on the phone via adb
#
adb shell su - -c /data/adb/modules/ssh/opensshd.init stop
Note: The sshd configuration file used is /data/ssh/sshd_config
Sample Script to backup all data in the directory /sdcard
Code:
##!/bin/bash
#
# simple script to backup the data of an phone using adb, ssh, and rsync
#
# History
# 27.06.2022 /bs
# initial release
#
# for testing
#
#RSYNC_OPTIONS="${RSYNC_OPTIONS} --dry-run"
RSYNC_OPTIONS="${RSYNC_OPTIONS} --del "
# default is to backup the phone connected via adb over LAN
#
[ $# -ne 0 ] && ADB_OPTIONS="$*" || ADB_OPTIONS="-e"
# retrieve the serial number of the attached phone
#
SERIAL_NO="$( adb ${ADB_OPTIONS} shell getprop ro.serialno )"
if [ "${SERIAL_NO}"x = ""x ] ; then
echo "ERROR: Can not read the serial number of the connected phone"
exit 89
fi
VENDOR_MODEL="$( adb ${ADB_OPTIONS} shell getprop ro.product.vendor.model )"
# directory for the backup
#
BACKUP_DIR="/data/backup/ASUS_ZENFONE8/data_backup/${VENDOR_MODEL}_${SERIAL_NO}"
if [ ! -d "${BACKUP_DIR}" ] ; then
echo "ERROR: The directory \"${BACKUP_DIR}\" does not exist"
exit 99
fi
PHONE_IP_ADDRESS="$( adb ${ADB_OPTIONS} shell ifconfig wlan0 | grep "inet addr:" | sed -e "s/.*inet addr://g" -e "s/[[:space:]]*Bcast.*//g" )"
if [ "${PHONE_IP_ADDRESS}"x = ""x ] ; then
echo "ERROR: Can not detect the IP address of the phone"
exit 100
fi
echo "Updating a backup of the data on the phone with the serial number \"${SERIAL_NO}\" and the IP \"${PHONE_IP_ADDRESS}\" to the directory \"${BACKUP_DIR}\" ..."
set -x
# start the sshd if neccessary
#
adb ${ADB_OPTIONS} shell su - -c /data/adb/modules/ssh/opensshd.init start
# do the backup
#
time rsync ${RSYNC_OPTIONS} -av --rsync-path /data/adb/modules/ssh/usr/bin/rsync [email protected]${PHONE_IP_ADDRESS}:/sdcard/ "${BACKUP_DIR}/"
# stop the sshd
#
adb ${ADB_OPTIONS} shell su - -c /data/adb/modules/ssh/opensshd.init stop
set +x
How to enable access via ssh for non-root user
In the standard configuration installed by MagiskSSH ssh access is only allowed as user root because the ssh keys are in the directory /data and all non-root user can not read files in the directory /data. Therefor some efforts are neccessary to add ssh access for non-root user.
e.g. To enable the ssh access for the user shell do:
To configure ssh access for the user shell we must create a .ssh directory for the user shell in a directory tree owned by the user shell. The only directory on the phone owned by the user shell that can be used for this purpose is /storage :
Code:
ASUS_I006D:/ # ls -ld /storage
drwx--x--- 4 shell everybody 80 2022-06-26 18:37 /storage
ASUS_I006D:/ #
But unfortunately all files and directories in this directory are temporary and will be deleted after a reboot of the phone.
Therefor we configure a startup script in Magisk to create this directory tree after each reboot, e.g.
/data/adb/service.d/create_ssh_dir_for_shell.sh:
Code:
# /data/adb/service.d/create_ssh_dir_for_shell.sh
#
mkdir -p /storage/shell/.ssh
chmod -R 700 /storage/shell/
touch /storage/shell/.ssh/authorized_keys
echo "<ssh_public_key>" > /storage/shell/.ssh/authorized_keys
chmod 600 /storage/shell/.ssh/authorized_keys
chown -R shell:shell /storage/shell
Make the script executable:
Code:
su - -c chmod +x data/adb/service.d/create_ssh_dir_for_shell.sh
To test the script just execute it one time manually as user root.
Code:
su - -c sh data/adb/service.d/create_ssh_dir_for_shell.sh
Now create a backup of the sshd config file
Code:
su - -c cp /data/ssh/sshd_config /storage/ssh/sshd_config.org.$$
and add these lines at the end of the file /data/ssh/sshd_config
Code:
Match User shell
AuthorizedKeysFile /storage/shell/.ssh/authorized_keys
Restart the sshd if it's already running
Now test the access as user shell, example:
Code:
[[email protected] ~]$ ssh -l shell 192.168.1.148 id
uid=2000(shell) gid=2000(shell) groups=2000(shell) context=u:r:magisk:s0
[[email protected] ~]$
The reason for this config is the setting "StrictMode yes" in the sshd config file /data/ssh/sshd_config (see the man page for sshd_config for details). So another "solution" is to change this setting:
With the setting "StrictModes no" in the file sshd_config the directory with the authorized_keys file for the non-root users can be anywhere (for example in /sdcard/shell)
Execute as user root:
Code:
sed -i -e "s/.*StrictModes.*//g" -e "s/UsePrivilegeSeparation/StrictModes no\nUsePrivilegeSeparation/g" /data/ssh/sshd_config
and change the entry in the file /data/ssh/sshd_config for the authorized_keys file for the user shell, for example:
Code:
Match User shell
AuthorizedKeysFile /sdcard/shell/.ssh/authorized_keys
Afterwards restart the sshd:
Code:
/data/adb/modules/ssh/opensshd.init stop
/data/adb/modules/ssh/opensshd.init start
Now create the directories and files neccessary for the ssh access (see above) in the directory /sdcard/shell:
Code:
SUS_I006D:/ # find /sdcard/shell -exec ls -ld {} \;
drwxrws--- 3 u0_a118 media_rw 3452 2022-06-26 18:32 /sdcard/shell
drwxrws--- 2 u0_a118 media_rw 3452 2022-06-26 18:32 /sdcard/shell/.ssh
-rw-rw---- 1 u0_a118 media_rw 408 2022-06-26 18:32 /sdcard/shell/.ssh/authorized_keys
ASUS_I006D:/ #
and the access as user shell via ssh should work

How To Guide How to run a script at shutdown

How to run a script at shutdown
To define additional startup scripts via Magisk the Magisk directories /data/adb/service.d and /data/adb/post-fs-data.d can be used. Unfortunately there is no equivalent for scripts that should be executed during shutdown.
So we must use other methods to implement these kind of scripts.
Using the overlay feature of Magisk to run a script at shutdown
Introduction
in Android it is possible to define actions that will be executed when certain conditions are satisfied.
These definitions are done in the file init.rc (and other .rc files) using the Android Init Language.
And this feature can be used to execute a command when the phone is shutting down.
Note:
For details about the Android Init Language used for these files see here https://android.googlesource.com/platform/system/core/+/master/init/README.md
The .rc files used by Android are in the directories
/system/etc/init​/vendor/etc/init​/odm/etc/init​
Note: The first .rc file read is /system/etc/init/hw/init.rc
Unfortunately it's useless to change the .rc files in these directories using the Magisk features to change files in the directory /system because these files are processed by the OS before the new files are "created" by Magisk.
Therefor the overlay functionality from Magisk must be used to create additional .rc files (see the section Root Directory Overlay System on this page https://github.com/topjohnwu/Magisk/blob/master/docs/guides.md for details about this Magisk Feature).
Preparation
To be able to restore the original boot partition in case of an error create an image of the original boot partition from the phone on your PC before starting the development:
Code:
CUR_SLOT=$( adb shell getprop ro.boot.slot_suffix )
adb shell su - -c dd if=/dev/block/by-name/boot${CUR_SLOT} | cat >boot${CUR_SLOT}
e.g.
Code:
[ OmniRomDev - [email protected] /data/develop/android/test ] $ CUR_SLOT=$( adb shell getprop ro.boot.slot_suffix )
[ OmniRomDev - [email protected] /data/develop/android/test ] $ echo ${CUR_SLOT}
_b
[ OmniRomDev - [email protected] /data/develop/android/test ] $
[ OmniRomDev - [email protected] /data/develop/android/test ] $ adb shell su - -c dd if=/dev/block/by-name/boot${CUR_SLOT} | cat >boot${CUR_SLOT}.img
196608+0 records in
196608+0 records out
100663296 bytes (96 M) copied, 2.668147 s, 36 M/s
[ OmniRomDev - [email protected] /data/develop/android/test ]
[ OmniRomDev - [email protected] /data/develop/android/test ] $ ls -ltr boot${CUR_SLOT}.img
-rw-r--r--. 1 xtrnaw7 xtrnaw7 100663296 Oct 1 12:13 boot_b.img
[ OmniRomDev - [email protected] /data/develop/android/test ] $
To trouble shoot issues with this approach it is highly recommended to create an Magisk init script in the directory
/data/adb/post-fs-data.d
to fetch and store the Android logs into a persistent file. Use these commands to create the script:
Code:
cat >/data/adb/post-fs-data.d/0002logcatboot <<-EOT
mkdir -p /cache/logs
# backup the OS logs from before the reboot:
#
[ -r /cache/logs/log ] && mv /cache/logs/log /cache/logs/oldlog
/system/bin/logcat -r 102400 -n 9 -v threadTime -f /cache/logs/log >/cache/logs/info.log 2>/cache/logs/err.log &
EOT
chmod 755 /data/adb/post-fs-data.d/0001logcatboot
Using this script the log messages from before the last reboot are stored in the file /cache/logs/oldlog.
To activate the script the phone must be rebooted.
Check the contents of the directory /cache/logs/log after the reboot as user root to be sure that it works.
Code:
[email protected]_I006D:/ $ su - -c ls -ltr /cache/logs
total 205008
-rw-rw-rw- 1 root root 0 1970-01-06 08:16 info.log
-rw-rw-rw- 1 root root 0 1970-01-06 08:16 err.log
-rw-r----- 1 root root 4707523 2022-10-01 17:29 log
[email protected]_I006D:/ $
Details
The trigger in the .rc files for the action that should be done while shutting down is
on shutdown
The trigger can be used more then once; the OS will execute all defined actions for the trigger in the order they are found in the rc files.
The action to run an executable in the .rc file is
exec [ <seclabel> [ <user> [ <group>\* ] ] ] -- <command> [ <argument>\* ]
Fork and execute command with the given arguments. The command starts after “--” so that an optional security context, user, and supplementary groups can be provided. No other commands will be run until this one finishes. seclabel can be a - to denote default. Properties are expanded within argument. Init halts executing commands until the forked process exits.
Click to expand...
Click to collapse
In Android SELinux is enabled by default. Therefor it's neccessary to use the correct SELinux context for the files used.
(Note: The SELinux context for the init process executing the action is u:r:init:0 )
It's quite difficult to find the correct SELinux contexts in Android for this approach therefor it's better to use the general SELinux context defined by Magisk: u:r:magisk:s0 .
Implementation
Note:
All commands must be done as user root in an session on the phone or in an adb session.
So first create the neccessary directories and files:
Code:
mkdir -p /data/init_scripts
mkdir -p /data/init_scripts/log
Create the script to execute on shutdown:
Code:
cat >/data/init_scripts/my_shutdown.sh <<-\EOT
#!/system/bin/sh
SHUTDOWN_LOG="/data/init_scripts/log/myshutdown.$$.log"
echo "$0: Shutdown with parameter \"$*\" started at $( date ) " >>${SHUTDOWN_LOG}
echo "*** id : " >>${SHUTDOWN_LOG} 2>&1
id >>${SHUTDOWN_LOG} 2>&1
# ... add necessary commands ...
EOT
chmod 755 /data/init_scripts/my_shutdown.sh
Correct the SELinux context:
Code:
chcon -R u:r:magisk:s0 /data/init_scripts/
Check the result
Code:
[email protected]_I006D:/ # find /data/init_scripts/ -exec ls -ld {} \;
drwxr-xr-x 3 root root u:r:magisk:s0 3452 2022-10-01 16:12 /data/init_scripts/
-rwxr-xr-x 1 root root u:r:magisk:s0 637 2022-10-01 16:12 /data/init_scripts/my_shutdown.sh
drwxr-xr-x 2 root root u:r:magisk:s0 3452 2022-10-01 16:16 /data/init_scripts/log
[email protected]_I006D:/ #
Create a working directory:
Code:
#
# create a working directory
#
mkdir -p /data/adb/workdir
cd /data/adb/workdir
Now create the additional .rc file:
Code:
#
# change the current directory to the working directory
#
cd /data/adb/workdir
cat >init.custom.rc <<-\EOT
on shutdown
exec u:r:magisk:s0 -- /system/bin/sh /data/init_scripts/my_shutdown.sh 0008
on early-init
setprop my_custom_rc_file loaded
EOT
Note:
The additional trigger for early-init is for testing the new .rc file (see the trouble shooting section below for details). Magisk supports more then one .rc file; the name of the .rc file is meaningless but the extension must be .rc.
And now add the new file to the ramdisk on the boot partition:
Code:
#
# change the current directory to the working directory
#
cd /data/adb/workdir
# get the current active slot
#
CURRENT_SLOT=$( getprop ro.boot.slot_suffix )
echo "The current active slot is: ${CURRENT_SLOT}"
# copy the boot partition from the active slot to a file
#
dd if=/dev/block/by-name/boot${CURRENT_SLOT} of=./boot_root.img
# unpack the image file
#
/data/adb/magisk/magiskboot unpack ./boot_root.img
# add the new dirs and files to the ramdisk from the boot partition
#
/data/adb/magisk/magiskboot cpio ramdisk.cpio \
"mkdir 0700 overlay.d" \
"add 0700 overlay.d/init.custom.rc init.custom.rc"
# recreate the image file for the boot partition
#
/data/adb/magisk/magiskboot repack boot_root.img
# write the corrected image file to the boot partition
#
dd if=./new-boot.img of=/dev/block/by-name/boot${CURRENT_SLOT}
Note:
The commands to unpack and pack the ramdisk manually using the cpio command are (if NOT using the Magisk binary magiskboot):
Code:
RAMDISK=$PWD/ramdisk
mkdir ${RAMDISK}
cd ${RAMDISK}
# unpack the ramdisk
#
cpio -idm <../ramdisk.cpio
# ... do what ever is necessary with the files/dirs in ${RAMDISK}
# pack the ramdisk again
#
cd ${RAMDISK}
find . | cpio -o >../ramdisk.cpio
Now reboot the phone to activate the new .rc config and after the reboot check that the .rc file was processed
Code:
getprop my_custom_rc_file
e.g
Code:
[email protected]_I006D:/ $ getprop my_custom_rc_file
loaded
[email protected]_I006D:/ $
If the property defined in the .rc file, my_custom_rc_file, is not set something went wrong and you should check the OS logs and double check your config.
If the new property is defined you can test the shutdown action by rebooting the phone again.
While doing this reboot the new shutdown script should be executed and after the reboot is done there should be the log file from the shutdown script:
Code:
[email protected]_I006D:/ $ su -
[email protected]_I006D:/ # ls -l /data/init_scripts/log
total 0
-rw------- 1 root root 179 2022-10-01 18:23 myshutdown.4617.log
[email protected]_I006D:/ # cat /data/init_scripts/log/myshutdown.4617.log
/data/init_scripts/my_shutdown.sh: Shutdown with parameter "0008" started at Sat Oct 1 18:23:14 CEST 2022
*** id :
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
[email protected]_I006D:/ #
That's it.
Note that you can change the script executed while doing the shutdown without changing the boot image again.
But you should always test the script before rebooting -- an error in your script may stop the reboot.
To change the additional .rc files it's necessary to recreate the ramdisk and boot partition.
The filesystems for /data and for /sdcard are still mounted while executing the actions for the trigger "on shutdown" .
To log the current environment while executing the shutdown script you can add code like this to the script:
Code:
(
echo
echo "*** Environment while executing the shutdown script ..."
echo
echo "*** pwd: "
pwd
echo
echo "*** id: "
id
echo
echo "*** df -h: "
df -h
echo
echo "*** ps -efZ : "
ps -efZ
echo
echo "*** env: "
env
echo
echo "*** set: "
set
echo
) >>/data/init_scripts/log/myshutdown_env.log 2>&1
To create a directory in which other actions from the .rc file (like write) can write with SELinux enabled use one of the SELInux contexts the init process can write to, e.g:
Code:
mkdir /data/system_data
chcon u:object_r:system_data_file:s0 /data/system_data
Now the .rc config
Code:
on shutdown
write /data/system_data/myshutdown.log Shutdown_started\n
will work.
See the file ./plat_file_contexts in the ramdisk from the boot partition for other existing SELinux contexts, e.g.:
Code:
[email protected]_I006D:/data/adb/test # /data/adb/magisk/magiskboot cpio ramdisk.cpio "extract plat_file_contexts plat_file_contexts" <
Loading cpio: [ramdisk.cpio]
Extract [plat_file_contexts] to [plat_file_contexts]
[email protected]_I006D:/data/adb/test # ls -l plat_file_contexts
-rw-r--r-- 1 root root 40490 2022-10-03 16:27 plat_file_contexts
[email protected]_I006D:/data/adb/test #
Please be aware that these changes will be gone after the next OS update. But on the other hand it's quite easy to create a script to re-install the shutdown script without user intervention.
Trouble Shooting
The main reason for problems with this approach are invalid SELinux contexts. Therefor you should test your script in permissive SELinux mode if it does not work like expected. To do that temporary disable SELinux before rebooting (SELinux will be automatically enabled again after the reboot), e.g.:
Code:
# set SELinux to permissive
#
setenforce 0
reboot
and check the log messages in the directory /cache/logs/oldlog for SELinux related messages:
Code:
su - -c grep deny /cache/logs/oldlog
Note that you can not disable SELinux in an action in an .rc file.
To check if your additional .rc file is processed by Magisk add a statement like these to the custom .rc file in the overlay directory:
Code:
on early-init
setprop sys.example.foo bar
If this statement is processed by Magisk and Android the property sys.example.foo should be defined after the reboot, e.g.:
Code:
[email protected]_I006D:/ # getprop sys.example.foo
bar
[email protected]_I006D:/ #
To check if the "on shutdown" trigger is processed use :
Code:
on shutdown
write /sdcard/Download/myshutdown.log Shutdown_started\n
and reboot with disabled SELinux:
Code:
setenforce 0
reboot
If the "on shutdown" trigger in your .rc file is processed there should exist the file
/sdcard/Download/myshutdown.log
after the reboot
If the shutdown of the phone hangs open another adb session to the phone and kill the script (the adb daemon should still run while the shutdown script is running).
If the phone does not boot anymore with the new shutdown script reboot the phone from the TWRP image and fix / delete the new shutdown script. Or reflash the boot partition with the image file created before starting the development.
In general you should carefully check your .rc file for syntax errors -- entries in the file after the first syntax error will be ignored
Useful URLs
I used ideas and code from the web pages listed below for this HowTo:
How to run an executable on boot and keep it running?
How to run an Android init service with superuser SELinux context?
Magisk overlay - execute a script or copy files
History
03.10.2022 /bs
added code about to extract a single file (plat_file_contexts) from the ramdisk cpio image using magiskboot

Categories

Resources