build_dlc: Add a simple (/etc/lsb-release) into the DLC

This patch adds functionally to add a simple /etc/lsb-release (different from
the chromiumos image's lsb-release) to the DLC image. It only has two values:
- The DLC ID
- The DLC name

BUG=chromium:913076
TEST=unittest
TEST='emerge-edgar dummy-dlc' generated the correct file.

Change-Id: I0e5b2ca0a87b0173ee08f923cb9a975e37c85be0
Reviewed-on: https://chromium-review.googlesource.com/1429700
Commit-Ready: Amin Hassani <ahassani@chromium.org>
Tested-by: Amin Hassani <ahassani@chromium.org>
Reviewed-by: Xiaochu Liu <xiaochu@chromium.org>
diff --git a/scripts/build_dlc.py b/scripts/build_dlc.py
index 03d4d0d..2b11e15 100644
--- a/scripts/build_dlc.py
+++ b/scripts/build_dlc.py
@@ -19,10 +19,15 @@
 
 DLC_META_DIR = 'opt/google/dlc/'
 DLC_IMAGE_DIR = 'build/rootfs/dlc/'
+LSB_RELEASE = 'etc/lsb-release'
+
+DLC_ID_KEY = 'DLC_ID'
+DLC_NAME_KEY = 'DLC_NAME'
 
 _SQUASHFS_TYPE = 'squashfs'
 _EXT4_TYPE = 'ext4'
 
+
 def HashFile(file_path):
   """Calculate the sha256 hash of a file.
 
@@ -119,6 +124,7 @@
       try:
         # Copy DLC files over to the image.
         osutils.CopyDirContents(self.src_dir, dlc_root_dir)
+        self.PrepareLsbRelease(mount_point)
 
         self.SquashOwnerships(mount_point)
       finally:
@@ -137,6 +143,7 @@
       dlc_root_dir = os.path.join(squashfs_root, self._DLC_ROOT_DIR)
       osutils.SafeMakedirs(dlc_root_dir)
       osutils.CopyDirContents(self.src_dir, dlc_root_dir)
+      self.PrepareLsbRelease(squashfs_root)
 
       self.SquashOwnerships(squashfs_root)
 
@@ -148,6 +155,25 @@
       # directory. Now we need to remove it manually.
       osutils.RmDir(squashfs_root, sudo=True)
 
+  def PrepareLsbRelease(self, dlc_dir):
+    """Prepare the file /etc/lsb-release in the DLC module.
+
+    This file is used dropping some identification parameters for the DLC.
+
+    Args:
+      dlc_dir: (str) The path to root directory of the DLC. e.g. mounted point
+          when we are creating the image.
+    """
+    lsb_release = os.path.join(dlc_dir, LSB_RELEASE)
+    osutils.SafeMakedirs(os.path.dirname(lsb_release))
+
+    fields = {
+        DLC_ID_KEY: self.dlc_id,
+        DLC_NAME_KEY: self.name,
+    }
+    content = ''.join(['%s=%s\n' % (k, v) for k, v in fields.items()])
+    osutils.WriteFile(lsb_release, content)
+
   def CreateImage(self):
     """Create the image and copy the DLC files to it."""
     if self.fs_type == _EXT4_TYPE:
diff --git a/scripts/build_dlc_unittest.py b/scripts/build_dlc_unittest.py
index 99d927f..eb262b1 100644
--- a/scripts/build_dlc_unittest.py
+++ b/scripts/build_dlc_unittest.py
@@ -91,6 +91,16 @@
     copy_dir_mock.assert_called_once_with(partial_mock.HasString('src'),
                                           partial_mock.HasString('root'))
 
+  def testPrepareLsbRelease(self):
+    """Tests that lsb-release is created correctly."""
+    generator = self.GetDlcGenerator()
+    dlc_dir = os.path.join(self.tempdir, 'dlc_dir')
+
+    generator.PrepareLsbRelease(dlc_dir)
+
+    self.assertEqual(osutils.ReadFile(os.path.join(dlc_dir, 'etc/lsb-release')),
+                     'DLC_ID=%s\nDLC_NAME=%s\n' %  (_ID, _NAME))
+
   def testGetImageloaderJsonContent(self):
     """Test that GetImageloaderJsonContent returns correct content."""
     blocks = 100