[PATCH] Generic partition creation interface.
This creates a generic partition table interface. One immediate advantage is that you can create GPT partitions which can be larger than 2 TB. For example, here is a 1 exabyte partition:
<fs> sparse /mnt/tmp/test/test.img 1E <fs> run <fs> part-init gpt /dev/vda <fs> part-add /dev/vda primary 0 0 <fs> list-devices /dev/vda <fs> list-partitions /dev/vda1 <fs> blockdev-getsize64 /dev/vda1 1099511627775983104
Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into Xen guests. http://et.redhat.com/~rjones/virt-p2v
This just tidies us the API over the previous version: (1) part-init now takes the parameters in (device, parttype) order, so it's consistent with the other calls. (2) guestfs_part_bootable -> guestfs_part_set_bootable, so we can add guestfs_part_get_bootable in future. (3) The documentation for part-init is changed to make it clear that only MS-DOS and GPT partition table types are supported. The others are best-effort. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://et.redhat.com/~rjones/libguestfs/ See what it can do: http://et.redhat.com/~rjones/libguestfs/recipes.html
This adds a "part-disk" command which can be used to quickly partition a whole disk into a single partition. Updates the tests and a recipe to use it. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://et.redhat.com/~rjones/virt-df/
After a bit of discussion with Jim Meyering I've decided to withdraw this and rewrite it based on libparted. Using the parted command line tool is just too unpredictable. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://et.redhat.com/~rjones/libguestfs/ See what it can do: http://et.redhat.com/~rjones/libguestfs/recipes.html
On Thu, Nov 05, 2009 at 01:15:03PM +0000, Richard W.M. Jones wrote:
After a bit of discussion with Jim Meyering I've decided to withdraw this and rewrite it based on libparted. Using the parted command line tool is just too unpredictable.
After going down this rathole I decided maybe that wasn't such a good plan after all. The following patch goes back to using the command line interface, but we have made guestfs_part_disk work for both MBR and GPT formats (thanks to Jim). Most of the rest of the patch is concerned with updating documentation and examples to emphasize using guestfs_part* instead of guestfs_sfdisk* calls. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://et.redhat.com/~rjones/virt-df/
Richard W.M. Jones wrote:
On Thu, Nov 05, 2009 at 01:15:03PM +0000, Richard W.M. Jones wrote:
After a bit of discussion with Jim Meyering I've decided to withdraw this and rewrite it based on libparted. Using the parted command line tool is just too unpredictable.
After going down this rathole I decided maybe that wasn't such a good plan after all.
The following patch goes back to using the command line interface, but we have made guestfs_part_disk work for both MBR and GPT formats (thanks to Jim).
Most of the rest of the patch is concerned with updating documentation and examples to emphasize using guestfs_part* instead of guestfs_sfdisk* calls.
Rich.
-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://et.redhat.com/~rjones/virt-df/
From 74914b6fd255d849c8d5caf482e9c727f0a13afd Mon Sep 17 00:00:00 2001 From: Richard Jones <rjones@redhat.com> Date: Wed, 4 Nov 2009 23:15:26 +0000 Subject: [PATCH] Generic partition creation interface.
The current sfdisk interface works, but is somewhat limited since it can only deal with MBR-style partitions. For disks larger than 2TB, MBR partitions are unsuitable and we need to provide access to other partition table types (eg. GPT).
Hi Rich, This all looks fine. Haven't tested yet, though. Two comments: ...
+int +do_part_add (const char *device, const char *prlogex, + int64_t startsect, int64_t endsect) +{ + char *err; + int r; + char startstr[32]; + char endstr[32]; + + /* Check and translate prlogex. */ + if (strcmp (prlogex, "primary") == 0 || + strcmp (prlogex, "logical") == 0 || + strcmp (prlogex, "extended") == 0) + ; + else if (strcmp (prlogex, "p") == 0) + prlogex = "primary"; + else if (strcmp (prlogex, "l") == 0) + prlogex = "logical"; + else if (strcmp (prlogex, "e") == 0) + prlogex = "extended"; + else { + reply_with_error ("part-add: unknown partition type: %s: this should be \"primary\", \"logical\" or \"extended\"", prlogex); + return -1;
Did you consider enforcing this only for msdos partition tables and allowing arbitrary names for GPT? ...
+int +do_part_disk (const char *device, const char *parttype) +{ + char *err; + int r; + const char *startstr; + const char *endstr; + + parttype = check_parttype (parttype); + if (!parttype) { + reply_with_error ("part-disk: unknown partition type: common choices are \"gpt\" and \"msdos\""); + return -1; + } + + /* Voooooodooooooooo (thanks Jim Meyering for working this out). */ + if (strcmp (parttype, "msdos") == 0) { + startstr = "1s"; + endstr = "-1s"; + } else if (strcmp (parttype, "gpt") == 0) { + startstr = "34s"; + endstr = "-34s"; + } else { + /* untested */ + startstr = "1s"; + endstr = "-1s"; + } + + r = command (NULL, &err, + "/sbin/parted", "-s", "--", device, + "mklabel", parttype, + /* This slightly wrong - in the GPT case it will give + * the partition the name "primary". We have to give + * it some name, so this is as good as any. + */ + "mkpart", "primary", startstr, endstr,
I created many GPT partition tables with "primary" as the name of each partition, before I noticed the error. To avoid giving the impression that libguestfs is doing this out of ignorance, how about using e.g., strcmp (parttype, "gpt") == 0 ? "p1" : "primary" ...
On Thu, Nov 05, 2009 at 05:19:33PM +0100, Jim Meyering wrote:
+int +do_part_add (const char *device, const char *prlogex, + int64_t startsect, int64_t endsect) +{ + char *err; + int r; + char startstr[32]; + char endstr[32]; + + /* Check and translate prlogex. */ + if (strcmp (prlogex, "primary") == 0 || + strcmp (prlogex, "logical") == 0 || + strcmp (prlogex, "extended") == 0) + ; + else if (strcmp (prlogex, "p") == 0) + prlogex = "primary"; + else if (strcmp (prlogex, "l") == 0) + prlogex = "logical"; + else if (strcmp (prlogex, "e") == 0) + prlogex = "extended"; + else { + reply_with_error ("part-add: unknown partition type: %s: this should be \"primary\", \"logical\" or \"extended\"", prlogex); + return -1;
Did you consider enforcing this only for msdos partition tables and allowing arbitrary names for GPT?
This is a bit tricky from the API point of view. If we were going to allow names too, we'd want something like: guestfs_part_add (g, device, prlogex, name, startsect, endsect); with name being an optional string and we'd have to enforce that the string is not set for msdos and set for gpt. Of course in guestfs_part_add we don't know the partition table type, unless we saved it from an earlier call or look it up, both of which are hairy. It's even worse though: $ parted -s /tmp/test.img -- \ mklabel gpt \ mkpart primary 34s 256s \ mkpart primary 257s -34s $ parted /tmp/test.img print WARNING: You are not superuser. Watch out for permissions. Model: (file) Disk /tmp/test.img: 10.5MB Sector size (logical/physical): 512B/512B Partition Table: gpt Number Start End Size File system Name Flags 1 17.4kB 132kB 114kB primary 2 132kB 10.5MB 10.3MB primary We've now got two GPT partitions named "primary". (At least parted allows this and doesn't barf). The undocumented name feature of parted mkpart is a minefield ... I've now added a guestfs_part_set_name call to the API [patch to follow] so at least people can change the name later. [in do_part_disk]
I created many GPT partition tables with "primary" as the name of each partition, before I noticed the error. To avoid giving the impression that libguestfs is doing this out of ignorance, how about using e.g.,
strcmp (parttype, "gpt") == 0 ? "p1" : "primary"
I agree in the guestfs_part_disk case we can do better because we do know the partition name, so this is a very sensible change. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into Xen guests. http://et.redhat.com/~rjones/virt-p2v
** NOT TO BE PUSHED ** This doesn't quite work at the moment. All the tests pass, except: test_list_partitions_0 wipes the disk and creates a single partition covering the whole disk. However the test fails because it thinks the disk has 3 partitions. At the moment I have no idea why it would think this. Anyway, the patch has evolved further: (1) guestfs_set_part_name can be used to set the name of a partition. (2) In guestfs_part_disk, if GPT set the partition name to "p1". But in guestfs_part_add we still have the bug that the partition name is set to "primary" for GPT partition tables. At least this is now documented as a bug. (3) Work around an apparent bug in parted where it fails to do ioctl(BLKRRPART) [re-read the partition table], by catching this case and rereading the partition table ourselves. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://et.redhat.com/~rjones/libguestfs/ See what it can do: http://et.redhat.com/~rjones/libguestfs/recipes.html
Richard W.M. Jones wrote: ...
(3) Work around an apparent bug in parted where it fails to do ioctl(BLKRRPART) [re-read the partition table], by catching this case and rereading the partition table ourselves.
Hi Rich, I haven't looked at that patch yet, but this part rings big bells: parted-2.0+ (not yet in rawhide) has had significant changes in this area.
I think this patch is looking in good shape to be reviewed and applied now. On Mon, Nov 09, 2009 at 10:08:32AM +0100, Jim Meyering wrote:
Richard W.M. Jones wrote: ...
(3) Work around an apparent bug in parted where it fails to do ioctl(BLKRRPART) [re-read the partition table], by catching this case and rereading the partition table ourselves.
Hi Rich,
I haven't looked at that patch yet, but this part rings big bells: parted-2.0+ (not yet in rawhide) has had significant changes in this area.
The workaround should be safe, in that it looks for the specific error string that parted 1.9 throws and tries to reread the partition table. Rereading the partition table is always safe, even if we do it at the wrong time.
** NOT TO BE PUSHED ** This doesn't quite work at the moment. All the tests pass, except: test_list_partitions_0 wipes the disk and creates a single partition covering the whole disk. However the test fails because it thinks the disk has 3 partitions. At the moment I have no idea why it would think this.
If this is still a problem, let me know and I'll take a look.
This fixed itself. That's the sort of bug I like. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones New in Fedora 11: Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 70 libraries supprt'd http://fedoraproject.org/wiki/MinGW http://www.annexia.org/fedora_mingw
Richard W.M. Jones wrote:
** NOT TO BE PUSHED ** This doesn't quite work at the moment. All the tests pass, except: test_list_partitions_0 wipes the disk and creates a single partition covering the whole disk. However the test fails because it thinks the disk has 3 partitions. At the moment I have no idea why it would think this.
If this is still a problem, let me know and I'll take a look.
participants (2)
-
Jim Meyering -
Richard W.M. Jones