2019-08-31 22:49:05 +03:00
|
|
|
|
{ stdenvNoCC, lib
|
|
|
|
|
, imageBuilder
|
|
|
|
|
, utillinux
|
|
|
|
|
}:
|
2019-08-27 21:14:03 +03:00
|
|
|
|
|
|
|
|
|
/* */ let scope = { "diskImage.makeMBR" =
|
|
|
|
|
|
|
|
|
|
let
|
2019-08-27 21:25:38 +03:00
|
|
|
|
inherit (lib) concatMapStringsSep optionalString;
|
2019-08-27 21:14:03 +03:00
|
|
|
|
|
|
|
|
|
# List of known mappings of MBR partition types to filesystems.
|
|
|
|
|
types = {
|
|
|
|
|
"FAT32" = "b";
|
|
|
|
|
"ESP" = "ef";
|
|
|
|
|
"ext2" = "83";
|
|
|
|
|
"ext3" = "83";
|
|
|
|
|
"ext4" = "83";
|
|
|
|
|
};
|
|
|
|
|
in
|
|
|
|
|
{
|
|
|
|
|
name
|
|
|
|
|
, partitions
|
2019-08-31 22:49:05 +03:00
|
|
|
|
# Without the prefixed `0x`
|
|
|
|
|
, diskID
|
2019-08-27 21:14:03 +03:00
|
|
|
|
}:
|
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
_name = name;
|
|
|
|
|
|
|
|
|
|
eachPart = partitions: fn: (
|
2019-08-27 21:25:38 +03:00
|
|
|
|
concatMapStringsSep "\n" (partition:
|
2019-08-27 21:14:03 +03:00
|
|
|
|
fn partition
|
|
|
|
|
) partitions);
|
2019-09-03 23:42:21 +03:00
|
|
|
|
|
|
|
|
|
# Default alignment.
|
|
|
|
|
alignment = toString (imageBuilder.size.MiB 1);
|
2019-08-27 21:14:03 +03:00
|
|
|
|
in
|
|
|
|
|
stdenvNoCC.mkDerivation rec {
|
|
|
|
|
name = "disk-image-${_name}";
|
|
|
|
|
filename = "${_name}.img";
|
|
|
|
|
img = "${placeholder "out"}/${filename}";
|
|
|
|
|
|
2019-08-31 22:49:05 +03:00
|
|
|
|
nativeBuildInputs = [
|
|
|
|
|
utillinux
|
|
|
|
|
];
|
|
|
|
|
|
2019-09-03 23:42:21 +03:00
|
|
|
|
buildCommand = let
|
|
|
|
|
# This fragment is used to compute the (aligned) size of the partition.
|
|
|
|
|
# It is used *only* to track the tally of the space used, thus the starting
|
|
|
|
|
# offset of the next partition. The filesystem sizes are untouched.
|
|
|
|
|
sizeFragment = ''
|
|
|
|
|
start=$totalSize
|
|
|
|
|
size=$(($(du --apparent-size -B 512 "$input_img" | awk '{ print $1 }') * 512))
|
|
|
|
|
size=$(( $(if (($size % ${alignment})); then echo 1; else echo 0; fi ) + size / ${alignment} ))
|
|
|
|
|
size=$(( size * ${alignment} ))
|
|
|
|
|
totalSize=$(( totalSize + size ))
|
2019-09-05 07:38:46 +03:00
|
|
|
|
echo "Partition: start $start | size $size | totalSize $totalSize"
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
# This fragment is used to add the desired gap to `totalSize`.
|
|
|
|
|
# We're setting `start` and `size` only to mirror the information shown
|
|
|
|
|
# for partitions.
|
|
|
|
|
# Do note that gaps are always aligned, so two gaps sized half the alignment
|
|
|
|
|
# would create 2× the space expected.
|
|
|
|
|
# What may *instead* be done at one point is always align `start` for partitions.
|
|
|
|
|
gapFragment = partition: ''
|
|
|
|
|
start=$totalSize
|
|
|
|
|
size=${toString partition.length}
|
|
|
|
|
size=$(( $(if (($size % ${alignment})); then echo 1; else echo 0; fi ) + size / ${alignment} ))
|
2019-11-22 04:41:55 +03:00
|
|
|
|
size=$(( size * ${alignment} ))
|
2019-09-05 07:38:46 +03:00
|
|
|
|
totalSize=$(( totalSize + size ))
|
|
|
|
|
echo "Gap: start $start | size $size | totalSize $totalSize"
|
2019-09-03 23:42:21 +03:00
|
|
|
|
'';
|
|
|
|
|
in ''
|
2019-08-27 21:14:03 +03:00
|
|
|
|
mkdir -p $out
|
|
|
|
|
|
|
|
|
|
cat <<EOF > script.sfdisk
|
|
|
|
|
label: dos
|
2019-09-04 07:45:59 +03:00
|
|
|
|
grain: 1024
|
2019-08-31 22:49:05 +03:00
|
|
|
|
label-id: 0x${diskID}
|
2019-08-27 21:14:03 +03:00
|
|
|
|
EOF
|
|
|
|
|
|
2019-09-03 23:42:21 +03:00
|
|
|
|
totalSize=${alignment}
|
2019-08-27 21:14:03 +03:00
|
|
|
|
echo
|
|
|
|
|
echo "Gathering information about partitions."
|
2019-09-05 07:38:46 +03:00
|
|
|
|
${eachPart partitions (partition:
|
|
|
|
|
if partition ? isGap && partition.isGap then
|
|
|
|
|
(gapFragment partition)
|
|
|
|
|
else
|
|
|
|
|
''
|
|
|
|
|
input_img="${partition}/${partition.filename}"
|
|
|
|
|
${sizeFragment}
|
|
|
|
|
echo " -> ${partition.name}: $size / ${partition.filesystemType}"
|
|
|
|
|
|
|
|
|
|
(
|
|
|
|
|
# The size is /1024; otherwise it's in sectors.
|
|
|
|
|
echo -n 'start='"$((start/1024))"'KiB'
|
|
|
|
|
echo -n ', size='"$((size/1024))"'KiB'
|
|
|
|
|
echo -n ', type=${types."${partition.filesystemType}"}'
|
|
|
|
|
${optionalString (partition ? bootable && partition.bootable)
|
|
|
|
|
"echo -n ', bootable'"}
|
|
|
|
|
echo "" # Finishes the command
|
|
|
|
|
) >> script.sfdisk
|
|
|
|
|
''
|
|
|
|
|
)}
|
2019-08-27 21:14:03 +03:00
|
|
|
|
|
2019-09-04 07:45:59 +03:00
|
|
|
|
echo "--- script ----"
|
|
|
|
|
cat script.sfdisk
|
|
|
|
|
echo "--- script ----"
|
|
|
|
|
|
2019-08-27 21:14:03 +03:00
|
|
|
|
echo
|
|
|
|
|
echo "Making image, $totalSize bytes..."
|
2019-09-04 07:45:59 +03:00
|
|
|
|
truncate -s $((totalSize)) $img
|
2019-08-27 21:14:03 +03:00
|
|
|
|
sfdisk $img < script.sfdisk
|
|
|
|
|
|
2019-09-03 23:42:21 +03:00
|
|
|
|
totalSize=${alignment}
|
2019-08-27 21:14:03 +03:00
|
|
|
|
echo
|
|
|
|
|
echo "Writing partitions into image"
|
2019-09-05 07:38:46 +03:00
|
|
|
|
${eachPart partitions (partition:
|
|
|
|
|
if partition ? isGap && partition.isGap then
|
|
|
|
|
(gapFragment partition)
|
|
|
|
|
else
|
|
|
|
|
''
|
|
|
|
|
input_img="${partition}/${partition.filename}"
|
|
|
|
|
${sizeFragment}
|
|
|
|
|
echo " -> ${partition.name}: $size / ${partition.filesystemType}"
|
|
|
|
|
|
|
|
|
|
echo "$start / $size"
|
|
|
|
|
dd conv=notrunc if=$input_img of=$img seek=$((start/512)) count=$((size/512)) bs=512
|
|
|
|
|
''
|
|
|
|
|
)}
|
2019-08-27 21:14:03 +03:00
|
|
|
|
|
2019-09-04 07:45:59 +03:00
|
|
|
|
echo
|
|
|
|
|
echo "Information about the image:"
|
2019-08-27 21:14:03 +03:00
|
|
|
|
ls -lh $img
|
2019-09-04 07:45:59 +03:00
|
|
|
|
sfdisk -V --list $img
|
2019-08-27 21:14:03 +03:00
|
|
|
|
'';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* */ ;}; in scope."diskImage.makeMBR"
|