2018-01-15 03:44:10 +00:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
|
|
|
|
import fixtures
|
|
|
|
import logging
|
|
|
|
import mock
|
|
|
|
import os
|
|
|
|
|
|
|
|
import diskimage_builder.block_device.tests.test_config as tc
|
|
|
|
|
|
|
|
from diskimage_builder.block_device.blockdevice import BlockDeviceState
|
|
|
|
from diskimage_builder.block_device.config import config_tree_to_graph
|
|
|
|
from diskimage_builder.block_device.config import create_graph
|
|
|
|
from diskimage_builder.block_device.level0.localloop import image_create
|
|
|
|
from diskimage_builder.block_device.level1.partition import PartitionNode
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class TestGPT(tc.TestGraphGeneration):
|
|
|
|
|
|
|
|
@mock.patch('diskimage_builder.block_device.level1.partitioning.exec_sudo')
|
|
|
|
def test_gpt_efi(self, mock_exec_sudo):
|
|
|
|
# Test the command-sequence for a GPT/EFI partition setup
|
|
|
|
tree = self.load_config_file('gpt_efi.yaml')
|
|
|
|
config = config_tree_to_graph(tree)
|
|
|
|
|
|
|
|
state = BlockDeviceState()
|
|
|
|
|
|
|
|
graph, call_order = create_graph(config, self.fake_default_config,
|
|
|
|
state)
|
|
|
|
|
|
|
|
# Create a fake temp backing file (we check the size of it,
|
|
|
|
# etc).
|
|
|
|
# TODO(ianw): exec_sudo is generically mocked out, thus the
|
|
|
|
# actual creation is mocked out ... but we could do this
|
|
|
|
# without root and use parted to create the partitions on this
|
|
|
|
# for slightly better testing. An exercise for another day...
|
|
|
|
self.tmp_dir = fixtures.TempDir()
|
|
|
|
self.useFixture(self.tmp_dir)
|
|
|
|
self.image_path = os.path.join(self.tmp_dir.path, "image.raw")
|
|
|
|
# should be sparse...
|
|
|
|
image_create(self.image_path, 1024 * 1024 * 1024)
|
|
|
|
logger.debug("Temp image in %s", self.image_path)
|
|
|
|
|
|
|
|
# Fake state for the loopback device
|
|
|
|
state['blockdev'] = {}
|
|
|
|
state['blockdev']['image0'] = {}
|
|
|
|
state['blockdev']['image0']['image'] = self.image_path
|
|
|
|
state['blockdev']['image0']['device'] = "/dev/loopX"
|
|
|
|
|
|
|
|
for node in call_order:
|
|
|
|
if isinstance(node, PartitionNode):
|
|
|
|
node.create()
|
|
|
|
|
|
|
|
# check the parted call looks right
|
2018-06-27 03:51:04 +00:00
|
|
|
parted_cmd = ['sgdisk', self.image_path,
|
|
|
|
'-n', '1:0:+8M', '-t', '1:EF00', '-c', '1:ESP',
|
|
|
|
'-n', '2:0:+8M', '-t', '2:EF02', '-c', '2:BSP',
|
|
|
|
'-n', '3:0:+1006M', '-t', '3:8300', '-c', '3:Root Part']
|
2018-01-15 03:44:10 +00:00
|
|
|
cmd_sequence = [
|
2018-06-27 03:51:04 +00:00
|
|
|
mock.call(parted_cmd),
|
2018-01-15 03:44:10 +00:00
|
|
|
mock.call(['sync']),
|
Use kpartx option to update partition mappings
Fix cases of 'mkfs' failing because the partitions never showed up. Partition
mappings will now be updated instead of just adding them with 'kpartx'. That
means that 'kpartx' will also remove devmappings for deleted partitions.
Traceback of failing mkfs call:
2020-05-11 22:03:25.523 | INFO diskimage_builder.block_device.utils [-] Calling [sudo sync]
2020-05-11 22:03:25.539 | INFO diskimage_builder.block_device.utils [-] Calling [sudo kpartx -avs /dev/loop0]
2020-05-11 22:03:25.581 | INFO diskimage_builder.block_device.utils [-] Calling [sudo mkfs -t ext4 -i 4096 -J size=64 -L cloudimg-rootfs -U 21c6f9eb-4d52-4e5c-b9b7-796735de8909 -q /dev/mapper/loop0p1]
2020-05-11 22:03:25.700 | ERROR diskimage_builder.block_device.blockdevice [-] Create failed; rollback initiated
2020-05-11 22:03:25.700 | Traceback (most recent call last):
2020-05-11 22:03:25.700 | File "/home/zuul/dib/lib/python3.6/site-packages/diskimage_builder/block_device/blockdevice.py", line 406, in cmd_create
2020-05-11 22:03:25.700 | node.create()
2020-05-11 22:03:25.700 | File "/home/zuul/dib/lib/python3.6/site-packages/diskimage_builder/block_device/level2/mkfs.py", line 133, in create
2020-05-11 22:03:25.700 | exec_sudo(cmd)
2020-05-11 22:03:25.700 | File "/home/zuul/dib/lib/python3.6/site-packages/diskimage_builder/block_device/utils.py", line 143, in exec_sudo
2020-05-11 22:03:25.700 | raise e
2020-05-11 22:03:25.700 | diskimage_builder.block_device.exception.BlockDeviceSetupException: exec_sudo failed
2020-05-11 22:03:25.700 | INFO diskimage_builder.block_device.level0.localloop [-] loopdev detach
2020-05-11 22:03:25.701 | INFO diskimage_builder.block_device.utils [-] Calling [sudo losetup -d /dev/loop0]
2020-05-11 22:03:25.732 | INFO diskimage_builder.block_device.level0.localloop [-] Remove image file [/tmp/dib_image.muyw7t1h/image0.raw]
2020-05-11 22:03:25.734 | ERROR diskimage_builder.block_device.blockdevice [-] Rollback complete, exiting
2020-05-11 22:03:25.740 | Traceback (most recent call last):
2020-05-11 22:03:25.740 | File "/home/zuul/dib/bin/dib-block-device", line 8, in <module>
2020-05-11 22:03:25.740 | sys.exit(main())
2020-05-11 22:03:25.740 | File "/home/zuul/dib/lib/python3.6/site-packages/diskimage_builder/block_device/cmd.py", line 120, in main
2020-05-11 22:03:25.740 | return bdc.main()
2020-05-11 22:03:25.740 | File "/home/zuul/dib/lib/python3.6/site-packages/diskimage_builder/block_device/cmd.py", line 115, in main
2020-05-11 22:03:25.740 | self.args.func()
2020-05-11 22:03:25.740 | File "/home/zuul/dib/lib/python3.6/site-packages/diskimage_builder/block_device/cmd.py", line 36, in cmd_create
2020-05-11 22:03:25.740 | self.bd.cmd_create()
2020-05-11 22:03:25.740 | File "/home/zuul/dib/lib/python3.6/site-packages/diskimage_builder/block_device/blockdevice.py", line 406, in cmd_create
2020-05-11 22:03:25.740 | node.create()
2020-05-11 22:03:25.740 | File "/home/zuul/dib/lib/python3.6/site-packages/diskimage_builder/block_device/level2/mkfs.py", line 133, in create
2020-05-11 22:03:25.740 | exec_sudo(cmd)
2020-05-11 22:03:25.740 | File "/home/zuul/dib/lib/python3.6/site-packages/diskimage_builder/block_device/utils.py", line 143, in exec_sudo
2020-05-11 22:03:25.740 | raise e
2020-05-11 22:03:25.740 | diskimage_builder.block_device.exception.BlockDeviceSetupException: exec_sudo failed
Change-Id: I374f7f22f9e93ef35eb5813712ca59e75f0733e8
Related-Bug: #1698337
2020-05-13 05:56:16 +00:00
|
|
|
mock.call(['kpartx', '-uvs', '/dev/loopX'])
|
2018-01-15 03:44:10 +00:00
|
|
|
]
|
|
|
|
self.assertEqual(mock_exec_sudo.call_count, len(cmd_sequence))
|
|
|
|
mock_exec_sudo.assert_has_calls(cmd_sequence)
|
|
|
|
|
|
|
|
# Check two new partitions appear in state correctly
|
|
|
|
self.assertDictEqual(state['blockdev']['ESP'],
|
|
|
|
{'device': '/dev/mapper/loopXp1'})
|
|
|
|
self.assertDictEqual(state['blockdev']['BSP'],
|
|
|
|
{'device': '/dev/mapper/loopXp2'})
|
2018-06-27 03:51:04 +00:00
|
|
|
self.assertDictEqual(state['blockdev']['Root Part'],
|
2018-01-15 03:44:10 +00:00
|
|
|
{'device': '/dev/mapper/loopXp3'})
|