diff mbox

[Branch,~linaro-image-tools/linaro-image-tools/trunk] Rev 450: Update rootfs /etc/network/interfaces with interfaces specified in the hwpack.

Message ID 20111020070110.31412.37006.launchpad@ackee.canonical.com
State Accepted
Headers show

Commit Message

Mattias Backman Oct. 20, 2011, 7:01 a.m. UTC
Merge authors:
  Mattias Backman (mabac)
Related merge proposals:
  https://code.launchpad.net/~mabac/linaro-image-tools/bug-629049-interfaces/+merge/79822
  proposed by: Mattias Backman (mabac)
  review: Approve - Ricardo Salveti (rsalveti)
  review: Approve - James Westby (james-w)
------------------------------------------------------------
revno: 450 [merge]
committer: Mattias Backman <mattias.backman@linaro.org>
branch nick: linaro-image-tools
timestamp: Thu 2011-10-20 08:55:24 +0200
message:
  Update rootfs /etc/network/interfaces with interfaces specified in the hwpack.
modified:
  linaro-media-create
  linaro_image_tools/media_create/boards.py
  linaro_image_tools/media_create/rootfs.py
  linaro_image_tools/media_create/tests/test_media_create.py


--
lp:linaro-image-tools
https://code.launchpad.net/~linaro-image-tools/linaro-image-tools/trunk

You are subscribed to branch lp:linaro-image-tools.
To unsubscribe from this branch go to https://code.launchpad.net/~linaro-image-tools/linaro-image-tools/trunk/+edit-subscription
diff mbox

Patch

=== modified file 'linaro-media-create'
--- linaro-media-create	2011-10-12 09:54:29 +0000
+++ linaro-media-create	2011-10-19 12:19:33 +0000
@@ -181,6 +181,6 @@ 
             create_swap = True
         populate_rootfs(ROOTFS_DIR, ROOT_DISK, root_partition, args.rootfs,
             rootfs_uuid, create_swap, str(args.swap_file),
-            board_config.mmc_part_offset)
+            board_config.mmc_part_offset, board_config)
 
     print "Done creating Linaro image on %s" % args.device

=== modified file 'linaro_image_tools/media_create/boards.py'
--- linaro_image_tools/media_create/boards.py	2011-10-06 09:15:07 +0000
+++ linaro_image_tools/media_create/boards.py	2011-10-19 12:19:33 +0000
@@ -313,9 +313,13 @@ 
             cls.load_addr = cls.get_metadata_field('load_addr')
             cls.dtb_addr = cls.get_metadata_field('dtb_addr')
             cls.serial_tty = cls.get_metadata_field('serial_tty')
-            cls.wired_interfaces = cls.get_metadata_field('wired_interfaces')
-            cls.wireless_interfaces = cls.get_metadata_field(
+            wired_interfaces = cls.get_metadata_field('wired_interfaces')
+            if wired_interfaces is not None:
+                cls.wired_interfaces = wired_interfaces.split(' ')
+            wireless_interfaces = cls.get_metadata_field(
                 'wireless_interfaces')
+            if wireless_interfaces is not None:
+                cls.wireless_interfaces = wireless_interfaces.split(' ')
             cls.vmlinuz = cls.get_metadata_field('kernel_file')
             cls.initrd = cls.get_metadata_field('initrd_file')
             cls.dtb_file = cls.get_metadata_field('dtb_file')

=== modified file 'linaro_image_tools/media_create/rootfs.py'
--- linaro_image_tools/media_create/rootfs.py	2011-07-25 16:48:37 +0000
+++ linaro_image_tools/media_create/rootfs.py	2011-10-19 13:59:30 +0000
@@ -43,7 +43,7 @@ 
 
 def populate_rootfs(content_dir, root_disk, partition, rootfs_type,
                     rootfs_uuid, should_create_swap, swap_size,
-                    partition_offset):
+                    partition_offset, board_config=None):
     """Populate the rootfs and make the necessary tweaks to make it usable.
 
     This consists of:
@@ -89,6 +89,30 @@ 
         print "\nCreating /etc/flash-kernel.conf\n"
         create_flash_kernel_config(root_disk, 1 + partition_offset)
 
+        if board_config is not None:
+            print "\nUpdating /etc/network/interfaces\n"
+            update_network_interfaces(root_disk, board_config)
+
+
+def update_network_interfaces(root_disk, board_config):
+    interfaces = []
+    if board_config.wired_interfaces is not None:
+        interfaces.extend(board_config.wired_interfaces)
+    if board_config.wireless_interfaces is not None:
+        interfaces.extend(board_config.wireless_interfaces)
+
+    if_path = os.path.join(root_disk, 'etc', 'network', 'interfaces')
+    if os.path.exists(if_path):
+        with open(if_path) as if_file:
+            config = if_file.read()
+    else:
+        config = ''
+    for interface in interfaces:
+        if interface not in config:
+            config += "auto %(if)s\niface %(if)s inet dhcp\n" % ({ 'if': interface })
+    if config != '':
+        write_data_to_protected_file(if_path, config)
+
 
 def create_flash_kernel_config(root_disk, boot_partition_number):
     """Create a flash-kernel.conf file under root_disk/etc.

