From 4832cbb6ef0fe4a403dfe8bf081fb85e1077f90e Mon Sep 17 00:00:00 2001 From: Charlie Root Date: Mon, 28 Dec 2015 14:34:13 +0100 Subject: [PATCH] Align partitions to 1MiB. --- sbin/gpt/gpt.8 | 6 ++++++ sbin/gpt/map.c | 13 +++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/sbin/gpt/gpt.8 b/sbin/gpt/gpt.8 index 274b81b..5b50580 100644 --- a/sbin/gpt/gpt.8 +++ b/sbin/gpt/gpt.8 @@ -156,6 +156,12 @@ UUID. You may also specify any symbolic name in the system .Xr uuids 5 files. +.Pp +.Em NOTE! +If you don't specify a beginning sector with +.Fl b Ar number +, the new partition will be aligned to 1MiB in size and position +(in case of 512 byte sector sizes). .\" ==== boot ==== .It Nm Ic boot Ar device ... The diff --git a/sbin/gpt/map.c b/sbin/gpt/map.c index 7bf152c..53c4fe2 100644 --- a/sbin/gpt/map.c +++ b/sbin/gpt/map.c @@ -34,6 +34,10 @@ #include "map.h" +#define ROUNDTO 2048 // must be a power of two +#define ROUNDDOWN(x) ((x-1) & ~(ROUNDTO-1)) +#define ROUNDUP(x) (ROUNDDOWN(x) + ROUNDTO) + int lbawidth; static map_t *mediamap; @@ -141,17 +145,22 @@ map_alloc(off_t start, off_t size) off_t delta; map_t *m; + if (start == 0 && size != 0) + size = ROUNDUP(size); for (m = mediamap; m != NULL; m = m->map_next) { if (m->map_type != MAP_TYPE_UNUSED || m->map_start < 2) continue; if (start != 0 && m->map_start > start) return (NULL); - delta = (start != 0) ? start - m->map_start : 0; + delta = (start != 0) ? start - m->map_start : ROUNDUP(m->map_start) - m->map_start; if (size == 0 || m->map_size - delta >= size) { if (m->map_size - delta <= 0) continue; - if (size == 0) + if (size == 0) { size = m->map_size - delta; + if (start == 0) + size = ROUNDDOWN(size); + } return (map_add(m->map_start + delta, size, MAP_TYPE_GPT_PART, NULL)); } -- 2.6.3