From 4a424ecabbb14e230be45864ee7eeca3722d49e8 Mon Sep 17 00:00:00 2001 From: Simon Westphahl Date: Wed, 13 May 2020 07:56:16 +0200 Subject: [PATCH] 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 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 --- diskimage_builder/block_device/level1/partitioning.py | 2 +- diskimage_builder/block_device/tests/test_gpt.py | 2 +- diskimage_builder/block_device/tests/test_lvm.py | 2 +- ...lock-device-update-partition-table-eb4ff077f90dfb19.yaml | 6 ++++++ 4 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 releasenotes/notes/block-device-update-partition-table-eb4ff077f90dfb19.yaml diff --git a/diskimage_builder/block_device/level1/partitioning.py b/diskimage_builder/block_device/level1/partitioning.py index 83e5b7bf2..078fd6a70 100644 --- a/diskimage_builder/block_device/level1/partitioning.py +++ b/diskimage_builder/block_device/level1/partitioning.py @@ -206,7 +206,7 @@ class Partitioning(PluginBase): # now all the partitions are created, get device-mapper to # mount them if not os.path.exists("/.dockerenv"): - exec_sudo(["kpartx", "-avs", self.device_path]) + exec_sudo(["kpartx", "-uvs", self.device_path]) else: # If running inside Docker, make our nodes manually, # because udev will not be working. kpartx cannot run in diff --git a/diskimage_builder/block_device/tests/test_gpt.py b/diskimage_builder/block_device/tests/test_gpt.py index db046282d..c3978db45 100644 --- a/diskimage_builder/block_device/tests/test_gpt.py +++ b/diskimage_builder/block_device/tests/test_gpt.py @@ -70,7 +70,7 @@ class TestGPT(tc.TestGraphGeneration): cmd_sequence = [ mock.call(parted_cmd), mock.call(['sync']), - mock.call(['kpartx', '-avs', '/dev/loopX']) + mock.call(['kpartx', '-uvs', '/dev/loopX']) ] self.assertEqual(mock_exec_sudo.call_count, len(cmd_sequence)) mock_exec_sudo.assert_has_calls(cmd_sequence) diff --git a/diskimage_builder/block_device/tests/test_lvm.py b/diskimage_builder/block_device/tests/test_lvm.py index fd45f1701..757453afc 100644 --- a/diskimage_builder/block_device/tests/test_lvm.py +++ b/diskimage_builder/block_device/tests/test_lvm.py @@ -512,7 +512,7 @@ class TestLVM(tc.TestGraphGeneration): mock.call.create_mbr(), # now mount partitions mock.call.sudo_part(['sync']), - mock.call.sudo_part(['kpartx', '-avs', '/dev/fake/image0']), + mock.call.sudo_part(['kpartx', '-uvs', '/dev/fake/image0']), # now create lvm environment mock.call.sudo_lvm(['pvcreate', '/dev/fake/root', '--force']), mock.call.sudo_lvm( diff --git a/releasenotes/notes/block-device-update-partition-table-eb4ff077f90dfb19.yaml b/releasenotes/notes/block-device-update-partition-table-eb4ff077f90dfb19.yaml new file mode 100644 index 000000000..a807c05e6 --- /dev/null +++ b/releasenotes/notes/block-device-update-partition-table-eb4ff077f90dfb19.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - 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.