=== modified file 'linaro_image_tools/media_create/tests/test_media_create.py'
--- linaro_image_tools/media_create/tests/test_media_create.py	2011-10-04 15:21:57 +0000
+++ linaro_image_tools/media_create/tests/test_media_create.py	2011-10-19 13:59:30 +0000
@@ -101,6 +101,7 @@ 
     populate_rootfs,
     rootfs_mount_options,
     write_data_to_protected_file,
+    update_network_interfaces,
     )
 from linaro_image_tools.media_create.tests.fixtures import (
     CreateTarballFixture,
@@ -355,7 +356,7 @@ 
         class config(BoardConfig):
             pass
         config.set_metadata('ahwpack.tar.gz')
-        self.assertEquals(data_to_set, config.wired_interfaces)
+        self.assertEquals(data_to_set.split(' '), config.wired_interfaces)
 
     def test_sets_wireless_interfaces(self):
         self.useFixture(MockSomethingFixture(
@@ -369,7 +370,7 @@ 
         class config(BoardConfig):
             pass
         config.set_metadata('ahwpack.tar.gz')
-        self.assertEquals(data_to_set, config.wireless_interfaces)
+        self.assertEquals(data_to_set.split(' '), config.wireless_interfaces)
 
     def test_sets_mmc_id(self):
         self.useFixture(MockSomethingFixture(
@@ -2389,6 +2390,59 @@ 
         self.assertFalse(
             has_space_left_for_swap('/', swap_size_in_megs))
 
+    def test_update_interfaces_no_interfaces_no_update(self):
+        tempdir = self.useFixture(CreateTempDirFixture()).get_temp_dir()
+        os.makedirs(os.path.join(tempdir, 'etc', 'network'))
+        if_path = os.path.join(tempdir, 'etc', 'network', 'interfaces')
+        class board_config(boards.BoardConfig):
+            pass
+
+        update_network_interfaces(tempdir, board_config)
+        self.assertFalse(os.path.exists(if_path))
+
+    def test_update_interfaces_creates_entry(self):
+        tempdir = self.useFixture(CreateTempDirFixture()).get_temp_dir()
+        os.makedirs(os.path.join(tempdir, 'etc', 'network'))
+        if_path = os.path.join(tempdir, 'etc', 'network', 'interfaces')
+        class board_config(boards.BoardConfig):
+            wired_interfaces = ['eth0']
+
+        expected = 'auto eth0\n' \
+            'iface eth0 inet dhcp\n'
+        update_network_interfaces(tempdir, board_config)
+        self.assertEqual(expected, open(if_path).read())
+
+    def test_update_interfaces_creates_entries(self):
+        tempdir = self.useFixture(CreateTempDirFixture()).get_temp_dir()
+        os.makedirs(os.path.join(tempdir, 'etc', 'network'))
+        if_path = os.path.join(tempdir, 'etc', 'network', 'interfaces')
+        class board_config(boards.BoardConfig):
+            wired_interfaces = ['eth0', 'eth1']
+            wireless_interfaces = ['wlan0']
+
+        expected = 'auto %(if)s\n' \
+            'iface %(if)s inet dhcp\n'
+
+        update_network_interfaces(tempdir, board_config)
+        self.assertIn(expected % {'if': 'eth1'}, open(if_path).read())
+        self.assertIn(expected % {'if': 'eth0'}, open(if_path).read())
+        self.assertIn(expected % {'if': 'wlan0'}, open(if_path).read())
+
+    def test_update_interfaces_leaves_original(self):
+        tempdir = self.useFixture(CreateTempDirFixture()).get_temp_dir()
+        os.makedirs(os.path.join(tempdir, 'etc', 'network'))
+        if_path = os.path.join(tempdir, 'etc', 'network', 'interfaces')
+        with open(if_path, 'w') as interfaces:
+            interfaces.write('Original contents of file.\n')
+        class board_config(boards.BoardConfig):
+            wired_interfaces = ['eth0']
+
+        expected = 'Original contents of file.\n' \
+            'auto eth0\n' \
+            'iface eth0 inet dhcp\n'
+        update_network_interfaces(tempdir, board_config)
+        self.assertEqual(expected, open(if_path).read())
+
     def test_write_data_to_protected_file(self):
         fixture = self.useFixture(MockCmdRunnerPopenFixture())
         data = 'foo'