build_dlc: Fix building DLC with squashfs type

We change the ownership of the tempdir that is used by squashfs to root but the
cleanup code does not handle the removal properly and it fails with permission
problems. This code adds the directory 'squashfs-root' inside the tempdir and
copies everything there instead. That way we can manually cleanup
'squashfs-root' directory before the tempdir is cleaned up.

Also set the squashfs as the default file system as for the majority of the DLCs
it would be a better option.

BUG=chromium:911228
TEST=created a squashfs dlc and mounted it, it worked fine.
TEST=unittest

Change-Id: I323bff1c36d25a60170a9828e4dee33334ed29b6
Reviewed-on: https://chromium-review.googlesource.com/1407462
Commit-Ready: Amin Hassani <ahassani@chromium.org>
Tested-by: Amin Hassani <ahassani@chromium.org>
Reviewed-by: Nicolas Norvez <norvez@chromium.org>
diff --git a/scripts/build_dlc.py b/scripts/build_dlc.py
index 8defd47..3b2fcda 100644
--- a/scripts/build_dlc.py
+++ b/scripts/build_dlc.py
@@ -17,6 +17,9 @@
 from chromite.lib import osutils
 
 
+_SQUASHFS_TYPE = 'squashfs'
+_EXT4_TYPE = 'ext4'
+
 def HashFile(file_path):
   """Calculate the sha256 hash of a file.
 
@@ -112,17 +115,24 @@
   def CreateSquashfsImage(self):
     """Create a squashfs image."""
     with osutils.TempDir(prefix='dlc_') as temp_dir:
-      cros_build_lib.SudoRunCommand(['cp', '-a', self.src_dir, temp_dir])
-      self.SquashOwnerships(temp_dir)
-      cros_build_lib.SudoRunCommand(
-          ['mksquashfs', temp_dir, self.dest_image, '-4k-align', '-noappend'],
-          capture_output=True)
+      squashfs_root = os.path.join(temp_dir, 'squashfs-root')
+      osutils.SafeMakedirs(squashfs_root)
+      cros_build_lib.SudoRunCommand(['cp', '-a', self.src_dir, squashfs_root])
+      self.SquashOwnerships(squashfs_root)
+
+      cros_build_lib.RunCommand(['mksquashfs', squashfs_root, self.dest_image,
+                                 '-4k-align', '-noappend'],
+                                capture_output=True)
+
+      # We changed the ownership and permissions of the squashfs_root
+      # directory. Now we need to remove it manually.
+      osutils.RmDir(squashfs_root, sudo=True)
 
   def CreateImage(self):
     """Create the image and copy the DLC files to it."""
-    if self.fs_type == 'ext4':
+    if self.fs_type == _EXT4_TYPE:
       self.CreateExt4Image()
-    elif self.fs_type == 'squashfs':
+    elif self.fs_type == _SQUASHFS_TYPE:
       self.CreateSquashfsImage()
     else:
       raise ValueError('Wrong fs type: %s used:' % self.fs_type)
@@ -206,9 +216,6 @@
                         required=True,
                         help='Root directory path that contains DLC metadata '
                         'output.')
-  required.add_argument('--fs-type', metavar='FS_TYPE', required=True,
-                        choices=['squashfs', 'ext4'],
-                        help='File system type of the image.')
   required.add_argument('--pre-allocated-blocks', type=int,
                         metavar='PREALLOCATEDBLOCKS', required=True,
                         help='Number of blocks (block size is 4k) that need to'
@@ -219,6 +226,12 @@
                         help='DLC ID (unique per DLC).')
   required.add_argument('--name', metavar='NAME', required=True,
                         help='A human-readable name for the DLC.')
+
+  args = parser.add_argument_group('Arguments')
+  args.add_argument('--fs-type', metavar='FS_TYPE', default=_SQUASHFS_TYPE,
+                    choices=(_SQUASHFS_TYPE, _EXT4_TYPE),
+                    help='File system type of the image.')
+
   return parser