Bug 1248115

Summary: sfdisk: you cannot change a partition into an extended one or vice versa
Product: [Fedora] Fedora Reporter: Marian Csontos <mcsontos>
Component: util-linuxAssignee: Karel Zak <kzak>
Status: CLOSED ERRATA QA Contact: Fedora Extras Quality Assurance <extras-qa>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 22CC: jonathan, kzak, madebr, mbooth, mcsontos, ptoscano, rjones, virt-maint
Target Milestone: ---   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: util-linux-2.26.2-3.fc22 Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2015-08-19 07:59:52 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Attachments:
Description Flags
virt-builder output none

Description Marian Csontos 2015-07-29 15:42:06 UTC
Description of problem:
Using virt-builder to create debian-6 VM fails with following:

    [root@hp-ms-01-c14 ~]# virt-builder debian-6 -o /dev/fedora_hp-ms-01-c14/debian-6 
    [   0.8] Downloading: http://libguestfs.org/download/builder/debian-6.xz
    ######################################################################## 100.0%
    [  88.8] Planning how to build this image
    [  88.8] Uncompressing
    [ 116.7] Resizing (using virt-resize) to expand the disk to 10.0G
    virt-resize: error: libguestfs error: part_set_mbr_id: sfdisk --part-type: 
    You cannot change a partition into an extended one or vice versa. Delete it 
    first.
    sfdisk: /dev/sdb: partition 2: failed to set partition type
    
    If reporting bugs, run virt-resize with debugging enabled and include the 
    complete output:
    
      virt-resize -v -x [...]


Version-Release number of selected component (if applicable):
libguestfs-tools-c-1.29.50-1.fc22.x86_64

How reproducible:
100% (with downloaded debian image)

Partitions in the image:

    [root@hp-ms-01-c14 f22]# fdisk /dev/mapper/fedora_hp--ms--01--c14-debian--6
    
    Welcome to fdisk (util-linux 2.26.2).
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    
    
    Command (m for help): p
    Disk /dev/mapper/fedora_hp--ms--01--c14-debian--6: 10 GiB, 10737418240 bytes, 20971520 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 65536 bytes / 65536 bytes
    Disklabel type: dos
    Disk identifier: 0x00083915
    
    Device                                         Boot    Start      End  Sectors  Size Id Type
    /dev/mapper/fedora_hp--ms--01--c14-debian--6p1 *        2048 20522623 20520576  9.8G 83 Linux
    /dev/mapper/fedora_hp--ms--01--c14-debian--6p2      20522624 20969089   446466  218M  f W95 Ext'd (LBA)
    /dev/mapper/fedora_hp--ms--01--c14-debian--6p5      20522626 20969089   446464  218M 82 Linux swap / Solaris

fsck'ing the partition everything seems sane:

    [root@hp-ms-01-c14 f22]# fsck /dev/mapper/fedora_hp--ms--01--c14-debian--6p1 
    fsck from util-linux 2.26.2
    e2fsck 1.42.12 (29-Aug-2014)
    /dev/mapper/fedora_hp--ms--01--c14-debian--6p1 has gone 625 days without being checked, check forced.
    Pass 1: Checking inodes, blocks, and sizes
    Pass 2: Checking directory structure
    Pass 3: Checking directory connectivity
    Pass 4: Checking reference counts
    Pass 5: Checking group summary information
    /dev/mapper/fedora_hp--ms--01--c14-debian--6p1: 24410/248000 files (0.9% non-contiguous), 171566/992000 blocks

Comment 1 Richard W.M. Jones 2015-07-29 16:12:58 UTC
This works for me on Fedora 22, so I guess it must have something
to do with the version of sfdisk on Debian vs Fedora (the program
was substantially rewritten upstream recently).

I have:
util-linux-2.26.2-1.fc22.x86_64

What version of sfdisk/util-linux do you have?

Comment 2 Richard W.M. Jones 2015-07-29 16:13:34 UTC
Sorry, I now see you're actually using Fedora host (not Debian
as I thought for some reason).  What version of util-linux is it anyway?

Comment 3 Marian Csontos 2015-07-30 07:26:53 UTC
Same here: util-linux-2.26.2-1.fc22.x86_64

The difference may be I was extracting to an existing thin-LV so the image was resized.

Trying virt-builder with `--size 10G` option: it fails:

[root@hp-ms-01-c14 ~]# virt-builder debian-6 -o /tmp/test-debian-6 --size 10G
[   0.8] Downloading: http://libguestfs.org/download/builder/debian-6.xz
[   1.9] Planning how to build this image
[   1.9] Uncompressing
[  29.7] Resizing (using virt-resize) to expand the disk to 10.0G
virt-resize: error: libguestfs error: part_set_mbr_id: sfdisk --part-type: 
You cannot change a partition into an extended one or vice versa. Delete it 
first.
sfdisk: /dev/sdb: partition 2: failed to set partition type

