1
1
mirror of https://github.com/NixOS/mobile-nixos.git synced 2024-12-18 05:21:47 +03:00
mobile-nixos/lib/image-builder/makeMBR.nix

107 lines
2.6 KiB
Nix
Raw Normal View History

{ 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
# 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);
# 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}";
nativeBuildInputs = [
utillinux
];
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 ))
'';
in ''
2019-08-27 21:14:03 +03:00
mkdir -p $out
cat <<EOF > script.sfdisk
label: dos
label-id: 0x${diskID}
2019-08-27 21:14:03 +03:00
EOF
totalSize=${alignment}
2019-08-27 21:14:03 +03:00
echo
echo "Gathering information about partitions."
${eachPart partitions (partition: ''
input_img="${partition}/${partition.filename}"
${sizeFragment}
2019-08-27 21:14:03 +03:00
echo " -> ${partition.name}: $size / ${partition.filesystemType}"
2019-08-27 21:25:38 +03:00
(
2019-08-27 21:14:03 +03:00
# The size is /1024; otherwise it's in sectors.
2019-08-27 21:25:38 +03:00
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
'')}
echo
echo "Making image, $totalSize bytes..."
truncate -s $totalSize $img
sfdisk $img < script.sfdisk
totalSize=${alignment}
2019-08-27 21:14:03 +03:00
echo
echo "Writing partitions into image"
${eachPart partitions (partition: ''
input_img="${partition}/${partition.filename}"
${sizeFragment}
2019-08-27 21:14:03 +03:00
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
'')}
ls -lh $img
'';
}
/* */ ;}; in scope."diskImage.makeMBR"