diff options
author | Guy Sheffer <guysoft@gmail.com> | 2022-08-20 02:54:12 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-20 02:54:12 +0300 |
commit | a6fd71ac27cb71f51fb27c21358155bf1681bec0 (patch) | |
tree | 8c0c54c93110d1dd539aab468585e6aff08c4bdd | |
parent | 1e5e8eaa61e0360954a0a3d709592ce20f9e6762 (diff) | |
parent | e5fc9bfb10a5de518459a16f3b7bfb23778c3b5e (diff) |
Merge pull request #791 from cp2004/userfix-script
Add user-fix script to allow changing usernames
-rwxr-xr-x | src/modules/octopi/filesystem/home/root/bin/user-fix | 53 | ||||
-rw-r--r-- | src/modules/octopi/filesystem/root/etc/systemd/system/user-fix.service | 18 | ||||
-rwxr-xr-x | src/modules/octopi/start_chroot_script | 9 |
3 files changed, 80 insertions, 0 deletions
diff --git a/src/modules/octopi/filesystem/home/root/bin/user-fix b/src/modules/octopi/filesystem/home/root/bin/user-fix new file mode 100755 index 0000000..09735c9 --- /dev/null +++ b/src/modules/octopi/filesystem/home/root/bin/user-fix @@ -0,0 +1,53 @@ +#!/usr/bin/env bash +# Written by Gina Häußge originally at https://github.com/OctoPrint/OctoPi-UpToDate/blob/e70ccdaf0cd4ef4adfaa3f9b6b288fb6bfda116a/scripts/files/user-fix + +set -e + +USERID=1000 +FIRSTUSER=`getent passwd $USERID | cut -d: -f1` +FIRSTUSERHOME=`getent passwd $USERID | cut -d: -f6` + +CURRENT=`grep User= /etc/systemd/system/octoprint.service | cut -d= -f2` + +if [ "$CURRENT" = "pi" -a "$FIRSTUSER" != "pi" ]; then + # if we get here it means that the first user was renamed but we haven't yet + # updated all of OctoPi's files that depend on that name, so let's do that now + + # first we need to figure out if we can use the new user name in systemd files + # directly or if we need to use the UID - we do that by checking if the + # escaped name differes from the plain name, if so something is non ASCII + # and the UID is the safer bet + FIRSTUSERESC=`systemd-escape "$FIRSTUSER"` + if [ "$FIRSTUSER" != "$FIRSTUSERESC" ]; then + SERVICEUSER=$USERID + else + SERVICEUSER=$FIRSTUSER + fi + + # fix OctoPrint service file + echo "Fixing service file" + sed -i "s!User=pi!User=$SERVICEUSER!g" /etc/systemd/system/octoprint.service + sed -i "s!ExecStart=/home/pi/!ExecStart=$FIRSTUSERHOME/!g" /etc/systemd/system/octoprint.service + systemctl daemon-reload + + # fix sudoers files + echo "Fixing sudoers" + sed -i "s!^pi!$FIRSTUSER!g" /etc/sudoers.d/octoprint-service + sed -i "s!^pi!$FIRSTUSER!g" /etc/sudoers.d/octoprint-shutdown + + # fix scripts + echo "Fixing scripts" + sed -i "s!/home/pi/!$FIRSTUSERHOME/!g" $FIRSTUSERHOME/scripts/add-octoprint-checkout + sed -i "s!/home/pi/!$FIRSTUSERHOME/!g" $FIRSTUSERHOME/scripts/welcome + sed -i "s!/home/pi/!$FIRSTUSERHOME/!g" $FIRSTUSERHOME/.bashrc + sed -i "s!/home/pi/!$FIRSTUSERHOME/!g" /root/bin/webcamd + + # fix virtualenv + echo "Fixing paths in virtual environment" + cd $FIRSTUSERHOME/oprint + sudo -u $FIRSTUSER $FIRSTUSERHOME/.local/bin/virtualenv-tools --update-path $FIRSTUSERHOME/oprint + + # finally, reboot for all of this to actually take affect + echo "Adjusted scripts to new user, restarting services..." + systemctl reboot +fi
\ No newline at end of file diff --git a/src/modules/octopi/filesystem/root/etc/systemd/system/user-fix.service b/src/modules/octopi/filesystem/root/etc/systemd/system/user-fix.service new file mode 100644 index 0000000..b95981c --- /dev/null +++ b/src/modules/octopi/filesystem/root/etc/systemd/system/user-fix.service @@ -0,0 +1,18 @@ +# Written by Gina Häußge originally at https://github.com/OctoPrint/OctoPi-UpToDate/blob/e70ccdaf0cd4ef4adfaa3f9b6b288fb6bfda116a/scripts/files/user-fix.service +[Unit] +Description=Ensure that user name changes are applied as needed + +DefaultDependencies=no + +Before=network-pre.target +Wants=network-pre.target + +After=local-fs.target +Wants=local-fs.target + +[Service] +Type=oneshot +ExecStart=/root/bin/user-fix + +[Install] +WantedBy=multi-user.target diff --git a/src/modules/octopi/start_chroot_script b/src/modules/octopi/start_chroot_script index 56f43b6..d78f49e 100755 --- a/src/modules/octopi/start_chroot_script +++ b/src/modules/octopi/start_chroot_script @@ -329,6 +329,15 @@ systemctl_if_exists enable networkcheck.timer echo "--- Installing avrdude" apt-get -y install avrdude +### User-fixing +# Users can change their username easily via the Raspberry Pi imager, which breaks some of OctoPi's scripts +# we need to install virtualenv-tools3, so let's get pip and that +apt install -y python3-pip +sudo -u pi pip3 install --user virtualenv-tools3 + +systemctl_if_exists enable user-fix.service + + #cleanup apt-get clean apt-get autoremove -y |