I'm running a VM on a virtual cluster. The (experimental) cluster provisioning system puts an unformatted block device in /dev/sdb. Upon cluster initialization, I want to format this device and mount.

In the initialization workflow, my VM contacts the cluster manager to learn how configure the VM--network interfaces, and devices. I'm feeding this info to a nix expression which my /etc/nixos/configuration.nix inherits. I can make the filesystem mount a partitioned disk:

fileSystems."/log" = { device = "${logDevice}1";
                       fsType = "bind"; };

However, I don't know how to format the device as a part of configuration. If this was a nix derivation, I would put make some sort of buildHook, e.g.

formatLogDisk = ''
   parted -s ${logDevice} mklabel gpt
   parted -s ${logDevice} unit % mkpart extended ext4 0 100
   mkfs.ext4 -q ${logDevice}1

So I'm wondering if there is some sort of preMount module hook that might run this. Otherwise, I can run the utilities directly during the phase that collects the system information, but I was hoping thered be a nixOs based answer.

  • It sounds a bit dangerous to always format a drive before mounting it. I mean what should happen when the system gets rebooted and it tries to mount a second time? – Chris Stryczynski Aug 24 '19 at 9:18
  • Its fine in this case. The drive is explicitly used for temporary data—experiment logs. – trevor cook Aug 24 '19 at 13:04
  • Have you tried using an activation script? These scripts run early in the boot process. – Emmanuel Rosa Sep 1 '19 at 1:35
  • Emmanuel thanks. I hadn’t heard of that. Turns out the fileSystems option has an autoformat option which looks like it should work—but i need to test. No partitioning, but that doesn’t look to be necessary. – trevor cook Sep 1 '19 at 2:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.