Mods to build_rootfs.py to make it work on the build server

Added call that can be used to just open a bash terminal

Change-Id: Ibfd5998d864c5a6fc2de08b86eab177ef6263aac
Signed-off-by: Jim Ostrowski <yimmy13@gmail.com>
diff --git a/frc971/orin/build_rootfs.py b/frc971/orin/build_rootfs.py
index 3516ebd..ea15cf8 100755
--- a/frc971/orin/build_rootfs.py
+++ b/frc971/orin/build_rootfs.py
@@ -9,6 +9,7 @@
 import jinja2
 import os
 import pathlib
+import platform
 import re
 import shlex
 import shutil
@@ -59,6 +60,15 @@
         subprocess.run(["sudo", "umount", partition], check=True)
 
 
+def check_buildifier():
+    """Checks if buildifier is in the path"""
+    result = subprocess.run(["which", "buildifier"], stdout=subprocess.PIPE)
+    if result.stdout.decode('utf-8') == "":
+        return False
+    else:
+        return True
+
+
 def check_required_deps(deps):
     """Checks if the provided list of dependencies is installed."""
     missing_deps = []
@@ -78,7 +88,7 @@
 
 def make_image(image):
     """Makes an image and creates an xfs filesystem on it."""
-    print("Creating image ", f"{image}")
+    print("--> Creating NEW image ", f"{image}")
     result = subprocess.run([
         "dd", "if=/dev/zero", f"of={image}", "bs=1", "count=0",
         "seek=8589934592"
@@ -556,7 +566,10 @@
 
         # Print out all the libraries and where they live as known to ldconfig
         result = subprocess.run(
-            ['ldconfig', '-C', f'{self.partition}/etc/ld.so.cache', '-p'],
+            [
+                '/usr/sbin/ldconfig', '-C',
+                f'{self.partition}/etc/ld.so.cache', '-p'
+            ],
             check=True,
             stdout=subprocess.PIPE,
         )
@@ -913,9 +926,37 @@
     subprocess.run(["sha256sum", tarball], check=True)
 
 
+def mount_and_bash():
+    """Helper function to just mount and open a bash interface
+    To run from the CLI, call
+    python3 -c "from build_rootfs import *; mount_and_bash()"
+    """
+    with scoped_mount(IMAGE) as partition:
+        subprocess.run([
+            "sudo", "cp", "/usr/bin/qemu-aarch64-static",
+            f"{partition}/usr/bin/"
+        ],
+                       check=True)
+
+        global PARTITION
+        PARTITION = partition
+        target(["/bin/bash"])
+
+
 def main():
     check_required_deps(REQUIRED_DEPS)
 
+    if not os.path.exists(YOCTO):
+        print("ERROR: Must have YOCTO directory properly specified to run")
+        print("See https://github.com/frc971/meta-frc971/tree/main for info")
+        exit()
+
+    if not check_buildifier():
+        print(
+            "ERROR: Need to have buildifier in the path.  Please resolve this."
+        )
+        exit()
+
     new_image = not os.path.exists(IMAGE)
     if new_image:
         make_image(IMAGE)
@@ -941,11 +982,10 @@
         if new_image:
             target(["/debootstrap/debootstrap", "--second-stage"])
 
-            target([
-                "useradd", "-m", "-p",
-                '$y$j9T$85lzhdky63CTj.two7Zj20$pVY53UR0VebErMlm8peyrEjmxeiRw/rfXfx..9.xet1',
-                '-s', '/bin/bash', 'pi'
-            ])
+            # Do this unescaped; otherwise, we get quotes in the password
+            target_unescaped(
+                "useradd -m -p \"\$y\$j9T\$85lzhdky63CTj.two7Zj20\$pVY53UR0VebErMlm8peyrEjmxeiRw/rfXfx..9.xet1\" -s /bin/bash pi"
+            )
             target(["addgroup", "debug"])
             target(["addgroup", "crypto"])
             target(["addgroup", "trusty"])
@@ -956,6 +996,10 @@
                      "etc/apt/sources.list.d/bullseye-backports.list")
             target(["apt-get", "update"])
 
+        # This is useful in recovering from a partial build, and shouldn't break
+        # anything otherwise
+        target(["apt", "--fix-broken", "install"])
+
         target([
             "apt-get", "-y", "install", "gnupg", "wget", "systemd",
             "systemd-resolved", "locales"
@@ -966,8 +1010,23 @@
         target_mkdir("root:root", "755", "run/systemd")
         target_mkdir("systemd-resolve:systemd-resolve", "755",
                      "run/systemd/resolve")
-        copyfile("systemd-resolve:systemd-resolve", "644",
-                 "run/systemd/resolve/stub-resolv.conf")
+
+        # Need to use the machine's local resolv.conf when running this
+        # We put a generic file in place at the end when we're done
+        subprocess.run([
+            "sudo", "cp", f"/etc/resolv.conf",
+            f"{PARTITION}/run/systemd/resolve/stub-resolv.conf"
+        ],
+                       check=True)
+        subprocess.run([
+            "sudo", "chmod", "644",
+            f"{PARTITION}/run/systemd/resolve/stub-resolv.conf"
+        ],
+                       check=True)
+        target([
+            "chown", "systemd-resolve:systemd-resolve",
+            f"/run/systemd/resolve/stub-resolv.conf"
+        ])
         target(["systemctl", "enable", "systemd-resolved"])
 
         target([
@@ -1215,6 +1274,13 @@
             )
             target(["vim", "-c", "\":qa!\""])
 
+        # Do this after all the network needs are finished, since it won't
+        # allow us to find URL's from the build server (frc971)
+        target(["systemctl", "disable", "systemd-resolved"])
+        copyfile("systemd-resolve:systemd-resolve", "644",
+                 "run/systemd/resolve/stub-resolv.conf")
+        target(["systemctl", "enable", "systemd-resolved"])
+
         generate_build_file(partition)
 
         do_package(partition)