int ploop_discard_get_stat_by_dev(const char *device, const char *mount_point, struct ploop_discard_stat *pd_stat) { int err; struct statfs stfs; struct stat st, balloon_stat; off_t ploop_size; char image[PATH_MAX]; err = get_balloon(mount_point, &balloon_stat, NULL); if (err) return err; err = statfs(mount_point, &stfs); if (err == -1) { ploop_err(errno, "statfs(%s) failed", mount_point); return 1; } err = ploop_get_size(device, &ploop_size); if (err) return 1; err = ploop_find_top_delta_name_and_format(device, image, sizeof(image), NULL, 0); if (err) return 1; err = stat(image, &st); if (err == -1) { ploop_err(errno, "stat(%s) failed", image); return 1; } pd_stat->ploop_size = S2B(ploop_size) - balloon_stat.st_size; pd_stat->image_size = st.st_size; pd_stat->data_size = pd_stat->ploop_size - stfs.f_bfree * stfs.f_bsize; pd_stat->balloon_size = balloon_stat.st_size; return 0; }
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; }