If reporting bugs, run virt-resize with debugging enabled and include the 
complete output:

  virt-resize -v -x [...]

Attaching output of the same command with -x -v.

Comment 4 Marian Csontos 2015-07-30 07:27:38 UTC
Created attachment 1057539 [details]
virt-builder output

Comment 5 Richard W.M. Jones 2015-07-30 09:23:25 UTC
Oh I see, I can reproduce it also using the --size 10G parameter.

This seems to be a bug in sfdisk as far as I can tell.  Here is
a reproducer:

$ rm -f disk.img 
$ truncate -s 4G disk.img
$ fdisk disk.img

Welcome to fdisk (util-linux 2.26.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x81a5bda3.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): e
Partition number (1-4, default 1): 
First sector (2048-8388607, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-8388607, default 8388607): 

Created a new partition 1 of type 'Extended' and of size 4 GiB.

Command (m for help): p
Disk disk.img: 4 GiB, 4294967296 bytes, 8388608 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x81a5bda3

Device     Boot Start     End Sectors Size Id Type
disk.img1        2048 8388607 8386560   4G  5 Extended

Command (m for help): w
The partition table has been altered.
Syncing disks.

### Notice that the MBR type byte is 05:

$ virt-filesystems -a disk.img --all --long -h
Name       Type        VFS      Label  MBR  Size  Parent
/dev/sda1  filesystem  unknown  -      -    1.0K  -
/dev/sda1  partition   -        -      05   1.0K  /dev/sda
/dev/sda   device      -        -      -    4.0G  -

### Now try setting the MBR type byte to 05.

$ sfdisk --part-type /dev/sda 1 5
You cannot change a partition into an extended one or vice versa. Delete it first.
sfdisk: /dev/sda: partition 1: failed to set partition type

Comment 6 Richard W.M. Jones 2015-07-30 09:25:32 UTC
Bleah; the sfdisk command there was obviously wrong.  However
it still fails when I use the correct command:

$ sfdisk --part-type disk.img 1 5
You cannot change a partition into an extended one or vice versa. Delete it first.
sfdisk: disk.img: partition 1: failed to set partition type

Comment 7 Karel Zak 2015-07-30 11:14:43 UTC
Fixed by upstream commit df15fb3d053716d03055df755911d66814054b4b. 
Thanks for your report!

Note that with the bugfix you still cannot change type of the extended partition which is already used by nested logical partition(s), but your use-case works as expected:

# sfdisk -ql /dev/sdc
Device     Boot Start     End Sectors  Size Id Type
/dev/sdc1        2048   22527   20480   10M 83 Linux
/dev/sdc2       22528 1023999 1001472  489M 83 Linux

# sfdisk /dev/sdc --part-type 2 5 
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

# sfdisk -ql /dev/sdc
Device     Boot Start     End Sectors  Size Id Type
/dev/sdc1        2048   22527   20480   10M 83 Linux
/dev/sdc2       22528 1023999 1001472  489M  5 Extended


The bugfix will be definitely in v2.27 (f23), is it necessary to backport it also to f22?

Comment 8 Richard W.M. Jones 2015-07-30 11:21:22 UTC
It would be nice to have it in F22, but don't sweat it if it's a
difficult backport.

Comment 9 Marian Csontos 2015-07-30 21:28:41 UTC
Thanks guys!

Comment 10 Fedora Update System 2015-08-13 09:36:58 UTC
util-linux-2.26.2-3.fc22 has been submitted as an update for Fedora 22.
https://admin.fedoraproject.org/updates/util-linux-2.26.2-3.fc22

Comment 11 Fedora Update System 2015-08-15 02:21:57 UTC
Package util-linux-2.26.2-3.fc22:
* should fix your issue,
* was pushed to the Fedora 22 testing repository,
* should be available at your local mirror within two days.
Update it with:
# su -c 'yum update --enablerepo=updates-testing util-linux-2.26.2-3.fc22'
as soon as you are able to.
Please go to the following url:
https://admin.fedoraproject.org/updates/FEDORA-2015-13513/util-linux-2.26.2-3.fc22
then log in and leave karma (feedback).

Comment 12 Fedora Update System 2015-08-19 07:59:52 UTC
util-linux-2.26.2-3.fc22 has been pushed to the Fedora 22 stable repository.  If problems still persist, please make note of it in this bug report.

Comment 13 Richard W.M. Jones 2015-09-22 07:59:04 UTC
*** Bug 1265078 has been marked as a duplicate of this bug. ***