Jun 11 22:15:19 miLaptop kernel: usb 3-10: New USB device strings: Mfr=0, Product=0, SerialNumber=0 Jun 11 22:15:19 miLaptop systemd[1]: Inserted module 'autofs4'
diff --git a/nixos/modules/system/etc/etc.nix b/nixos/modules/system/etc/etc.nix index 15387f261..f1e43521a 100644 --- a/nixos/modules/system/etc/etc.nix +++ b/nixos/modules/system/etc/etc.nix @@ -362,6 +362,7 @@ in # Set up the statically computed bits of /etc. echo "setting up /etc..." ${pkgs.perl.withPackages (p: [ p.FileSlurp ])}/bin/perl ${./setup-etc.pl} ${etc}/etc + echo "perl exec finished" '';
重新启动后, 日志果然印证了我的猜测:
1 2 3 4 5 6
[ 1.489603] stage-2-init: setting up /etc... [ 1.495332] usb 3-10: new full-speed USB device number 4 using xhci_hcd [ 1.620655] usb 3-10: New USB device found, idVendor=8087, idProduct=0026, bcdDevice= 0.02 [ 1.620661] usb 3-10: New USB device strings: Mfr=0, Product=0, SerialNumber=0 [ 1.622555] probe of 3-10 returned 0 after 1478 usecs [ 28.326322] stage-2-init: perl exec finished
diff --git a/nixos/modules/system/etc/setup-etc.pl b/nixos/modules/system/etc/setup-etc.pl index ea0a38308..2defc6653 100644 --- a/nixos/modules/system/etc/setup-etc.pl +++ b/nixos/modules/system/etc/setup-etc.pl @@ -8,7 +8,10 @@ use File::Slurp; my $etc = $ARGV[0] or die; my $static = "/etc/static";
+my %function_call_counts; + sub atomicSymlink { + $function_call_counts{atomicSymlink}++; my ($source, $target) = @_; my $tmp = "$target.tmp"; unlink $tmp; @@ -21,7 +24,6 @@ sub atomicSymlink { } }
- # Atomically update /etc/static to point at the etc files of the # current configuration. atomicSymlink $etc, $static or die; @@ -30,6 +32,8 @@ atomicSymlink $etc, $static or die; # means either argument is a symlink to a file in /etc/static or a # directory with all children being static. sub isStatic { + $function_call_counts{isStatic}++; + my $path = shift;
if (-l $path) { @@ -59,6 +63,7 @@ sub isStatic { # in the current one. For efficiency, don't look under /etc/nixos # (where all the NixOS sources live). sub cleanup { + $function_call_counts{cleanup}++; if ($File::Find::name eq "/etc/nixos") { $File::Find::prune = 1; return; @@ -66,6 +71,7 @@ sub cleanup { if (-l $_) { my $target = readlink $_; if (substr($target, 0, length $static) eq $static) { + print "cleanup-remove:FILE:---$File::Find::name\n"; my $x = "/etc/static/" . substr($File::Find::name, length "/etc/"); unless (-l $x) { print STDERR "removing obsolete symlink ‘$File::Find::name’...\n"; @@ -73,8 +79,10 @@ sub cleanup { } } } + }
+ find(\&cleanup, "/etc");
@@ -82,14 +90,15 @@ find(\&cleanup, "/etc"); my @oldCopied = read_file("/etc/.clean", chomp => 1, err_mode => 'quiet'); open CLEAN, ">>/etc/.clean";
- # For every file in the etc tree, create a corresponding symlink in # /etc to /etc/static. The indirection through /etc/static is to make # switching to a new configuration somewhat more atomic. my %created; my @copied;
+ sub link { + $function_call_counts{link}++; my $fn = substr $File::Find::name, length($etc) + 1 or next;
# nixos-enter sets up /etc/resolv.conf as a bind mount, so skip it. @@ -136,15 +145,18 @@ sub link {
find(\&link, $etc);
- # Delete files that were copied in a previous version but not in the # current. foreach my $fn (@oldCopied) { + $function_call_counts{foreach}++; + if (!defined $created{$fn}) { + $function_call_counts{foreach_y}++; $fn = "/etc/$fn"; print STDERR "removing obsolete file ‘$fn’...\n"; unlink "$fn"; } + }
@@ -157,3 +169,9 @@ write_file("/etc/.clean", map { "$_\n" } sort @copied); # so we need to check and re-create it during activation. open TAG, ">>/etc/NIXOS"; close TAG; + +print "\n--- Function Call Summary ---\n"; +foreach my $func_name (sort keys %function_call_counts) { + print sprintf "%-20s: %d times\n", $func_name, $function_call_counts{$func_name}; +} +print "---------------------------\n";
这次的日志输出令人震惊:
1 2 3 4 5 6 7 8
[ 27.649163] stage-2-init: --- Function Call Summary --- [ 27.649176] stage-2-init: atomicSymlink : 153 times [ 27.649191] stage-2-init: cleanup : 2011360 times [ 27.649203] stage-2-init: foreach : 3 times [ 27.649216] stage-2-init: isStatic : 16 times [ 27.649230] stage-2-init: link : 203 times [ 27.649242] stage-2-init: --------------------------- [ 27.655408] stage-2-init: perl exec finished
+my $static_docker_path = "/etc/static/docker"; + +if (-e $static_docker_path) { + print STDERR "Entering /etc/docker to check for obsolete symlinks (as $static_docker_path is not a symlink).\n"; +} # Remove dangling symlinks that point to /etc/static. These are # configuration files that existed in a previous configuration but not # in the current one. For efficiency, don't look under /etc/nixos @@ -68,6 +73,12 @@ sub cleanup { $File::Find::prune = 1; return; } + if ($File::Find::name eq "/etc/docker") { + unless (-e $static_docker_path) { + $File::Find::prune = 1; + return; + } + } if (-l $_) { my $target = readlink $_; if (substr($target, 0, length $static) eq $static) {