File: image.prerm

package info (click to toggle)
kernel-package 8.135
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,008 kB
  • ctags: 696
  • sloc: perl: 3,089; makefile: 2,344; sh: 538
file content (303 lines) | stat: -rwxr-xr-x 11,352 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#! /usr/bin/perl
#                              -*- Mode: Perl -*- 
# image.prerm --- 
# Author           : root ( root@melkor.pilgrim.umass.edu ) 
# Created On       : Fri May 17 03:28:59 1996
# Created On Node  : melkor.pilgrim.umass.edu
# Last Modified By : Manoj Srivastava
# Last Modified On : Thu Mar 17 21:05:00 2005
# Last Machine Used: glaurung.internal.golden-gryphon.com
# Update Count     : 69
# Status           : Unknown, Use with caution!
# HISTORY          : 
# Description      : 
# 
#
#    $Id: image.prerm,v 1.22 2003/10/07 16:24:20 srivasta Exp $
#
# 
#use strict;

$|=1;
# Predefined values:
my $version         = "=V";
my $link_in_boot   = "=B";	# Should be empty, mostly
my $no_symlink      = "=S";	# Should be empty, mostly
my $reverse_symlink = "=R";	# Should be empty, mostly
my $do_symlinks     = "Yes";	# target machine defined
my $do_boot_enable  = "Yes";	# target machine defined
my $do_bootfloppy   = "Yes";	# target machine defined
my $do_bootloader   = "Yes";	# target machine defined
my $move_image      = '';       # target machine defined
my $kimage          = "=K";	# Should be empty, mostly
my $loader          = "=L";     # lilo, silo, quik, palo, vmelilo, or nettrom
my $image_dir       = "=D";     # where the image is located
my $clobber_modules = '';       # target machine defined
my $mkimage         = "=M";        # command to generate the initrd image
my $initrd          = "=I";     # initrd kernel
my $use_hard_links  = '';       # hardlinks do not wirk across fs boundaries
my $postinst_hook   = '';       #Normally we do not
my $postrm_hook     = '';       #Normally we do not
my $preinst_hook    = '';       #Normally we do not
my $prerm_hook      = '';       #Normally we do not
my $minimal_swap    = '';       # Do not swap symlinks
my $ignore_depmod_err = '';	# normally we do not
my $relink_build_link = 'YES';	# There is no harm in checking the link
my $force_build_link = '';	# There is no harm in checking the link
my $official_image    = "=OF";      # only true for official images

my $Loader          = "NoLOADER"; # 
$Loader             = "LILO"     if $loader =~ /^lilo/io;
$Loader             = "SILO"     if $loader =~ /^silo/io;
$Loader             = "QUIK"     if $loader =~ /^quik/io;
$Loader             = "yaboot"   if $loader =~ /^yaboot/io;
$Loader             = "PALO"     if $loader =~ /^palo/io;
$Loader             = "NETTROM"  if $loader =~ /^nettrom/io;
$Loader             = "VMELILO"  if $loader =~ /^vmelilo/io;
$Loader             = "ZIPL"     if $loader =~ /^zipl/io;
$Loader             = "ELILO"    if $loader =~ /^elilo/io;


# This should not point to /tmp, because of security risks.
my $temp_file_name = "/var/log/$loader" . "_log.$$";

#known variables
my @boilerplate     = ();
my @silotemplate    = ();
my @quiktemplate    = ();
my @palotemplate    = ();
my @vmelilotemplate = ();
my $bootdevice      = '';
my $rootdevice      = '';
my $rootdisk        = '';
my $rootpartition   = '';
my $image_dest      = "/";
my $realimageloc    = "/$image_dir/";
my $have_conffile   = "";
my $CONF_LOC        = '/etc/kernel-img.conf';
my $relative_links = '';
my $silent_modules  = '';
my $silent_loader   = '';

