Debian Codegraph @

bin/linux-update-symlinks

raw
1 #!/usr/bin/perl
2
3 # Copyright 2016 Ben Hutchings
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 use strict;
20 use warnings;
21 use File::Spec;
22 use POSIX ();
23
24 use DebianLinux qw(image_stem image_list read_kernelimg_conf version_cmp);
25
26 sub usage {
27 my $fh = shift;
28 print $fh (<< "EOT");
29 Usage: $0 {install|upgrade|remove} KERNEL-VER IMAGE-PATH
30
31 This command is intended to be called from the postinst and postrm
32 maintainer scripts of Linux kernel packages. The postinst script must
33 pass the first argument 'install' or 'upgrade' depending on whether a
34 fresh installation or an upgrade has taken place.
35
36 The KERNEL-VER argument must be the kernel version string as shown by
37 'uname -r' and used in filenames, not the package version.
38
39 The IMAGE-PATH argument must be the absolute filename of the kernel
40 image.
41 EOT
42 }
43
44 sub usage_error {
45 usage(*STDERR{IO});
46 exit 2;
47 }
48
49 sub update_symlink {
50 my ($source, $dest) = @_;
51
52 $source = File::Spec->abs2rel($source, (File::Spec->splitpath($dest))[1]);
53
54 # Don't make unnecessary changes
55 my $old_source = readlink($dest);
56 if (defined($old_source) && $old_source eq $source) {
57 return;
58 }
59
60 # Create a symlink with a temporary name
61 my $rand;
62 while (($rand = int(rand(1000000))) && !symlink($source, "$dest.$rand")) {
63 if ($! != POSIX::EEXIST) {
64 die "Failed to create symlink to $source: $!";
65 }
66 }
67
68 # Try to apply SELinux label
69 if (-x "/sbin/restorecon" && system("/sbin/restorecon", "$dest.$rand")) {
70 print STDERR "W: Failed to set SELinux label for $dest\n";
71 }
72
73 # Move it into place atomically
74 if (!rename("$dest.$rand", $dest)) {
75 my $err = "$!";
76 unlink("$dest.$rand");
77 die "Failed to create or replace $dest: $err";
78 }
79
80 print "I: $dest is now a symlink to $source\n";
81 }
82
83 sub promote_default {
84 my ($sorted_images, $link) = @_;
85
86 # Get the absolute path
87 my $image_path = readlink($link);
88 if (!defined($image_path)) {
89 return;
90 }
91 $image_path = File::Spec->rel2abs($image_path,
92 (File::Spec->splitpath($link))[1]);
93
94 # If it's already on the list, move it to the end. (If it's not,
95 # presumably the symlink is broken.)
96 for my $i (0..$#$sorted_images) {
97 if ($sorted_images->[$i]->[1] eq $image_path) {
98 my $tmp = $sorted_images->[$i];
99 splice @$sorted_images, $i, 1;
100 push @$sorted_images, $tmp;
101 last;
102 }
103 }
104 }
105
106 sub update_all {
107 my ($change, $version, $image_path) = @_;
108 my $conf = read_kernelimg_conf();
109
110 # Check whether symlinks are actually wanted
111 if (!$conf->{do_symlinks}) {
112 exit 0;
113 }
114
115 my $default_dir = $conf->{image_dest};
116 $default_dir =~ s|/*$|/|; # tidy up
117
118 my $image_stem = image_stem();
119
120 # We build a list of [version, image_path, initrd_path] in
121 # ascending priority order, then update the symlinks to match it.
122
123 # Start with a list of existing images sorted by version, adding any
124 # existing initrds.
125 my @sorted_images = sort({version_cmp($a->[0], $b->[0])} image_list());
126 for my $i (0..$#sorted_images) {
127 my $exist_version = $sorted_images[$i]->[0];
128 if (-f "/boot/initrd.img-$exist_version") {
129 $sorted_images[$i]->[2] = "/boot/initrd.img-$exist_version";
130 }
131 }
132
133 # The files for this version may not actually have been
134 # installed/removed yet (e.g. on installation the initrd will
135 # probably be built later). Also, on installation this version
136 # should have the highest priority, not dependent on version
137 # ordering. So ensure that this version is included (for
138 # for upgrade) or excluded (otherwise).
139 my $initrd_path = ((exists($ENV{INITRD}) && $ENV{INITRD} eq 'No') ?
140 undef : "/boot/initrd.img-$version");
141 for my $i (0..@sorted_images) {
142 my $diff = ($i < @sorted_images ?
143 version_cmp($version, $sorted_images[$i]->[0]) : -1);
144 if ($diff == 0) {
145 if ($change eq 'upgrade') {
146 $sorted_images[$i] = [$version, $image_path, $initrd_path];
147 } else {
148 splice @sorted_images, $i, 1;
149 }
150 last;
151 } elsif ($diff < 0) {
152 if ($change eq 'upgrade') {
153 splice @sorted_images, $i, 0,
154 [$version, $image_path, $initrd_path];
155 }
156 last;
157 }
158 }
159
160 # Any existing non-broken symlinks have higher priority.
161 promote_default(\@sorted_images, "${default_dir}${image_stem}.old");
162 promote_default(\@sorted_images, "${default_dir}${image_stem}");
163
164 # In case of installation, this version has highest priority.
165 if ($change eq 'install') {
166 push @sorted_images, [$version, $image_path, $initrd_path];
167 }
168
169 if (@sorted_images) {
170 # Take the two highest priority entries on the list. In case there
171 # is only one entry, use it twice!
172 my ($old_version, $old_image_path, $old_initrd_path) =
173 @{$sorted_images[@sorted_images >= 2 ? -2 : -1]};
174 my ($cur_version, $cur_image_path, $cur_initrd_path) =
175 @{$sorted_images[-1]};
176
177 update_symlink($old_image_path, "${default_dir}${image_stem}.old");
178 if (defined($old_initrd_path)) {
179 update_symlink($old_initrd_path, "${default_dir}initrd.img.old");
180 } else {
181 unlink("${default_dir}initrd.img.old");
182 }
183 update_symlink($cur_image_path, "${default_dir}${image_stem}");
184 if (defined($cur_initrd_path)) {
185 update_symlink($cur_initrd_path, "${default_dir}initrd.img");
186 } else {
187 unlink("${default_dir}initrd.img");
188 }
189 } else {
190 print STDERR "W: Last kernel image has been removed, so removing the default symlinks\n";
191 unlink("${default_dir}${image_stem}.old");
192 unlink("${default_dir}initrd.img.old");
193 unlink("${default_dir}${image_stem}");
194 unlink("${default_dir}initrd.img");
195 }
196
197 exit 0;
198 }
199
200 if (@ARGV == 0) {
201 usage_error();
202 }
203
204 my $change = $ARGV[0];
205 if ($change eq 'help' or grep({$_ eq '--help'} @ARGV)) {
206 usage(*STDOUT{IO});
207 exit 0;
208 } elsif ($change =~ /^(?:install|upgrade|remove)$/ && @ARGV == 3) {
209 update_all(@ARGV);
210 }
211 usage_error();