Skip to contentSkip to conversation

block: online resize of disk partitions

PATCH Superseded Vivek Goyal 2 participants 9 messages Feb 13, 2012 – Feb 14, 2012

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover block: online resize of disk partitions

Hi,

Following patch adds support for online resizing of a partition. This patch is based on previously posted patches by Phillip Susi.

There are two patches. Out of which one is kernel patch and other one is util-linux patch to add support of a user space utility "resizepart" to allow resizing the partition.

This ioctl only resizes the partition size in kenrel and does not change the size on disk. A user needs to make sure that corresponding changes are made to disk data structures also using fdisk(or partx), if changes are to be retained across reboot.

I made some changes according to the feedback received last time. Following are changes since the version Phillip posted.

- RESIZE ioctl ignores the partition "start" and does not expect user to specify one. Caller needs to just specify "device", "partition number" and "size" of new partition.

- Got rid of part_nr_sects_write_begin/part_nr_sects_write_end functions and replaced these with single part_nr_sects_write().

- Some sequence counter related changes are simply lifted from i_size_write().

- Initialized part->nr_sects_seq using seqcount_init().

Phillip, do let me know if I should put your signed-off-by also in the patch.

Any feedback is welcome.

I did following test.

- Create a partition of 10MB on a disk using fdisk.
- Add this partition to a volume group
- Use fdisk to increase the partition size to 20MB. (First delete the partition and then create a new one of 20MB size).
- Use resizepart to extend partition size in kernel.

	resizepart /dev/sdc 1 40960

- Do pvresize on partition so that physical volume can be incrased in size online.
pvresize /dev/sda1

pvresize does recognize the new size. Also lsblk and /proc/partitions report the new size of partition.

Thanks
Vivek

[PATCH 1/2] block: add partition resize function to blkpg ioctl [PATCH 2/2] resizepart: Utility to resize a partition

#1 lore raw

PATCH 1/2 block: add partition resize function to blkpg ioctl

Add a new operation code ( BLKPG_RESIZE_PARTITION ) to the BLKPG ioctl that allows altering the size of an existing partition, even if it is currently in use.

---
 block/genhd.c             |   20 ++++++++++++----
 block/ioctl.c             |   51 +++++++++++++++++++++++++++++++++++++--
 block/partition-generic.c |    4 ++-
 include/linux/blkpg.h     |    1 +
 include/linux/genhd.h     |   57 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 124 insertions(+), 9 deletions(-)
block/genhd.c +15 -5
index 23b4f70..935e09b 100644
@@ -153,7 +153,7 @@ struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
 		part = rcu_dereference(ptbl->part[piter->idx]);
 		if (!part)
 			continue;
-		if (!part->nr_sects &&
+		if (!part_nr_sects_read(part) &&
 		    !(piter->flags & DISK_PITER_INCL_EMPTY) &&
 		    !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
 		      piter->idx == 0))
@@ -190,7 +190,7 @@ EXPORT_SYMBOL_GPL(disk_part_iter_exit);
 static inline int sector_in_part(struct hd_struct *part, sector_t sector)
 {
 	return part->start_sect <= sector &&
-		sector < part->start_sect + part->nr_sects;
+		sector < part->start_sect + part_nr_sects_read(part);
 }
 
 /**
@@ -765,8 +765,8 @@ void __init printk_all_partitions(void)
 
 			printk("%s%s %10llu %s %s", is_part0 ? "" : "  ",
 			       bdevt_str(part_devt(part), devt_buf),
-			       (unsigned long long)part->nr_sects >> 1,
-			       disk_name(disk, part->partno, name_buf), uuid);
+			       (unsigned long long)part_nr_sects_read(part) >> 1
+			       , disk_name(disk, part->partno, name_buf), uuid);
 			if (is_part0) {
 				if (disk->driverfs_dev != NULL &&
 				    disk->driverfs_dev->driver != NULL)
@@ -857,7 +857,7 @@ static int show_partition(struct seq_file *seqf, void *v)
 	while ((part = disk_part_iter_next(&piter)))
 		seq_printf(seqf, "%4d  %7d %10llu %s\n",
 			   MAJOR(part_devt(part)), MINOR(part_devt(part)),
-			   (unsigned long long)part->nr_sects >> 1,
+			   (unsigned long long)part_nr_sects_read(part) >> 1,
 			   disk_name(sgp, part->partno, buf));
 	disk_part_iter_exit(&piter);
 
@@ -1263,6 +1263,16 @@ struct gendisk *alloc_disk_node(int minors, int node_id)
 		}
 		disk->part_tbl->part[0] = &disk->part0;
 
+		/*
+		 * set_capacity() and get_capacity() currently don't use
+		 * seqcounter to read/update the part0->nr_sects. Still init
+		 * the counter as we can read the sectors in IO submission
+		 * patch using seqence counters.
+		 *
+		 * TODO: Ideally set_capacity() and get_capacity() should be
+		 * converted to make use of bd_mutex and sequence counters.
+		 */
+		seqcount_init(&disk->part0.nr_sects_seq);
 		hd_ref_init(&disk->part0);
 
 		disk->minors = minors;