# remove multiple leading slashes; make sure there is at least one.
$realimageloc  =~ s|^/*|/|o;
$realimageloc  =~ s|/+|/|o;


# Variables used
my $image='';
my $answer='';
my $running = '';
my $WouldInvalidate = 0;

if ($ARGV[0] && ($ARGV[0] =~ /remove/ || $ARGV[0] =~ /upgrade/)) {
  if (-l "/usr/doc/=ST-image-$version") {
    unlink "/usr/doc/=ST-image-$version";
  }
}

# Ignore all invocations uxcept when called on to remove
exit 0 unless ($ARGV[0] && $ARGV[0] =~ /remove/) ;

# Paranoid check to make sure that the correct value is put in there
if (! $kimage)                    { $kimage = "vmlinuz";} # Hmm. empty
elsif ($kimage =~ m/^b?zImage$/o) { $kimage = "vmlinuz";} # these produce vmlinuz
elsif ($kimage =~ m/^[iI]mage$/o) { my $nop = $kimage;  }
elsif ($kimage =~ m/^vmlinux$/o)  { my $nop = $kimage;  }
else                              { $kimage = "vmlinuz";} # Default

if (-r "$CONF_LOC" && -f "$CONF_LOC"  ) {
  if (open(CONF, "$CONF_LOC")) {
    while (<CONF>) {
      chomp;
      s/\#.*$//g;
      next if /^\s*$/;

      $do_symlink      = "" if /do_symlinks\s*=\s*(no|false|0)\s*$/ig;
      $no_symlink      = "" if /no_symlinks\s*=\s*(no|false|0)\s*$/ig;
      $reverse_symlink = "" if /reverse_symlinks\s*=\s*(no|false|0)\s*$/ig;
      $link_in_boot    = "" if /image_in_boot\s*=\s*(no|false|0)\s*$/ig;
      $link_in_boot    = "" if /link_in_boot\s*=\s*(no|false|0)\s*$/ig;
      $move_image      = "" if /move_image\s*=\s*(no|false|0)\s*$/ig;
      $clobber_modules = '' if /clobber_modules\s*=\s*(no|false|0)\s*$/ig;
      $do_boot_enable  = '' if /do_boot_enable\s*=\s*(no|false|0)\s*$/ig;
      $do_bootfloppy   = '' if /do_bootfloppy\s*=\s*(no|false|0)\s*$/ig;
      $relative_links  = '' if /relative_links \s*=\s*(no|false|0)\s*$/ig;
      $do_bootloader   = '' if /do_bootloader\s*=\s*(no|false|0)\s*$/ig;
      $do_initrd       = '' if /do_initrd\s*=\s*(no|false|0)\s*$/ig;
      $use_hard_links  = '' if /use_hard_links\s*=\s*(no|false|0)\s*$/ig;
      $silent_modules  = '' if /silent_modules\s*=\s*(no|false|0)\s*$/ig;
      $silent_loader   = '' if /silent_loader\s*=\s*(no|false|0)\s*$/ig;
      $minimal_swap    = '' if /minimal_swap\s*=\s*(no|false|0)\s*$/ig;
      $ignore_depmod_err = '' if /ignore_depmod_err\s*=\s*(no|false|0)\s*$/ig;
      $relink_build_link = '' if /relink_build_link\s*=\s*(no|false|0)\s*$/ig;
      $force_build_link = '' if /force_build_link\s*=\s*(no|false|0)\s*$/ig;


      $do_symlink      = "Yes" if /do_symlinks\s*=\s*(yes|true|1)\s*$/ig;
      $no_symlink      = "Yes" if /no_symlinks\s*=\s*(yes|true|1)\s*$/ig;
      $reverse_symlink = "Yes" if /reverse_symlinks\s*=\s*(yes|true|1)\s*$/ig;
      $link_in_boot    = "Yes" if /image_in_boot\s*=\s*(yes|true|1)\s*$/ig;
      $link_in_boot    = "Yes" if /link_in_boot\s*=\s*(yes|true|1)\s*$/ig;
      $move_image      = "Yes" if /move_image\s*=\s*(yes|true|1)\s*$/ig;
      $clobber_modules = "Yes" if /clobber_modules\s*=\s*(yes|true|1)\s*$/ig;
      $do_boot_enable  = "Yes" if /do_boot_enable\s*=\s*(yes|true|1)\s*$/ig;
      $do_bootfloppy   = "Yes" if /do_bootfloppy\s*=\s*(yes|true|1)\s*$/ig;
      $do_bootloader   = "Yes" if /do_bootloader\s*=\s*(yes|true|1)\s*$/ig;
      $relative_links  = "Yes" if /relative_links\s*=\s*(yes|true|1)\s*$/ig;
      $do_initrd       = "Yes" if /do_initrd\s*=\s*(yes|true|1)\s*$/ig;
      $use_hard_links  = "Yes" if /use_hard_links\s*=\s*(yes|true|1)\s*$/ig;
      $silent_modules  = 'Yes' if /silent_modules\s*=\s*(yes|true|1)\s*$/ig;
      $silent_loader   = 'Yes' if /silent_loader\s*=\s*(yes|true|1)\s*$/ig;
      $minimal_swap    = 'Yes' if /minimal_swap\s*=\s*(yes|true|1)\s*$/ig;
      $ignore_depmod_err = 'Yes' if /ignore_depmod_err\s*=\s*(yes|true|1)\s*$/ig;
      $relink_build_link = 'Yes' if /relink_build_link\s*=\s*(yes|true|1)\s*$/ig;
      $force_build_link = 'Yes' if /force_build_link\s*=\s*(yes|true|1)\s*$/ig;

      $image_dest      = "$1"  if /image_dest\s*=\s*(\S+)/ig;
      $postinst_hook   = "$1"  if /postinst_hook\s*=\s*(\S+)/ig;
      $postrm_hook     = "$1"  if /postrm_hook\s*=\s*(\S+)/ig;
      $preinst_hook    = "$1"  if /preinst_hook\s*=\s*(\S+)/ig;
      $prerm_hook      = "$1"  if /prerm_hook\s*=\s*(\S+)/ig;
      $mkimage         = "$1"  if /mkimage\s*=\s*(.+)$/ig;
    }
    close CONF;
    $have_conffile = "Yes";
  }
}


#check to see if we are trying to remove a running kernel
# if so we abort right now.
chop($running=`uname -r`);
if ($running eq $version) {
  print STDERR <<"EOFERR";

  You are running a kernel (version $running) and attempting to remove
  the same version. This is a potentially disastrous action. Not only
  will /boot/vmlinuz-$running be removed, making it impossible to boot
  it, (you will have to take action to change your boot loader to boot
  a new kernel), it will also remove all modules under the directory 
  /lib/modules/$running. Just having a copy of the kernel image is not
  enough, you will have to replace the modules too.

    I repeat, this is very dangerous. If at all in doubt, answer
    no. If you know exactly what you are doing, and are prepared to
    hose your system, then answer Yes.
EOFERR
  
  my $done = 0;
  while (!$done) {
    print "Remove the running kernel image (not recommended) [No]? ";
    $answer=<STDIN>;
    if ($answer !~ /^\s*[Yy]/o) {
      exit 1; #Operation not permitted
    }
    if ($answer =~ /yes/io) {
      $done = 1;
      print "Ok, proceeding with removing running kernel image.\n";
      last;
    }
    print "Please answer yes or no.\n\n";
    $done = 0;
  }
}

#Now, they have an alternate kernel which they are currently running

## Run user hook script here, if any
if (-x "$prerm_hook") {
  system ("$prerm_hook", $version, $realimageloc . "$kimage-$version") &&
	    warn "User hook script $prerm_hook failed";
}
if (-d "/etc/kernel/prerm.d") {
  system ("run-parts", "--verbose", "--exit-on-error", "--arg=$version",
          "--arg=$realimageloc" . "$kimage-$version",
          "/etc/kernel/prerm.d") &&
            die "Failed to process /etc/kernel/prerm.d";
}
if (-d "/etc/kernel/prerm.d/$version") {
  system ("run-parts", "--verbose", "--exit-on-error", "--arg=$version",
          "--arg=$realimageloc" . "$kimage-$version",
          "/etc/kernel/prerm.d/$version") &&
            die "Failed to process /etc/kernel/prerm.d/$version";
}

# The rest is just us being nice to lilo users.

chdir("/") or die "could not chdir to /:$!\n";

if (-f "/etc/$loader.conf") { #I know, could be a link, but ..
  open (LILO, "/etc/$loader.conf") || &success(); # this is not critical
  while (<LILO>) {
    chop;
    s/\#.*//;			  # nix the comments
    next unless /^\s*image\s*=\s(\S+)/o;
    $image = $1;
    if ($image && -e $image) {
      while (defined($image) && -l $image) {
	$image = readlink ($image);
      }
      if (defined($image) && -e $image) {
	$WouldInvalidate |= $image =~ /$kimage-$version/;
      }
      else {
	&success(); # invalid $loader.conf file
      }
    }
    else {
      &success(); # invalid $loader.conf file
    }
  }
  close (LILO);
  if ($WouldInvalidate) {
    print STDERR <<"EOF";

 You have a valid /etc/$loader.conf file that mentions
 $kimage-$version.  Removing =ST-image-$version would invalidate
 that file. (you will have to edit /etc/$loader.conf or re-target
 symbolic links mentioned there (typically, /vmlinuz and /vmlinuz.old)
 to not refer to $kimage-$version and will have to re-run $loader). 

 I repeat: you shall have to make changes to your boot loader setup
 and will have to re-run $loader.

EOF
  ;
    if (&ask("Are you sure you want to remove =ST-image-$version")) {
      print STDERR <<"EOG";

Remember to edit /etc/$loader.conf and re-run $loader, or Make other
arrangements to boot your machine.

EOG
;
      &success();
    }
    else {
      print STDERR "\nNot removing =ST-image-$version.\n";
      exit 1;
    }
  }
}

sub success () {
    -f "/lib/modules/$version/modules.dep"  && 
      unlink "/lib/modules/$version/modules.dep";
    exit 0;
}

sub ask {
    print @_,"? [Yes] ";
    $answer=<STDIN>;
    return ( $answer !~ /^[Nn].*/ );
}

&success();
exit 0;
__END__