Clear stale shadow-utils lockfiles before building

If a build is killed in the middle, stale lockfiles for group and user
dbs might get left around. Clean them up in build_packages, build_image,
and update_chroot.

BUG=None
TEST=build_packages with a stale /etc/passwd.lock sitting around.

Change-Id: Ie9887b6b38ba92cc8c008ea15a2f44a0f312b758
Reviewed-on: https://chromium-review.googlesource.com/185605
Tested-by: Chris Masone <cmasone@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Commit-Queue: Chris Masone <cmasone@chromium.org>
diff --git a/build_image b/build_image
index 1ad93b0..03e4c7f 100755
--- a/build_image
+++ b/build_image
@@ -116,6 +116,8 @@
 
 load_board_specific_script "${FLAGS_board}" "board_specific_setup.sh"
 
+sudo_clear_shadow_locks "/build/${FLAGS_board}"
+
 # TODO: <prebuild hook>
 
 BASE_PACKAGE="chromeos-base/chromeos"
diff --git a/build_packages b/build_packages
index a35fcb2..9916fb9 100755
--- a/build_packages
+++ b/build_packages
@@ -141,6 +141,8 @@
 
 "${SCRIPTS_DIR}"/setup_board --quiet --board=${FLAGS_board} "${UPDATE_ARGS[@]}"
 
+sudo_clear_shadow_locks "/build/${FLAGS_board}"
+
 # Setup all the emerge command/flags.
 EMERGE_FLAGS=( -uDNv --backtrack=30 --select )
 
diff --git a/common.sh b/common.sh
index b5beea2..da305ff 100644
--- a/common.sh
+++ b/common.sh
@@ -615,6 +615,12 @@
   fi
 }
 
+# Clears out stale shadow-utils locks in the given target root.
+sudo_clear_shadow_locks() {
+  info "Clearing shadow utils lockfiles under $1"
+  sudo rm -f "$1/etc/"{passwd,group,shadow,gshadow}.lock*
+}
+
 # Writes stdin to the given file name as the sudo user in overwrite mode.
 #
 # $@ - The output file names.
diff --git a/update_chroot b/update_chroot
index 12cfec9..19577fb 100755
--- a/update_chroot
+++ b/update_chroot
@@ -60,6 +60,8 @@
 
 info "Updating chroot"
 
+sudo_clear_shadow_locks /
+
 EMERGE_FLAGS="-uNv --with-bdeps=y --select"
 if [ "${FLAGS_usepkg}" -eq "${FLAGS_TRUE}" ]; then
   EMERGE_FLAGS="${EMERGE_FLAGS} --getbinpkg"