block/ioctl.c +48 -3

69 lines not on this page · raw message

block/partition-generic.c +3 -1
index d06ec1c..363a6f6 100644
@@ -84,7 +84,7 @@ ssize_t part_size_show(struct device *dev,
 		       struct device_attribute *attr, char *buf)
 {
 	struct hd_struct *p = dev_to_part(dev);
-	return sprintf(buf, "%llu\n",(unsigned long long)p->nr_sects);
+	return sprintf(buf, "%llu\n",(unsigned long long)part_nr_sects_read(p));
 }
 
 static ssize_t part_ro_show(struct device *dev,
@@ -294,6 +294,8 @@ struct hd_struct *add_partition(struct gendisk *disk, int partno,
 		err = -ENOMEM;
 		goto out_free;
 	}
+
+	seqcount_init(&p->nr_sects_seq);
 	pdev = part_to_dev(p);
 
 	p->start_sect = start;
include/linux/blkpg.h +1 -0
index faf8a45..a851944 100644
@@ -40,6 +40,7 @@ struct blkpg_ioctl_arg {
 /* The subfunctions (for the op field) */
 #define BLKPG_ADD_PARTITION	1
 #define BLKPG_DEL_PARTITION	2
+#define BLKPG_RESIZE_PARTITION	3
 
 /* Sizes of name fields. Unused at present. */
 #define BLKPG_DEVNAMELTH	64
include/linux/genhd.h +57 -0

70 lines not on this page · raw message

Signature · 1 line
1.7.6.4
#2 lore raw

PATCH 2/2 resizepart: Utility to resize a partition

In reply to Vivek Goyal

A simple user space utility to resize an existing partition.

---
 partx/Makefile.am  |    4 ++--
 partx/partx.h      |   19 +++++++++++++++++++
 partx/resizepart.8 |   38 ++++++++++++++++++++++++++++++++++++++
 partx/resizepart.c |   30 ++++++++++++++++++++++++++++++
 4 files changed, 89 insertions(+), 2 deletions(-)
 create mode 100644 partx/resizepart.8
 create mode 100644 partx/resizepart.c
partx/Makefile.am +2 -2
index 080bc47..1f4dbf5 100644
@@ -1,7 +1,7 @@
 include $(top_srcdir)/config/include-Makefile.am
 
-usrsbin_exec_PROGRAMS = addpart delpart
-dist_man_MANS = addpart.8 delpart.8
+usrsbin_exec_PROGRAMS = addpart delpart resizepart
+dist_man_MANS = addpart.8 delpart.8 resizepart.8
 
 usrsbin_exec_PROGRAMS += partx
 partx_SOURCES = partx.c partx.h \
partx/partx.h +19 -0
index b40fa8f..7a509f3 100644
@@ -41,4 +41,23 @@ static inline int partx_add_partition(int fd, int partno,
 	return ioctl(fd, BLKPG, &a);
 }
 
+static inline int partx_resize_partition(int fd, int partno, unsigned long size)
+{
+	struct blkpg_ioctl_arg a;
+	struct blkpg_partition p;
+
+	p.pno = partno;
+	/* start is unused for resize operation. It can't be changed */
+	p.start = 0;
+	p.length = size << 9;
+	p.devname[0] = 0;
+	p.volname[0] = 0;
+	a.op = BLKPG_RESIZE_PARTITION;
+	a.flags = 0;
+	a.datalen = sizeof(p);
+	a.data = &p;
+
+	return ioctl(fd, BLKPG, &a);
+}
+
 #endif /*  UTIL_LINUX_PARTX_H */
partx/resizepart.8 new file +38 -0
new file mode 100644
index 0000000..0b47e81
@@ -0,0 +1,38 @@
+.\" resizepart.8 --
+.\" Copyright 2012 Vivek Goyal <[email protected]>
+.\" Copyright 2012 Red Hat, Inc.
+.\" May be distributed under the GNU General Public License
+.TH RESIZEPART 8 "February 2012" "util-linux" "System Administration"
+.SH NAME
+resizepart \-
+simple wrapper around the "resize partition" ioctl
+.SH SYNOPSIS
+.B resizepart
+.I device partition length
+.SH DESCRIPTION
+.B resizepart
+is a program that informs the Linux kernel of new partition size.
+
+This command doesn't manipulate partitions on hard drive.
+
+.SH PARAMETERS
+.TP
+.I device
+Specify the disk device.
+.TP
+.I partition
+Specify the partition number.
+.TP
+.I length
+Specify the length of the partition (in 512-byte sectors).
+
+.SH SEE ALSO
+.BR addpart (8),
+.BR delpart (8),
+.BR fdisk (8),
+.BR parted (8),
+.BR partprobe (8),
+.BR partx (8)
+.SH AVAILABILITY
+The addpart command is part of the util-linux package and is available from
+ftp://ftp.kernel.org/pub/linux/utils/util-linux/.
partx/resizepart.c new file +30 -0
new file mode 100644
index 0000000..e230fcd
@@ -0,0 +1,30 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+
+#include "partx.h"
+
+int
+main(int argc, char **argv)
+{
+	int fd;
+
+	if (argc != 4) {
+		fprintf(stderr,
+			"usage: %s diskdevice partitionnr length\n",
+			argv[0]);
+		exit(1);
+	}
+	if ((fd = open(argv[1], O_RDONLY)) < 0) {
+		perror(argv[1]);
+		exit(1);
+	}
+
+	if (partx_resize_partition(fd, atoi(argv[2]),
+				atoll(argv[3]))) {
+		perror("BLKPG");
+		exit(1);
+	}
+
+	return 0;
+}
Signature · 1 line
1.7.6.4
#3 lore raw
In reply to Vivek Goyal

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 14 lines · Vivek Goyal

Hi,

Following patch adds support for online resizing of a partition. This patch is based on previously posted patches by Phillip Susi.

There are two patches. Out of which one is kernel patch and other one is util-linux patch to add support of a user space utility "resizepart" to allow resizing the partition.

This ioctl only resizes the partition size in kenrel and does not change the size on disk. A user needs to make sure that corresponding changes are made to disk data structures also using fdisk(or partx), if changes are to be retained across reboot.

Quoted 6 lines

I made some changes according to the feedback received last time. Following are changes since the version Phillip posted.

- RESIZE ioctl ignores the partition "start" and does not expect user to specify one. Caller needs to just specify "device", "partition number" and "size" of new partition.

I would prefer that the start argument not be left undefined. I checked it to make sure it was unchanged as a simple sanity check to guard against user space being goofy, as well as to allow for the possibility of extending the interface in the future to allow changing the start as well as the length.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJPOYH+AAoJEJrBOlT6nu75rvwIALjEMrUZPEvwYzH9MI9wHmGg lZzeBTSalqEOWL/hM0Ge6o1Of1adjeEyB7riXi7Fbb4Z8U55oBkAyKcRD/fdm0EA 8SyfMnGnMqZjh6VthIZMw43HbNwHojXeu2ri4ygeUORMIrX8tmZLAePw6iuOWcX6 qxOZsJOz3CdoGy2yc362AuIQACKH6IfFimP81NH5uUA01DIawWpes3EuN+KcxOQP hlIeixVPzlNrXuDY/m35xlXNaXcTUqTYFJ30vvVn0/MHylAbfCXKx6P19jvPHTt9 j0OyBNVnsb/BCKsbSW89lfHoVPMeYC6Kz2u8d1AX5byCxcYcznqclA6S93/UUms= =ZABC
-----END PGP SIGNATURE-----

#4 lore raw
Quoted 21 lines · Phillip Susi

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 17 lines · Vivek Goyal

Hi,

Following patch adds support for online resizing of a partition. This patch is based on previously posted patches by Phillip Susi.

There are two patches. Out of which one is kernel patch and other one is util-linux patch to add support of a user space utility "resizepart" to allow resizing the partition.

This ioctl only resizes the partition size in kenrel and does not change the size on disk. A user needs to make sure that corresponding changes are made to disk data structures also using fdisk(or partx), if changes are to be retained across reboot.

I made some changes according to the feedback received last time. Following are changes since the version Phillip posted.

Quoted 7 lines
Quoted 3 lines

- RESIZE ioctl ignores the partition "start" and does not expect user to specify one. Caller needs to just specify "device", "partition number" and "size" of new partition.

I would prefer that the start argument not be left undefined. I checked it to make sure it was unchanged as a simple sanity check to guard against user space being goofy,

I am not sure that it is really helping. I looked at pvresize, lvresize and resize2fs user interfaces and a user has to just specify the new size of physical volume, logical volume, filesystem respectively. So it makes sense to keep partition resize interface inline with above tools.

So if a user anyway does not specify the start of sector, then tools shall have to first read it and then call the ioctl. If anyway tools are specifying, there is no much scope of screwing up the things?

Also in delete partition ioctl, we just ask the user the partition number being deleted. We don't ask for start and size of partition in an effort to verify that you are deleting the right partition.

Quoted 3 lines

as well as to allow for the
possibility of extending the interface in the future to allow changing the start as well as the length.

Even if we allow changing start at some point of time, then IOCTL can remain the same and just the implementation will change in a backward compatible manner. Old tools still will continue to work as they have always been, and new ones can start passing "start" too.

So I really did not find passing and checking "start" of partition very appealing.

Thanks
Vivek

#5 lore raw

On Mon, Feb 13, 2012 at 04:50:39PM -0500, Vivek Goyal wrote:

[..]

Quoted 8 lines
Quoted 3 lines

as well as to allow for the
possibility of extending the interface in the future to allow changing the start as well as the length.

Even if we allow changing start at some point of time, then IOCTL can remain the same and just the implementation will change in a backward compatible manner. Old tools still will continue to work as they have always been, and new ones can start passing "start" too.

Let me thake that back. Changes will not be backward compatible as if old tools run on new kernel specifying start as 0, then change will fail on newer kernels which might allow changing start too.

Do you really expect that changing start will really be useful? If yes, then it does make a case for not leaving "start" undefined.

Thanks
Vivek

#6 lore raw
In reply to Vivek Goyal

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 4 lines · Vivek Goyal

So if a user anyway does not specify the start of sector, then tools shall have to first read it and then call the ioctl. If anyway tools are specifying, there is no much scope of screwing up the things?

Tools certainly should know where the partition starts. I already have patched parted and partx to pass the current size, which they had trivially available. Making sure of that is a good sanity check to guard against, for instance, resizing the wrong partition.

Quoted 8 lines

Even if we allow changing start at some point of time, then IOCTL can remain the same and just the implementation will change in a backward compatible manner. Old tools still will continue to work as they have always been, and new ones can start passing "start" too.

So I really did not find passing and checking "start" of partition
very appealing.

That is exactly why passing and checking start is required. If it is entirely ignored, then the interface can not start using it in the future in a backward compatible way, because user mode tools will have grown used to passing in any kind of garbage, so if the kernel starts using it to alter the start position of the partition, older tools would randomly and accidentally be shifting the the start of partitions they just mean to change the length of.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJPOYw5AAoJEJrBOlT6nu75CV4H/2vyZwujRqb+mqTAfQ+/XJE7 E850b+MOFZEQEoKQy+XAeu15pmm/TIa8NTk83kBeoQb30P2sU814+rJjbMKpZ9x/ xc8DZVC+DdnLLJ8a9aQw5feV0nmy102QrL0CWS+Wi6vS3DLlLCQQ+nv7AZ1jPV0Z NPzqtmI/zBopf/F2IrL+5DLD9TAi7mf+Yv5IBb7w2JQSiU9LoBo9TTMJhPAhaqtC ihqSJ0Q/t0DG3WacK+8fwh6Jyta1Bx+YsoUzYijtsaH6GQ1zbe+3c44T1JMI+Zvq 30M8V7y1BsLhp+lfZSUlC5YvPpu7gCJ1lg2fpMz56q2DENURj3L1HkdFwPwfLcw= =TTAk
-----END PGP SIGNATURE-----

#7 lore raw
Quoted 9 lines · Phillip Susi

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 4 lines · Vivek Goyal

So if a user anyway does not specify the start of sector, then tools shall have to first read it and then call the ioctl. If anyway tools are specifying, there is no much scope of screwing up the things?

Quoted 4 lines

Tools certainly should know where the partition starts. I already have patched parted and partx to pass the current size, which they had trivially available. Making sure of that is a good sanity check to guard against, for instance, resizing the wrong partition.

Ok, so user still specifies just the new size of partition and tool passes in both the "start" and new "size" to the ioctl?

Quoted 10 lines
Quoted 9 lines

Even if we allow changing start at some point of time, then IOCTL can remain the same and just the implementation will change in a backward compatible manner. Old tools still will continue to work as they have always been, and new ones can start passing "start" too.

So I really did not find passing and checking "start" of partition
very appealing.

Quoted 7 lines

That is exactly why passing and checking start is required. If it is entirely ignored, then the interface can not start using it in the future in a backward compatible way, because user mode tools will have grown used to passing in any kind of garbage, so if the kernel starts using it to alter the start position of the partition, older tools would randomly and accidentally be shifting the the start of partitions they just mean to change the length of.

Ok, I think keeping the ioctl backward compatible in light of any future changes makes sense. I will change the patches to not ignore the partition "start" and repost.

Thanks for the comments.

Vivek

#8 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 2 lines · Vivek Goyal

Ok, so user still specifies just the new size of partition and tool passes in both the "start" and new "size" to the ioctl?

Bingo. Eventually I'm kicking around the idea of having the ioctl allow changing the start and modifying gparted to be able to use that on a mounted partition if the fs supports it ( I think btrfs could be made to without too much difficulty ). In the mean time, libparted enforces the start remaining unchanged and parted, gparted, and kpartx automatically use the existing start position.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJPObfXAAoJEJrBOlT6nu75IKwH/jKcbW6L/j/v7hF0rXHql7Tg /E17+L37YvtwNA3aiZvDmj6FWablT2Dmvi/zcPITJ0FOWtZ+HKQ6+1odqPWRkOl/ 4gQrcUyYn/OK/RqyqCM9O6AwpiKZCvAn9LmDc0BCKDptbKshXe+L2GmgOQbiFSdk 2rcbncXI9Sz1pt8nCP8M3gNdYIl6Ln2u0PxwohMoIVGFDhxETdzm8YfX9MfH4Ay6 FU+5chGJUEiXQgjDw5k+vgX85Xc4pC7IRR6vqAZ5I69RxWB4DBteiR0znBNBZG1Z Mg8cAJTku2n4fZojCAXv35i3gRa3bgwyHv0suaSc2BGKYEeAu41ncAC3q77Arp4= =Qxgd
-----END PGP SIGNATURE-----