Mods to the orin build root code/setup

Adding sudo call to do_flash
Otherwise, you have to wait for tar to finish and then enter sudo password

Adding frc971 systemctl services
The services set up things for aos_starter to work properly

Change-Id: Ie61f53b6fc220d6446a8a31b392b5bd3c9bf5b22
Signed-off-by: Jim Ostrowski <yimmy13@gmail.com>
diff --git a/frc971/orin/build_rootfs.py b/frc971/orin/build_rootfs.py
index 59a3d7f..3516ebd 100755
--- a/frc971/orin/build_rootfs.py
+++ b/frc971/orin/build_rootfs.py
@@ -1172,15 +1172,18 @@
         copyfile("pi:pi", "600", "home/pi/.ssh/authorized_keys")
         target_mkdir("root:root", "700", "root/bin")
         copyfile("root:root", "644", "etc/systemd/system/grow-rootfs.service")
+        copyfile("root:root", "644", "etc/systemd/system/frc971.service")
+        copyfile("root:root", "644", "etc/systemd/system/frc971chrt.service")
         copyfile("root:root", "644",
                  "etc/systemd/system/jetson-clocks.service")
         copyfile("root:root", "500", "root/bin/change_hostname.sh")
+        copyfile("root:root", "500", "root/bin/chrt.sh")
         copyfile("root:root", "700", "root/trace.sh")
         copyfile("root:root", "440", "etc/sudoers")
         copyfile("root:root", "644", "etc/fstab")
         copyfile("root:root", "644",
                  "var/nvidia/nvcam/settings/camera_overrides.isp")
-        copyfile("root.root", "644", "/etc/ld.so.conf.d/yocto.conf")
+        copyfile("root:root", "644", "/etc/ld.so.conf.d/yocto.conf")
 
         target_mkdir("root:root", "755", "etc/systemd/network")
         copyfile("root:root", "644", "etc/systemd/network/eth0.network")
@@ -1189,11 +1192,13 @@
         copyfile("root:root", "644", "etc/systemd/network/80-canc.network")
         copyfile("root:root", "644", "etc/udev/rules.d/nvidia.rules")
         copyfile("root:root", "644", "etc/udev/rules.d/can.rules")
-        target(["/root/bin/change_hostname.sh", "pi-971-1"])
+        target(["/root/bin/change_hostname.sh", "orin-971-1"])
 
         target(["systemctl", "enable", "systemd-networkd"])
         target(["systemctl", "enable", "grow-rootfs"])
         target(["systemctl", "enable", "jetson-clocks"])
+        target(["systemctl", "enable", "frc971"])
+        target(["systemctl", "enable", "frc971chrt"])
 
         target(["apt-file", "update"])
 
diff --git a/frc971/orin/contents/etc/systemd/system/frc971.service b/frc971/orin/contents/etc/systemd/system/frc971.service
new file mode 100644
index 0000000..d109b0f
--- /dev/null
+++ b/frc971/orin/contents/etc/systemd/system/frc971.service
@@ -0,0 +1,22 @@
+[Unit]
+Description=Start up 971 robot code
+Requires=network-online.target
+After=network-online.target
+
+[Service]
+User=root
+Group=root
+Type=simple
+WorkingDirectory=/home/pi/bin
+ExecStart=/home/pi/bin/starter.sh
+KillMode=mixed
+TimeoutStopSec=10
+LimitRTPRIO=60
+LimitNICE=-20
+LimitMEMLOCK=infinity
+
+Restart=always
+RestartSec=5s
+
+[Install]
+WantedBy=multi-user.target
diff --git a/frc971/orin/contents/etc/systemd/system/frc971chrt.service b/frc971/orin/contents/etc/systemd/system/frc971chrt.service
new file mode 100644
index 0000000..1fc304e
--- /dev/null
+++ b/frc971/orin/contents/etc/systemd/system/frc971chrt.service
@@ -0,0 +1,10 @@
+[Unit]
+Description=Configure tracing to be available by everyone
+After=sys-kernel-tracing.mount
+
+[Service]
+Type=oneshot
+ExecStart=/root/bin/chrt.sh
+
+[Install]
+WantedBy=multi-user.target
diff --git a/frc971/orin/contents/root/bin/change_hostname.sh b/frc971/orin/contents/root/bin/change_hostname.sh
index 785903e..12e1564 100755
--- a/frc971/orin/contents/root/bin/change_hostname.sh
+++ b/frc971/orin/contents/root/bin/change_hostname.sh
@@ -5,15 +5,15 @@
 HOSTNAME="$1"
 
 # TODO<Jim>: Should probably add handling for imu hostname, too
-if [[ ! "${HOSTNAME}" =~ ^pi-[0-9]*-[0-9]$ ]]; then
-  echo "Invalid hostname ${HOSTNAME}, needs to be pi-[team#]-[pi#]"
+if [[ ! "${HOSTNAME}" =~ ^orin-[0-9]*-[0-9]$ ]]; then
+  echo "Invalid hostname ${HOSTNAME}, needs to be orin-[team#]-[orin#]"
   exit 1
 fi
 
