This commit is contained in:
paukstelis
2022-10-21 09:35:24 -04:00
parent 9260a1dae4
commit ebaf739ab8

View File

@@ -8,14 +8,14 @@ fi
# from stackoverflow.com/questions/3231804 # from stackoverflow.com/questions/3231804
prompt_confirm() { prompt_confirm() {
while true; do while true; do
read -r -n 1 -p "${1:-Continue?} [y/n]: " REPLY read -r -n 1 -p "${1:-Continue?} [y/n]: " REPLY
case $REPLY in case $REPLY in
[yY]) echo ; return 0 ;; [yY]) echo ; return 0 ;;
[nN]) echo ; return 1 ;; [nN]) echo ; return 1 ;;
*) printf " \033[31m %s \n\033[0m" "invalid input" *) printf " \033[31m %s \n\033[0m" "invalid input"
esac esac
done done
} }
# from unix.stackexchange.com/questions/391293 # from unix.stackexchange.com/questions/391293
log () { log () {
@@ -26,76 +26,192 @@ log () {
fi | tee -a "$logfile" fi | tee -a "$logfile"
} }
detect_printer() {
echo
echo
dmesg -C
echo "Plug your printer in via USB now (detection time-out in 1 min)"
counter=0
while [[ -z "$UDEV" ]] && [[ $counter -lt 60 ]]; do
TEMPUSB=$(dmesg | sed -n -e 's/^.*\(cdc_acm\|ftdi_sio\|ch341\|cp210x\) \([0-9].*[0-9]\): \(tty.*\|FTD.*\|ch341-uart.*\|cp210x\).*/\2/p')
UDEV=$(dmesg | sed -n -e 's/^.*SerialNumber: //p')
counter=$(( $counter + 1 ))
if [[ -n "$TEMPUSB" ]] && [[ -z "$UDEV" ]]; then
break
fi
sleep 1
done
dmesg -C
}
detect_camera() {
dmesg -C
echo "Plug your camera in via USB now (detection time-out in 1 min)"
counter=0
while [[ -z "$CAM" ]] && [[ $counter -lt 60 ]]; do
CAM=$(dmesg | sed -n -e 's/^.*SerialNumber: //p')
TEMPUSBCAM=$(dmesg | sed -n -e 's|^.*input:.*/\(.*\)/input/input.*|\1|p')
counter=$(( $counter + 1 ))
if [[ -n "$TEMPUSBCAM" ]] && [[ -z "$CAM" ]]; then
break
fi
sleep 1
done
dmesg -C
}
# initiate logging # initiate logging
logfile='printer_udev.log' logfile='udev.log'
echo "$(date) starting instance installation" >> $logfile echo "$(date) starting udev rules" >> $logfile
do_printer() {
echo "UNPLUG PRINTER YOU ARE INSTALLING NOW (other printers can remain)"
echo "Enter the name for new printer/instance (no spaces):"
read INSTANCE
if [ -z "$INSTANCE" ]; then
echo "No instance given. Exiting" | log
exit 1
fi
echo "UNPLUG PRINTER YOU ARE INSTALLING NOW (other printers can remain)" if prompt_confirm "Begin auto-detect printer serial number for udev entry?"
echo "Enter the name for new printer/instance (no spaces):" then
read INSTANCE echo
if [ -z "$INSTANCE" ]; then detect_printer
echo "No instance given. Exiting" | log else
exit 1 echo "OK. Restart when you are ready" | log; exit 0
fi fi
if prompt_confirm "Begin auto-detect printer serial number for udev entry?" if [ -z "$UDEV" ]; then
then echo "Printer Serial Number not detected"
echo prompt_confirm "Do you want to use the physical USB port to assign the udev entry? If you use this any USB hubs and printers detected this way must stay plugged into the same USB positions on your machine as they are right now" || exit 0
#clear out journalctl - probably a better way to do this echo
journalctl --rotate > /dev/null 2>&1 USB=$TEMPUSB
journalctl --vacuum-time=1seconds > /dev/null 2>&1 echo "Your printer will be setup at the following usb address:"
echo "Plug your printer in via USB now (detection time-out in 1 min)" echo $USB | log
counter=0 echo
while [[ -z "$UDEV" ]] && [[ $counter -lt 30 ]]; do else
UDEV=$(timeout 1s journalctl -kf | sed -n -e 's/^.*SerialNumber: //p') echo "Serial number detected as: $UDEV" | log
TEMPUSB=$(timeout 1s journalctl -kf | sed -n -e 's/^.*\(cdc_acm\|ftdi_sio\|ch341\) \([0-9].*[0-9]\): \(tty.*\|FTD.*\|ch341-uart.*\).*/\2/p') fi
counter=$(( $counter + 1 ))
done echo
else
echo "OK. Restart when you are ready" | log; exit 0
fi
if [ -z "$UDEV" ]; then if prompt_confirm "Ready to write udev entry. Do you want to proceed?"
echo "Printer Serial Number not detected" then
prompt_confirm "Do you want to use the physical USB port to assign the udev entry? If you use this any USB hubs and printers detected this way must stay plugged into the same USB positions on your machine as they are right now" || exit 0 #Printer udev identifier technique - either Serial number or USB port
#if [[ $REPLY =~ ^[Yy]$ ]]; then #Serial Number
echo if [ -n "$UDEV" ]; then
USB=$TEMPUSB #echo $UDEV
echo "Your printer will be setup at the following usb address:" echo SUBSYSTEM==\"tty\", ATTRS{serial}==\"$UDEV\", SYMLINK+=\"octo_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules
echo $USB | log echo This printer can now always be found at /dev/octo_$INSTANCE
echo fi
else
echo "Serial number detected as: $UDEV" | log
fi
echo #USB port
if [ -n "$USB" ]; then
#echo $USB
echo KERNELS==\"$USB\",SUBSYSTEM==\"tty\",SYMLINK+=\"octo_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules
echo This printer can now always be found at /dev/octo_$INSTANCE
fi
#Reset udev
udevadm control --reload-rules
udevadm trigger
if prompt_confirm "Ready to write udev entry. Do you want to proceed?" fi
then main_menu
#Printer udev identifier technique - either Serial number or USB port }
#Serial Number
if [ -n "$UDEV" ]; then
#echo $UDEV
echo SUBSYSTEM==\"tty\", ATTRS{serial}==\"$UDEV\", SYMLINK+=\"octo_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules
echo This printer can now always be found at /dev/octo_$INSTANCE
fi
#USB port do_camera() {
if [ -n "$USB" ]; then echo "Unplug the camera you are going to install if is already plugged in"
#echo $USB echo "Enter the name for new camera (no spaces):"
echo KERNELS==\"$USB\",SUBSYSTEM==\"tty\",SYMLINK+=\"octo_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules read INSTANCE
echo This printer can now always be found at /dev/octo_$INSTANCE if [ -z "$INSTANCE" ]; then
fi echo "No instance given. Exiting" | log
exit 1
fi
#Reset udev
udevadm control --reload-rules
udevadm trigger
systemctl daemon-reload
sleep 1
fi if prompt_confirm "Begin auto-detect printer serial number for udev entry?"
then
echo
detect_camera
else
echo "OK. Restart when you are ready" | log; exit 0
fi
#Failed state. Nothing detected
if [ -z "$CAM" ] && [ -z "$TEMPUSBCAM" ] ; then
echo
echo -e "\033[0;31mNo camera was detected during the detection period.\033[0m"
echo
return
fi
if [ -z "$CAM" ]; then
echo "Camera Serial Number not detected" | log
echo -e "Camera will be setup with physical USB address of \033[0;34m $TEMPUSBCAM.\033[0m" | log
echo "The camera will have to stay plugged into this location." | log
USBCAM=$TEMPUSBCAM
else
echo -e "Camera detected with serial number: \033[0;34m $CAM \033[0m" | log
check_sn "$CAM"
fi
#Serial Number
if [ -n "$CAM" ]; then
echo SUBSYSTEM==\"video4linux\", ATTRS{serial}==\"$CAM\", ATTR{index}==\"0\", SYMLINK+=\"cam_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules
fi
#USB port camera
if [ -n "$USBCAM" ]; then
echo SUBSYSTEM==\"video4linux\",KERNELS==\"$USBCAM\", SUBSYSTEMS==\"usb\", ATTR{index}==\"0\", DRIVERS==\"uvcvideo\", SYMLINK+=\"cam_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules
fi
echo "Camera rule written. The camera will now be found at /dev/cam_$INSTANCE"
#Reset udev
udevadm control --reload-rules
udevadm trigger
main_menu
}
main_menu() {
VERSION=0.0.1
#reset
UDEV=''
TEMPUSB=''
CAM=''
TEMPUSBCAM=''
INSTANCE=''
echo
echo
echo "*************************"
echo "udev_rules $VERSION"
echo "*************************"
echo
PS3='Select udev rule to create: '
options=("Printer rule" "USB camera rule" "Quit")
select opt in "${options[@]}"
do
case $opt in
"Printer rule")
do_printer
break ;;
"USB camera rule")
do_camera
break ;;
"Quit")
exit 1
;;
*) echo "invalid option $REPLY";;
esac
done
}
main_menu