linux/debian/templates/image.prerm.in

150 lines
3.7 KiB
Plaintext
Raw Normal View History

#! /usr/bin/perl
#
use strict;
use warnings;
use POSIX ();
use Debconf::Client::ConfModule qw(:all);
use FileHandle;
version('2.0');
my $capb=capb("backup");
$|=1;
# Predefined values:
my $version = "@abiname@@localversion@";
my $kimage = "@image-stem@";
my $prerm_hook = ''; #Normally we do not
my $package_name = "linux-image-$version";
#known variables
my $realimageloc = "/boot/";
my $CONF_LOC = '/etc/kernel-img.conf';
# Variables used
my $ret=0;
my $seen='';
my $answer='';
my $running = '';
# Ignore all invocations uxcept when called on to remove
exit 0 unless ($ARGV[0] && $ARGV[0] =~ /remove/) ;
if (-r "$CONF_LOC" && -f "$CONF_LOC" ) {
if (open(CONF, "$CONF_LOC")) {
while (<CONF>) {
chomp;
s/\#.*$//g;
next if /^\s*$/;
$prerm_hook = "$1" if /prerm_hook\s*=\s*(\S+)/i;
}
close CONF;
}
}
# Are we in a container? Check for $container in pid 1's environment.
sub in_container {
my $res = 0;
if (my $fh = new FileHandle('/proc/1/environ', 'r')) {
local $/ = "\0";
$res = grep(/^container=/, <$fh>);
close($fh);
}
return $res;
}
# Are we in in a chroot? Compare root device and inode numbers with pid 1.
sub in_chroot {
my @my_root_st = stat('/');
my @pid1_root_st = stat('/proc/1/root');
return @my_root_st && @pid1_root_st &&
($my_root_st[0] != $pid1_root_st[0] || $my_root_st[1] != $pid1_root_st[1]);
}
# Check to see if we are trying to remove a running kernel.
chop($running=`uname -r`);
if (!in_container() && !in_chroot() && $running eq $version) {
# If we can ask debconf questions, ask whether that's intended
# and abort if not.
if (exists($ENV{'DEBIAN_FRONTEND'}) &&
$ENV{'DEBIAN_FRONTEND'} eq 'noninteractive') {
print STDERR "W: removing running kernel image.\n";
} else {
my $question = "${package_name}/prerm/removing-running-kernel-$version";
($ret,$seen) = fset ("$question", 'seen', 'false');
die "Error setting debconf flags in $question: $seen" if $ret;
$ret = subst("$question", 'running', "$running");
die "Error setting debconf substitutions in $question: $seen" if $ret;
($ret,$seen) = input('critical', "$question");
if ($ret && $ret != 30 ) {
die "Error setting debconf question $question: $seen";
}
($ret,$seen) = go ();
if ($ret && $ret != 30 ) {
die "Error asking debconf question $question: $seen";
}
($ret,$answer) = get("$question");
die "Error retreiving answer for $question: $answer" if $ret;
if ($answer =~ /^(y|t)/i) {
print STDERR "Aborting removal of running kernel image.\n";
exit 1; #Operation not permitted
}
else {
print STDERR "Ok, proceeding with removing running kernel image.\n";
}
}
}
#Now, they have an alternate kernel which they are currently running
chdir("/") or die "could not chdir to /:$!\n";
sub system_failure_message {
if ($? < 0) {
return "$!";
} elsif (POSIX::WIFSIGNALED($?)) {
return sprintf('signal %d', POSIX::WTERMSIG($?));
} else {
return sprintf('exit code %d', POSIX::WEXITSTATUS($?));
}
}
sub run_hook {
my $type = shift;
my $script = shift;
print STDERR "Running $script.\n";
if (system ("$script $version $realimageloc$kimage-$version")) {
die ("$script failed: " . system_failure_message());
}
}
my $options;
for (@ARGV) {
s,','\\'',g;
$options .= " '$_'";
}
$ENV{'DEB_MAINT_PARAMS'}="$options";
## Run user hook script here, if any
if (-x "$prerm_hook") {
&run_hook("prerm", $prerm_hook);
}
if (-d "/etc/kernel/prerm.d") {
system ("run-parts --report --exit-on-error --arg=$version " .
"--arg=$realimageloc$kimage-$version /etc/kernel/prerm.d") &&
die "Failed to process /etc/kernel/prerm.d";
}
exit 0;
__END__