-TEAM_NUMBER="$(echo ${HOSTNAME} | sed 's/pi-\(.*\)-.*/\1/')"
-PI_NUMBER="$(echo ${HOSTNAME} | sed 's/pi-.*-\(.*\)/\1/')"
+TEAM_NUMBER="$(echo ${HOSTNAME} | sed 's/orin-\(.*\)-.*/\1/')"
+ORIN_NUMBER="$(echo ${HOSTNAME} | sed 's/orin-.*-\(.*\)/\1/')"
 IP_BASE="$(echo ${TEAM_NUMBER} | sed 's/\(.*\)\(..\)/10.\1.\2/')"
-IP="${IP_BASE}.$(( 100 + ${PI_NUMBER}))"
+IP="${IP_BASE}.$(( 100 + ${ORIN_NUMBER}))"
 
 echo "Changing to team number ${TEAM_NUMBER}, IP ${IP}"
 
@@ -30,19 +30,19 @@
   echo -e "127.0.1.1\t${HOSTNAME}" >> /etc/hosts
 fi
 
-# Put corret team number in pi's IP addresses, or add them if needed
-if grep '^10\.[0-9]*\.[0-9]*\.[0-9]*\s*pi-[0-9]*-[0-9] pi[0-9]$' /etc/hosts >/dev/null ;
+# Put correct team number in orin's IP addresses, or add them if needed
+if grep '^10\.[0-9]*\.[0-9]*\.[0-9]*\s*orin-[0-9]*-[0-9] orin[0-9]$' /etc/hosts >/dev/null ;
 then
-  sed -i "s/^10\.[0-9]*\.[0-9]*\(\.[0-9]*\s*pi-\)[0-9]*\(-[0-9] pi[0-9]\)\(.*\)$/${IP_BASE}\1${TEAM_NUMBER}\2\3/" /etc/hosts
+  sed -i "s/^10\.[0-9]*\.[0-9]*\(\.[0-9]*\s*orin-\)[0-9]*\(-[0-9] orin[0-9]\)\(.*\)$/${IP_BASE}\1${TEAM_NUMBER}\2\3/" /etc/hosts
 else
-  for i in {1..6}; do
+  for i in {1..3}; do
       imu=""
-      # Add imu name to pi6.  Put space in this string, since extra
+      # Add imu name to orin3.  Put space in this string, since extra
       # spaces otherwise will make the above grep fail
-      if [[ ${i} == 6 ]]; then
+      if [[ ${i} == 3 ]]; then
           imu=" imu"
       fi
-    echo -e "${IP_BASE}.$(( i + 100 ))\tpi-${TEAM_NUMBER}-${i} pi${i}${imu}" >> /etc/hosts
+    echo -e "${IP_BASE}.$(( i + 100 ))\torin-${TEAM_NUMBER}-${i} orin${i}${imu}" >> /etc/hosts
   done
 fi
 
diff --git a/frc971/orin/contents/root/bin/chrt.sh b/frc971/orin/contents/root/bin/chrt.sh
new file mode 100644
index 0000000..4a6b917
--- /dev/null
+++ b/frc971/orin/contents/root/bin/chrt.sh
@@ -0,0 +1,19 @@
+#!/bin/bash
+
+set -ex
+
+chmod a+rwx /sys/kernel/debug
+
+# Make sure /sys/kernel/tracing is reasonably accessible so --enable_ftrace
+# works.
+chmod a+w /sys/kernel/tracing/trace_marker
+echo 10000 > /sys/kernel/tracing/buffer_size_kb
+chmod a+rw /sys/kernel/tracing/tracing_on
+chmod a+rwx /sys/kernel/tracing
+
+# Make sure we never scale the CPUs down.
+# This takes time to do which we don't have...
+cat /sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq > \
+  /sys/devices/system/cpu/cpufreq/policy0/scaling_min_freq
+cat /sys/devices/system/cpu/cpufreq/policy4/scaling_max_freq > \
+  /sys/devices/system/cpu/cpufreq/policy4/scaling_min_freq
diff --git a/frc971/orin/doflash_frc971.sh b/frc971/orin/doflash_frc971.sh
index ecfa90c..f8cbbc3 100755
--- a/frc971/orin/doflash_frc971.sh
+++ b/frc971/orin/doflash_frc971.sh
@@ -9,6 +9,9 @@
 }
 trap finish EXIT
 
+# Call sudo to get it started here, rather than waiting for after tar/cp's
+sudo echo "Flashing orin"
+
 # Assumes that the image has been copied into ./
 tar xf frc971-image-orin-nx-8g.tegraflash.tar.gz -C "${TMPDIR}"