update_engine: Expose the update_engine.conf in the FilesystemInterface.

The old image contains a /etc/update_engine.conf file that states the
payload version supported by that image. Currently we read this file
directly from the mounted filesystem and parse its contents. To stop
mounting the filesystem we need to retrieve the information on this file
somehow.

This patch extends the FilesystemInterface with a method to load the
update_engine.conf settings from the filesystem and implemets it using
ext2fs on ext2 filesystems.

CQ-DEPED=CL:282380
BUG=chromium:305832
TEST=Added new unittests with and without this file.

Change-Id: I41b41e8aac58c645fb40aabfe340cde8821e405a
Reviewed-on: https://chromium-review.googlesource.com/282381
Reviewed-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
Trybot-Ready: Alex Deymo <deymo@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
diff --git a/sample_images/disk_ext2_1k.txt b/sample_images/disk_ext2_1k.txt
index f13c080..1494535 100644
--- a/sample_images/disk_ext2_1k.txt
+++ b/sample_images/disk_ext2_1k.txt
@@ -1 +1 @@
-16777216 1024
+default 16777216 1024
diff --git a/sample_images/disk_ext2_4k.txt b/sample_images/disk_ext2_4k.txt
index d01d105..cac718f 100644
--- a/sample_images/disk_ext2_4k.txt
+++ b/sample_images/disk_ext2_4k.txt
@@ -1 +1 @@
-16777216 4096
+default 16777216 4096
diff --git a/sample_images/disk_ext2_ue_settings.txt b/sample_images/disk_ext2_ue_settings.txt
new file mode 100644
index 0000000..554f7da
--- /dev/null
+++ b/sample_images/disk_ext2_ue_settings.txt
@@ -0,0 +1 @@
+ue_settings 16777216 4096
diff --git a/sample_images/generate_image.sh b/sample_images/generate_image.sh
index 0f0c384..c4f132a 100755
--- a/sample_images/generate_image.sh
+++ b/sample_images/generate_image.sh
@@ -17,29 +17,11 @@
   rmdir "$1"
 }
 
-# generate_fs <filename> <size> [block_size] [block_groups]
-generate_fs() {
-  local filename="$1"
-  local size="$2"
-  local block_size="${3:-4096}"
-  local block_groups="${4:-}"
-
-  local mkfs_opts=( -q -F -b "${block_size}" -L "ROOT-TEST" -t ext2 )
-  if [[ -n "${block_groups}" ]]; then
-    mkfs_opts+=( -G "${block_groups}" )
-  fi
-
-  local mntdir=$(mktemp --tmpdir -d generate_ext2.XXXXXX)
-  trap 'cleanup "${mntdir}"; rm -f "${filename}"' INT TERM EXIT
-
-  # Cleanup old image.
-  if [[ -e "${filename}" ]]; then
-    rm -f "${filename}"
-  fi
-  truncate --size="${size}" "${filename}"
-
-  mkfs.ext2 "${mkfs_opts[@]}" "${filename}"
-  sudo mount "${filename}" "${mntdir}" -o loop
+# add_files_default <mntdir> <block_size>
+# Add several test files to the image mounted in <mntdir>.
+add_files_default() {
+  local mntdir="$1"
+  local block_size="$2"
 
   ### Generate the files used in unittest with descriptive names.
   sudo touch "${mntdir}"/empty-file
@@ -93,6 +75,71 @@
     "empty space data but it won't be all zeros." |
     sudo dd of="${mntdir}"/removed conv=fsync status=none
   sudo rm "${mntdir}"/removed
+}
+
+# add_files_ue_settings <mntdir> <block_size>
+# Add the update_engine.conf settings file. This file contains the
+add_files_ue_settings() {
+  local mntdir="$1"
+
+  sudo mkdir -p "${mntdir}"/etc >/dev/null
+  sudo tee "${mntdir}"/etc/update_engine.conf >/dev/null <<EOF
+PAYLOAD_MINOR_VERSION=1234
+EOF
+  # Example of a real lsb-release file released on link stable.
+  sudo tee "${mntdir}"/etc/lsb-release >/dev/null <<EOF
+CHROMEOS_AUSERVER=https://tools.google.com/service/update2
+CHROMEOS_BOARD_APPID={F26D159B-52A3-491A-AE25-B23670A66B32}
+CHROMEOS_CANARY_APPID={90F229CE-83E2-4FAF-8479-E368A34938B1}
+CHROMEOS_DEVSERVER=
+CHROMEOS_RELEASE_APPID={F26D159B-52A3-491A-AE25-B23670A66B32}
+CHROMEOS_RELEASE_BOARD=link-signed-mp-v4keys
+CHROMEOS_RELEASE_BRANCH_NUMBER=63
+CHROMEOS_RELEASE_BUILD_NUMBER=6946
+CHROMEOS_RELEASE_BUILD_TYPE=Official Build
+CHROMEOS_RELEASE_CHROME_MILESTONE=43
+CHROMEOS_RELEASE_DESCRIPTION=6946.63.0 (Official Build) stable-channel link
+CHROMEOS_RELEASE_NAME=Chrome OS
+CHROMEOS_RELEASE_PATCH_NUMBER=0
+CHROMEOS_RELEASE_TRACK=stable-channel
+CHROMEOS_RELEASE_VERSION=6946.63.0
+GOOGLE_RELEASE=6946.63.0
+EOF
+}
+
+# generate_fs <filename> <kind> <size> [block_size] [block_groups]
+generate_fs() {
+  local filename="$1"
+  local kind="$2"
+  local size="$3"
+  local block_size="${4:-4096}"
+  local block_groups="${5:-}"
+
+  local mkfs_opts=( -q -F -b "${block_size}" -L "ROOT-TEST" -t ext2 )
+  if [[ -n "${block_groups}" ]]; then
+    mkfs_opts+=( -G "${block_groups}" )
+  fi
+
+  local mntdir=$(mktemp --tmpdir -d generate_ext2.XXXXXX)
+  trap 'cleanup "${mntdir}"; rm -f "${filename}"' INT TERM EXIT
+
+  # Cleanup old image.
+  if [[ -e "${filename}" ]]; then
+    rm -f "${filename}"
+  fi
+  truncate --size="${size}" "${filename}"
+
+  mkfs.ext2 "${mkfs_opts[@]}" "${filename}"
+  sudo mount "${filename}" "${mntdir}" -o loop
+
+  case "${kind}" in
+    ue_settings)
+      add_files_ue_settings "${mntdir}" "${block_size}"
+      ;;
+    default)
+      add_files_default "${mntdir}" "${block_size}"
+      ;;
+  esac
 
   cleanup "${mntdir}"
   trap - INT TERM EXIT