Richard W.M. Jones
2015-Nov-27 10:33 UTC
[Libguestfs] [PATCH 1/2] resize: Work around regression in sfdisk (RHBZ#1285847).
'sfdisk --part-type' used to merely set the MBR ID byte in the partition. However since sfdisk was rewritten, it now "helpfully" corrupts the first sector of the partition if you change the type byte from an ordinary partition to an extended partition. So we need to change the order in which we sets the partition type byte, to do it before copying the partition content. This fixes a major regression in the way that virt-resize handles any guest with logical partitions (specifically Ubuntu guests). --- resize/resize.ml | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/resize/resize.ml b/resize/resize.ml index ff10fca..5ca2f20 100644 --- a/resize/resize.ml +++ b/resize/resize.ml @@ -1159,6 +1159,28 @@ read the man page virt-resize(1). g#part_add "/dev/sdb" "primary" p.p_target_start p.p_target_end ) partitions; + (* Set bootable and MBR IDs. Do this *before* copying over the data, + * because the rewritten sfdisk "helpfully" overwrites the partition + * table in the first sector of an extended partition if a partition + * is changed from primary to extended. Thus we need to set the + * MBR ID before doing the copy so sfdisk doesn't corrupt things. + *) + let set_partition_bootable_and_id p + if p.p_bootable then + g#part_set_bootable "/dev/sdb" p.p_target_partnum true; + + may (g#part_set_name "/dev/sdb" p.p_target_partnum) p.p_label; + may (g#part_set_gpt_guid "/dev/sdb" p.p_target_partnum) p.p_guid; + + match parttype, p.p_id with + | GPT, GPT_Type gpt_type -> + g#part_set_gpt_type "/dev/sdb" p.p_target_partnum gpt_type + | MBR, MBR_ID mbr_id -> + g#part_set_mbr_id "/dev/sdb" p.p_target_partnum mbr_id + | GPT, (No_ID|MBR_ID _) | MBR, (No_ID|GPT_Type _) -> () + in + List.iter set_partition_bootable_and_id partitions; + (* Copy over the data. *) let copy_partition p match p.p_operation with @@ -1202,26 +1224,6 @@ read the man page virt-resize(1). in List.iter copy_partition partitions; - (* Set bootable and MBR IDs. Do this *after* copying over the data, - * so that we can magically change the primary partition to an extended - * partition if necessary. - *) - let set_partition_bootable_and_id p - if p.p_bootable then - g#part_set_bootable "/dev/sdb" p.p_target_partnum true; - - may (g#part_set_name "/dev/sdb" p.p_target_partnum) p.p_label; - may (g#part_set_gpt_guid "/dev/sdb" p.p_target_partnum) p.p_guid; - - match parttype, p.p_id with - | GPT, GPT_Type gpt_type -> - g#part_set_gpt_type "/dev/sdb" p.p_target_partnum gpt_type - | MBR, MBR_ID mbr_id -> - g#part_set_mbr_id "/dev/sdb" p.p_target_partnum mbr_id - | GPT, (No_ID|MBR_ID _) | MBR, (No_ID|GPT_Type _) -> () - in - List.iter set_partition_bootable_and_id partitions; - (* Fix the bootloader if we aligned the first partition. *) if align_first_partition_and_fix_bootloader then ( (* See can_fix_boot_loader above. *) -- 2.5.0
Richard W.M. Jones
2015-Nov-27 10:33 UTC
[Libguestfs] [PATCH 2/2] resize: Add a regression test for RHBZ#1285847.
--- tests/regressions/Makefile.am | 2 ++ tests/regressions/rhbz1285847.sh | 72 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100755 tests/regressions/rhbz1285847.sh diff --git a/tests/regressions/Makefile.am b/tests/regressions/Makefile.am index 18a4f37..a62a935 100644 --- a/tests/regressions/Makefile.am +++ b/tests/regressions/Makefile.am @@ -47,6 +47,7 @@ EXTRA_DIST = \ rhbz1175196.sh \ rhbz1232192.sh \ rhbz1232192.xml \ + rhbz1285847.sh \ test-noexec-stack.pl TESTS = \ @@ -75,6 +76,7 @@ TESTS = \ rhbz1174551.sh \ rhbz1175196.sh \ rhbz1232192.sh \ + rhbz1285847.sh \ test-big-heap \ test-noexec-stack.pl diff --git a/tests/regressions/rhbz1285847.sh b/tests/regressions/rhbz1285847.sh new file mode 100755 index 0000000..f34fe54 --- /dev/null +++ b/tests/regressions/rhbz1285847.sh @@ -0,0 +1,72 @@ +#!/bin/bash - +# libguestfs +# Copyright (C) 2015 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +# Regression test for virt-resize handling of logical volumes +# https://bugzilla.redhat.com/1285847 + +set -e +export LANG=C + +if [ -n "$SKIP_TEST_RHBZ1285847_SH" ]; then + echo "$0: test skipped because environment variable is set" + exit 77 +fi + +if ! ../../resize/virt-resize --help >/dev/null 2>&1; then + echo "$0: test skipped because virt-resize was not built" + exit 77 +fi + +rm -f rhbz1285847.img rhbz1285847-2.img rhbz1285847.out + +# Create a disk with logical volumes. +guestfish <<EOF + sparse rhbz1285847.img 4G + run + part-init /dev/sda mbr + # This partition layout is copied from the Ubuntu 14.04 + # virt-builder template. + part-add /dev/sda p $((1048576/512)) $(((3221225471+1)/512)) + part-add /dev/sda e $((3222273024/512)) $(((4293918719+1)/512)) + part-add /dev/sda l $((3222274048/512)) $(((4293918719+1)/512)) + + mkfs ext4 /dev/sda1 + mkswap /dev/sda5 + + echo "Partitions before resizing:" + part-list /dev/sda + + echo "Filesystems before resizing:" + list-filesystems +EOF + +truncate -s 10G rhbz1285847-2.img +virt-resize rhbz1285847.img rhbz1285847-2.img --expand /dev/sda2 + +# Check that the filesystems made it across. +guestfish -a rhbz1285847-2.img run : list-filesystems > rhbz1285847.out + +if [ "$(cat rhbz1285847.out)" != "/dev/sda1: ext4 +/dev/sda2: unknown +/dev/sda5: swap" ]; then + echo "$0: unexpected result:" + cat rhbz1285847.out + exit 1 +fi + +rm rhbz1285847.img rhbz1285847-2.img rhbz1285847.out -- 2.5.0
Maybe Matching Threads
- [PATCH 3/3] resize: copy GPT partition flags
- [PATCH v2 3/3] resize: copy GPT partition flags
- [PATCH v3 7/7] resize: add support to resize logical partitions
- [PATCH RFC] resize: add p_mbr_p_type as member of type partition
- [PATCH V5 2/4] resize: add support to resize logical partitions