Skip to content

Commit

Permalink
customizable fsType for make-disk-image
Browse files Browse the repository at this point in the history
  • Loading branch information
dingxiangfei2009 committed May 23, 2019
1 parent 8f3bfa5 commit 586a5ce
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions nixos/lib/make-disk-image.nix
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
}:

assert partitionTableType == "legacy" || partitionTableType == "efi" || partitionTableType == "none";
# We use -E offset=X below, which is only supported by e2fsprogs
assert partitionTableType != "none" -> fsType == "ext4";

with lib;

Expand All @@ -70,14 +68,14 @@ let format' = format; in let
legacy = ''
parted --script $diskImage -- \
mklabel msdos \
mkpart primary ext4 1MiB -1
mkpart primary ${fsType} 1MiB -1
'';
efi = ''
parted --script $diskImage -- \
mklabel gpt \
mkpart ESP fat32 8MiB 256MiB \
set 1 boot on \
mkpart primary ext4 256MiB -1
mkpart primary ${fsType} 256MiB -1
'';
none = "";
}.${partitionTableType};
Expand All @@ -96,11 +94,16 @@ let format' = format; in let
echo -n ${config.system.nixos.versionSuffix} > $out/nixos/.version-suffix
'';

fsTools = with pkgs; {
ext4 = e2fsprogs;
btrfs = btrfs-progs;
};

binPath = with pkgs; makeBinPath (
[ rsync
utillinux
parted
e2fsprogs
fsTools.${fsType}
lkl
config.system.build.nixos-install
config.system.build.nixos-enter
Expand All @@ -115,6 +118,11 @@ let format' = format; in let

closureInfo = pkgs.closureInfo { rootPaths = [ config.system.build.toplevel channelSources ]; };

mkfsFlags = {
ext4 = label: dest: "-F -L ${label} ${dest} -F $(sectorsToKilobytes $SECTORS)K";
btrfs = label: dest: "-f -L ${label} -b $(sectorsToBytes $SECTORS) ${dest}";
};

prepareImage = ''
export PATH=${binPath}
Expand All @@ -137,9 +145,12 @@ let format' = format; in let
# Get start & length of the root partition in sectors to $START and $SECTORS.
eval $(partx $diskImage -o START,SECTORS --nr ${rootPartition} --pairs)
mkfs.${fsType} -F -L ${label} $diskImage -E offset=$(sectorsToBytes $START) $(sectorsToKilobytes $SECTORS)K
partImage=part.raw
truncate -s $(sectorsToBytes $SECTORS) $partImage
mkfs.${fsType} ${mkfsFlags.${fsType} label "$partImage"}
dd if=$partImage of=$diskImage bs=1M oflag=seek_bytes seek=$(sectorsToBytes $START)
'' else ''
mkfs.${fsType} -F -L ${label} $diskImage
mkfs.${fsType} ${mkfsFlags.${fsType} label "$diskImage"}
''}
root="$PWD/root"
Expand Down Expand Up @@ -190,7 +201,7 @@ let format' = format; in let
in pkgs.vmTools.runInLinuxVM (
pkgs.runCommand name
{ preVM = prepareImage;
buildInputs = with pkgs; [ utillinux e2fsprogs dosfstools ];
buildInputs = with pkgs; [ utillinux fsTools.${fsType} dosfstools ];
postVM = ''
${if format == "raw" then ''
mv $diskImage $out/${filename}
Expand Down

0 comments on commit 586a5ce

Please sign in to comment.