/* Resize the partition and notify the kernel. * returns: * CGPT_OK for resize successful or nothing to do * CGPT_FAILED on error */ static int resize_partition(CgptResizeParams *params, blkid_dev dev) { char *disk_devname; struct drive drive; GptHeader *header; GptEntry *entry; int gpt_retval, entry_index, entry_count; uint64_t free_bytes, last_free_lba, entry_size_lba; if ((disk_devname = dev_to_wholedevname(dev)) == NULL) { Error("Failed to find whole disk device for %s\n", blkid_dev_devname(dev)); return CGPT_FAILED; } if (DriveOpen(disk_devname, &drive, 0, O_RDWR) != CGPT_OK) { free(disk_devname); return CGPT_FAILED; } free(disk_devname); if (CGPT_OK != ReadPMBR(&drive)) { Error("Unable to read PMBR\n"); goto nope; } if (GPT_SUCCESS != (gpt_retval = GptSanityCheck(&drive.gpt))) { Error("GptSanityCheck() returned %d: %s\n", gpt_retval, GptError(gpt_retval)); goto nope; } // If either table is bad fix it! (likely if disk was extended) GptRepair(&drive.gpt); header = (GptHeader*)drive.gpt.primary_header; last_free_lba = header->last_usable_lba; entry_count = GetNumberOfEntries(&drive); entry_index = dev_to_partno(dev) - 1; if (entry_index < 0 || entry_index >= entry_count) { Error("Kernel and GPT disagree on the number of partitions!\n"); goto nope; } entry = GetEntry(&drive.gpt, PRIMARY, entry_index); // Scan entire table to determine if entry can grow. for (int i = 0; i < entry_count; i++) { GptEntry *other = GetEntry(&drive.gpt, PRIMARY, i); if (GuidIsZero(&other->type)) continue; if (other->starting_lba > entry->ending_lba && other->starting_lba - 1 < last_free_lba) { last_free_lba = other->starting_lba - 1; } } // Exit without doing anything if the size is too small free_bytes = (last_free_lba - entry->ending_lba) * drive.gpt.sector_bytes; if (entry->ending_lba >= last_free_lba || free_bytes < params->min_resize_bytes) { if (DriveClose(&drive, 0) != CGPT_OK) return CGPT_FAILED; else return CGPT_OK; } // Update and test partition table in memory entry->ending_lba = last_free_lba; entry_size_lba = entry->ending_lba - entry->starting_lba; UpdateAllEntries(&drive); gpt_retval = CheckEntries((GptEntry*)drive.gpt.primary_entries, (GptHeader*)drive.gpt.primary_header); if (gpt_retval != GPT_SUCCESS) { Error("CheckEntries() returned %d: %s\n", gpt_retval, GptError(gpt_retval)); goto nope; } // Notify kernel of new partition size via an ioctl. if (blkpg_resize_partition(drive.fd, dev_to_partno(dev), entry->starting_lba * drive.gpt.sector_bytes, entry_size_lba * drive.gpt.sector_bytes) < 0) { Error("Failed to notify kernel of new partition size: %s\n" "Leaving existing partition table in place.\n", strerror(errno)); goto nope; } UpdatePMBR(&drive, PRIMARY); if (WritePMBR(&drive) != CGPT_OK) { Error("Failed to write legacy MBR.\n"); goto nope; } // Whew! we made it! Flush to disk. return DriveClose(&drive, 1); nope: DriveClose(&drive, 0); return CGPT_FAILED; }
int resize_gpt_partition(const char *devname, __u64 new_size) { unsigned char buf[SECTOR_SIZE*GPT_DATA_SIZE]; // LBA1 header, LBA2-34 partition entry int fd; int part, ret; struct GptHeader *pt; struct GptEntry *pe; __u32 pt_crc32, pe_crc32, orig_crc; off_t size; __u64 tmp; ret = has_partition(devname, &part); if (ret) return ret; if (!part) return 0; ret = ploop_get_size(devname, &size); if (ret) return ret; // Resize up to max available space if (new_size == 0) new_size = size; if (new_size > size) { ploop_err(0, "Unable to resize GPT partition:" " incorrect parameter new_size=%llu size=%lu", new_size, (long)size); return SYSEXIT_PARAM; } ploop_log(1, "Resizing GPT partition to %ld", (long)new_size); fd = open(devname, O_RDWR); if (fd == -1) { ploop_err(errno, "open %s", devname); return SYSEXIT_OPEN; } // skip LBA0 Protective MBR ret = pread(fd, buf, sizeof(buf), SECTOR_SIZE); if (ret == -1) { ploop_err(errno, "pread %s", devname); goto err; } pt = (struct GptHeader *)buf; pe = (struct GptEntry *)(&buf[SECTOR_SIZE * GPT_HEADER_SIZE]); // Validate crc orig_crc = pt->header_crc32; pt->header_crc32 = 0; pt_crc32 = ploop_crc32((unsigned char *)pt, pt->header_size); if (pt_crc32 != orig_crc) { ploop_err(0, "GPT validation failed orig crc %x != %x", orig_crc, pt_crc32); ret = -1; goto err; } // change GPT header pt->alternate_lba = new_size - 1; pt->last_usable_lba = new_size - GPT_DATA_SIZE - 1; pe->ending_lba = (pt->last_usable_lba >> 3 << 3) - 1; // Recalculate crc32 pe_crc32 = ploop_crc32((unsigned char *)pe, SECTOR_SIZE * GPT_PT_ENTRY_SIZE); pt->partition_entry_array_crc32 = pe_crc32; pt->header_crc32 = 0; pt_crc32 = ploop_crc32((unsigned char *)pt, pt->header_size); pt->header_crc32 = pt_crc32; ploop_log(0, "Storing GPT"); ret = pwrite(fd, pt, SECTOR_SIZE * GPT_DATA_SIZE, SECTOR_SIZE); if (ret == -1) { ploop_err(errno, "Failed to store primary GPT %s", devname); goto err; } ret = fsync(fd); if (ret) { ploop_err(errno, "Can't fsync %s", devname); ret = SYSEXIT_FSYNC; goto err; } // Store secondary GPT entries tmp = pt->my_lba; pt->my_lba = pt->alternate_lba; pt->alternate_lba = tmp; pt->partition_entry_lba = pt->last_usable_lba + 1; // Recalculate crc32 pt->header_crc32 = 0; pt_crc32 = ploop_crc32((unsigned char *)pt, pt->header_size); pt->header_crc32 = pt_crc32; ret = pwrite(fd, pe, SECTOR_SIZE * GPT_PT_ENTRY_SIZE, (new_size - GPT_DATA_SIZE)*SECTOR_SIZE); if (ret == -1) { ploop_err(errno, "Failed to store secondary GPT %s", devname); goto err; } // Store Secondary GPT header ret = pwrite(fd, pt, SECTOR_SIZE, (new_size - GPT_HEADER_SIZE)*SECTOR_SIZE); if (ret == -1) { ploop_err(errno, "Failed to store secondary GPT header %s", devname); goto err; } if (fsync(fd)) { ploop_err(errno, "Can't fsync %s", devname); ret = SYSEXIT_FSYNC; goto err; } blkpg_resize_partition(fd, pe); ret = 0; err: close(fd); if (ret < 0) ret = SYSEXIT_CHANGE_GPT; return ret; }