From 984921e657e4e7b0bde19a7b9c4c32e291399839 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 11 Oct 2013 17:17:26 +0200 Subject: [PATCH] nixos-generate-config: Emit fileSystems automatically --- .../installer/tools/nixos-generate-config.pl | 99 ++++++++++++++++--- 1 file changed, 84 insertions(+), 15 deletions(-) diff --git a/nixos/modules/installer/tools/nixos-generate-config.pl b/nixos/modules/installer/tools/nixos-generate-config.pl index efb250c081d..0f72f94fd35 100644 --- a/nixos/modules/installer/tools/nixos-generate-config.pl +++ b/nixos/modules/installer/tools/nixos-generate-config.pl @@ -6,6 +6,19 @@ use File::Basename; use File::Slurp; +sub uniq { + my %seen; + my @res = (); + foreach my $s (@_) { + if (!defined $seen{$s}) { + $seen{$s} = 1; + push @res, $s; + } + } + return @res; +} + + # Process the command line. my $outDir = "/etc/nixos"; @@ -192,7 +205,8 @@ if ($dmi =~ /Manufacturer: innotek/) { } -# Generate the list of swap devices. +# Generate the swapDevices option from the currently activated swap +# devices. my @swaps = read_file("/proc/swaps"); shift @swaps; my @swapDevices; @@ -202,20 +216,75 @@ foreach my $swap (@swaps) { } -# Generate the hardware configuration file. +# Generate the fileSystems option from the currently mounted +# filesystems. +sub in { + my ($d1, $d2) = @_; + return $d1 eq $d2 || substr($d1, 0, length($d2) + 1) eq "$d2/"; +} -sub removeDups { - my %seen; - my @res = (); - foreach my $s (@_) { - if (!defined $seen{$s}) { - $seen{$s} = ""; - push @res, $s; +my $fileSystems; +my %fsByDev; +foreach my $fs (read_file("/proc/self/mountinfo")) { + chomp $fs; + my @fields = split / /, $fs; + my $mountPoint = $fields[4]; + next unless -d $mountPoint; + my @mountOptions = split /,/, $fields[5]; + + # Skip special filesystems. + next if in($mountPoint, "/proc") || in($mountPoint, "/dev") || in($mountPoint, "/sys") || in($mountPoint, "/run"); + + # Skip the optional fields. + my $n = 6; $n++ while $fields[$n] ne "-"; $n++; + my $fsType = $fields[$n]; + my $device = $fields[$n + 1]; + my @superOptions = split /,/, $fields[$n + 2]; + + # Skip the read-only bind-mount on /nix/store. + next if $mountPoint eq "/nix/store" && (grep { $_ eq "rw" } @superOptions) && (grep { $_ eq "ro" } @mountOptions); + + # Maybe this is a bind-mount of a filesystem we saw earlier? + if (defined $fsByDev{$fields[2]}) { + my $path = $fields[3]; $path = "" if $path eq "/"; + $fileSystems .= <