int main(int argc, char *argv[]) { int rc; int cmd; int done = 0; // Check arguments if (argc == 1) { devname = "/dev/hd0"; } else if (argc == 2) { devname = argv[1]; } else { printf("usage: fdisk <device>\n"); return 1; } // Open device hdev = open(devname, O_RDWR | O_BINARY); if (hdev < 0) { printf("%s: error %d opening device\n", devname, errno); return 1; } // Get disk geometry rc = ioctl(hdev, IOCTL_GETGEOMETRY, &geom , sizeof(struct geometry)); if (rc < 0) { printf("%s: error %d determining disk geometry\n", devname, errno); close(hdev); return 1; } // Read master boot record rc = read_mbr(); if (rc < 0 && errno != EINVAL) { printf("%s: error %d reading master boot record\n", devname, errno); close(hdev); return 1; } // Ask to create new master boot record if the existing is invalid if (rc < 0 && errno == EINVAL) { printf("%s: invalid master boot record\n", devname); if (ask("create new master boot record (y/n)? ", "yn") == 'y') { memcpy(&mbr, bootrecord, sizeof(mbr)); } } // Read commands printf("(a)dd (b)oot (c)ommit (d)elete (l)ist (m)br (h)elp e(x)it\n"); while (!done) { cmd = ask("fdisk> ", "abcdlmhx?"); switch (cmd) { case 'a': add_partition(); break; case 'b': set_boot_part(); break; case 'c': commit_mbr(); break; case 'd': delete_partition(); break; case 'l': list_partitions(); break; case 'm': clear_mbr(); break; case 'h': case '?': help(); break; case 'x': done = 1; break; } } // Close device close(hdev); return 0; }
/* * This routine implements the 'select' command. It allows the user * to make a pre-defined partition map the current map. */ int p_select() { struct partition_info *pptr, *parts; u_ioparam_t ioparam; int i, index, deflt, *defltptr = NULL; blkaddr_t b_cylno; #if defined(i386) blkaddr_t cyl_offset; #endif parts = cur_dtype->dtype_plist; /* * If there are no pre-defined maps for this disk type, it's * an error. */ if (parts == NULL) { err_print("No defined partition tables.\n"); return (-1); } /* * Loop through the pre-defined maps and list them by name. If * the current map is one of them, make it the default. If any * the maps are unnamed, label them as such. */ for (i = 0, pptr = parts; pptr != NULL; pptr = pptr->pinfo_next) { if (cur_parts == pptr) { deflt = i; defltptr = &deflt; } if (pptr->pinfo_name == NULL) fmt_print(" %d. unnamed\n", i++); else fmt_print(" %d. %s\n", i++, pptr->pinfo_name); } ioparam.io_bounds.lower = 0; ioparam.io_bounds.upper = i - 1; /* * Ask which map should be made current. */ index = input(FIO_INT, "Specify table (enter its number)", ':', &ioparam, defltptr, DATA_INPUT); for (i = 0, pptr = parts; i < index; i++, pptr = pptr->pinfo_next) ; if (cur_label == L_TYPE_EFI) { enter_critical(); cur_disk->disk_parts = cur_parts = pptr; exit_critical(); fmt_print("\n"); return (0); } #if defined(i386) /* * Adjust for the boot and alternate sectors partition - assuming that * the alternate sectors partition physical location follows * immediately the boot partition and partition sizes are * expressed in multiple of cylinder size. */ cyl_offset = pptr->pinfo_map[I_PARTITION].dkl_cylno + 1; if (pptr->pinfo_map[J_PARTITION].dkl_nblk != 0) { cyl_offset = pptr->pinfo_map[J_PARTITION].dkl_cylno + ((pptr->pinfo_map[J_PARTITION].dkl_nblk + (spc() - 1)) / spc()); } #else /* !defined(i386) */ b_cylno = 0; #endif /* defined(i386) */ /* * Before we blow the current map away, do some limits checking. */ for (i = 0; i < NDKMAP; i++) { #if defined(i386) if (i == I_PARTITION || i == J_PARTITION || i == C_PARTITION) { b_cylno = 0; } else if (pptr->pinfo_map[i].dkl_nblk == 0) { /* * Always accept starting cyl 0 if the size is 0 also */ b_cylno = 0; } else { b_cylno = cyl_offset; } #endif /* defined(i386) */ if (pptr->pinfo_map[i].dkl_cylno < b_cylno || pptr->pinfo_map[i].dkl_cylno > (ncyl-1)) { err_print( "partition %c: starting cylinder %d is out of range\n", (PARTITION_BASE+i), pptr->pinfo_map[i].dkl_cylno); return (0); } if (pptr->pinfo_map[i].dkl_nblk > ((ncyl - pptr->pinfo_map[i].dkl_cylno) * spc())) { err_print( "partition %c: specified # of blocks, %u, " "is out of range\n", (PARTITION_BASE+i), pptr->pinfo_map[i].dkl_nblk); return (0); } } /* * Lock out interrupts so the lists don't get mangled. */ enter_critical(); /* * If the old current map is unnamed, delete it. */ if (cur_parts != NULL && cur_parts != pptr && cur_parts->pinfo_name == NULL) delete_partition(cur_parts); /* * Make the selected map current. */ cur_disk->disk_parts = cur_parts = pptr; #if defined(_SUNOS_VTOC_16) for (i = 0; i < NDKMAP; i++) { cur_parts->vtoc.v_part[i].p_start = (blkaddr_t)(cur_parts->pinfo_map[i].dkl_cylno * (nhead * nsect)); cur_parts->vtoc.v_part[i].p_size = (blkaddr_t)cur_parts->pinfo_map[i].dkl_nblk; } #endif /* defined(_SUNOS_VTOC_16) */ exit_critical(); fmt_print("\n"); return (0); }
/** * @brief Delete partition function. * @param disk[in]: General disk structure. * @param partno[in]: Partition number. * @return None. */ void gp_delete_partition(struct gendisk *disk, int partno) { return delete_partition(disk, partno); }
int main() { uint32_t i; int32_t x, y, rx, ry; init_partition(&partition, 0); printf("\n*** Initial partition ***\n"); print_partition_details(&partition); for (i=0; i<40; i++) { x = random() % N; printf("--- testing %"PRId32" ---\n", x); y = partition_find(&partition, x); if (y < 0) { printf("item %"PRId32" not present; adding it\n", x); partition_add(&partition, x); } else { printf("root[%"PRId32"] = %"PRId32"\n", x, y); } } printf("\n*** Partition ***\n"); print_partition_details(&partition); printf("\n"); print_partition(&partition); printf("\n"); for (x=0; x<N; x++) { aux[x] = partition_find(&partition, x); } for (i=1; i<10; i++) { do { x = random() % N; } while (aux[x] < 0); do { y = random() % N; } while (x == y || aux[y] < 0); x = partition_find(&partition, x); y = partition_find(&partition, y); if (x != y) { printf("--- Merging %"PRId32" and %"PRId32" ---\n", x, y); partition_merge(&partition, x, y); } } printf("\n"); print_partition(&partition); reset_partition(&partition); for (i=0; i<100; i++) { x = random() % 300; rx = partition_find(&partition, x); if (rx < 0) { partition_add(&partition, x); rx = x; } y = random() % 300; ry = partition_find(&partition, y); if (ry < 0) { partition_add(&partition, y); ry = y; } if (rx != ry) { partition_merge(&partition, rx, ry); } } printf("\n\n"); print_partition(&partition); delete_partition(&partition); return 0; }