return swap_size;
}
-static unsigned long long hw_root_size(struct hw_destination* dest) {
- unsigned long long root_size;
-
- if (dest->size < MB2BYTES(2048))
- root_size = MB2BYTES(1024);
-
- else if (dest->size >= MB2BYTES(2048) && dest->size <= MB2BYTES(3072))
- root_size = MB2BYTES(1536);
-
- else
- root_size = MB2BYTES(2048);
-
- return root_size;
-}
-
static unsigned long long hw_boot_size(struct hw_destination* dest) {
return MB2BYTES(128);
}
// Add some more space for partition tables, etc.
dest->size -= MB2BYTES(1);
+ // The disk has to have at least 2GB
+ if (dest->size <= MB2BYTES(2048))
+ return -1;
+
// Determine partition table
dest->part_table = HW_PART_TABLE_MSDOS;
}
dest->size_boot = hw_boot_size(dest);
- dest->size_root = hw_root_size(dest);
+
+ // Determine the size of the data partition.
+ unsigned long long space_left = dest->size - \
+ (dest->size_bootldr + dest->size_boot);
+
+ // If we have less than 2GB left, we disable swap
+ if (space_left <= MB2BYTES(2048))
+ disable_swap = 1;
// Should we use swap?
if (disable_swap)
else
dest->size_swap = hw_swap_size(dest);
- // Determine the size of the data partition.
- unsigned long long used_space = dest->size_bootldr + dest->size_boot
- + dest->size_swap + dest->size_root;
+ // Subtract swap
+ space_left -= dest->size_swap;
- // Disk is way too small
- if (used_space >= dest->size)
- return -1;
-
- dest->size_data = dest->size - used_space;
-
- // If it gets too small, we remove the swap space.
- if (dest->size_data <= MB2BYTES(256)) {
- dest->size_data += dest->size_swap;
- dest->size_swap = 0;
- }
+ // Root is getting what ever is left
+ dest->size_root = space_left;
// Set partition names
if (dest->size_boot > 0) {
snprintf(dest->part_root, sizeof(dest->part_root), "%s%d", path, part_idx++);
- if (dest->size_data > 0)
- snprintf(dest->part_data, sizeof(dest->part_data), "%s%d", path, part_idx++);
- else
- *dest->part_data = '\0';
-
return 0;
}
part_start += dest->size_root;
}
- if (*dest->part_data) {
- asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd,
- (dest->part_table == HW_PART_TABLE_GPT) ? "DATA" : "primary",
- part_start, part_start + dest->size_data - 1);
-
- part_start += dest->size_data;
- }
-
if (dest->part_boot_idx > 0)
asprintf(&cmd, "%s set %d boot on", cmd, dest->part_boot_idx);
if (*dest->part_root && (try_open(dest->part_root) != 0))
continue;
- if (*dest->part_data && (try_open(dest->part_data) != 0))
- continue;
-
// All partitions do exist, exiting the loop.
break;
}
if (r)
return r;
- // data
- if (*dest->part_data) {
- r = hw_format_filesystem(dest->part_data, dest->filesystem, output);
- if (r)
- return r;
- }
-
return 0;
}
}
}
- // data
- if (*dest->part_data) {
- snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_DATA);
- mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO);
-
- r = hw_mount(dest->part_data, target, filesystem, 0);
- if (r) {
- hw_umount_filesystems(dest, prefix);
-
- return r;
- }
- }
-
// swap
if (*dest->part_swap) {
r = swapon(dest->part_swap, 0);
return -1;
}
- // data
- if (*dest->part_data) {
- snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_DATA);
- r = hw_umount(target);
- if (r)
- return -1;
- }
-
// swap
if (*dest->part_swap) {
swapoff(dest->part_swap);
free(uuid);
}
- // data
- if (*dest->part_data) {
- uuid = hw_get_uuid(dest->part_data);
-
- if (uuid) {
- fprintf(f, FSTAB_FMT, uuid, "/var", "auto", "defaults", 1, 1);
- free(uuid);
- }
- }
-
fclose(f);
return 0;