OLD | NEW |
(Empty) | |
| 1 #! /usr/bin/perl |
| 2 # -*- Mode: Cperl -*- |
| 3 # image.postrm --- |
| 4 # Author : Manoj Srivastava ( srivasta@glaurung.green-gryphon.com ) |
| 5 # Created On : Sat May 15 11:05:13 1999 |
| 6 # Created On Node : glaurung.green-gryphon.com |
| 7 # Last Modified By : Manoj Srivastava |
| 8 # Last Modified On : Wed Sep 13 11:26:19 2006 |
| 9 # Last Machine Used: glaurung.internal.golden-gryphon.com |
| 10 # Update Count : 57 |
| 11 # Status : Unknown, Use with caution! |
| 12 # HISTORY : |
| 13 # Description : |
| 14 # |
| 15 # $Id: image.postrm,v 1.31 2003/10/07 16:24:20 srivasta Exp $ |
| 16 # |
| 17 |
| 18 |
| 19 # |
| 20 #use strict; #for debugging |
| 21 use Cwd 'abs_path'; |
| 22 |
| 23 $|=1; |
| 24 |
| 25 # Predefined values: |
| 26 my $version = "=V"; |
| 27 my $link_in_boot = ""; # Should be empty, mostly |
| 28 my $no_symlink = ""; # Should be empty, mostly |
| 29 my $reverse_symlink = ""; # Should be empty, mostly |
| 30 my $do_symlink = "Yes"; # target machine defined |
| 31 my $do_boot_enable = "Yes"; # target machine defined |
| 32 my $do_bootfloppy = "Yes"; # target machine defined |
| 33 my $do_bootloader = "Yes"; # target machine defined |
| 34 my $move_image = ''; # target machine defined |
| 35 my $kimage = "=K"; # Should be empty, mostly |
| 36 my $loader = "=L"; # lilo, silo, quik, palo, vmelilo, or nettrom |
| 37 my $image_dir = "/boot"; # where the image is located |
| 38 my $clobber_modules = ''; # target machine defined |
| 39 my $initrd = "YES"; # initrd kernel |
| 40 my $do_initrd = ''; # Normally, we don't |
| 41 my $warn_initrd = 'YES'; # Normally we do |
| 42 my $use_hard_links = ''; # hardlinks do not work across fs boundaries |
| 43 my $postinst_hook = ''; #Normally we do not |
| 44 my $postrm_hook = ''; #Normally we do not |
| 45 my $preinst_hook = ''; #Normally we do not |
| 46 my $prerm_hook = ''; #Normally we do not |
| 47 my $minimal_swap = ''; # Do not swap symlinks |
| 48 my $ignore_depmod_err = ''; # normally we do not |
| 49 my $relink_build_link = 'YES'; # There is no harm in checking the link |
| 50 my $force_build_link = ''; # we shall not create a dangling link |
| 51 my $kernel_arch = "=B"; |
| 52 my $ramdisk = "/usr/sbin/update-initramfs"; |
| 53 my $package_name = "linux-image-$version"; |
| 54 |
| 55 my $Loader = "NoLOADER"; # |
| 56 $Loader = "LILO" if $loader =~ /^lilo/io; |
| 57 $Loader = "SILO" if $loader =~ /^silo/io; |
| 58 $Loader = "QUIK" if $loader =~ /^quik/io; |
| 59 $Loader = "yaboot" if $loader =~ /^yaboot/io; |
| 60 $Loader = "PALO" if $loader =~ /^palo/io; |
| 61 $Loader = "NETTROM" if $loader =~ /^nettrom/io; |
| 62 $Loader = "VMELILO" if $loader =~ /^vmelilo/io; |
| 63 $Loader = "ZIPL" if $loader =~ /^zipl/io; |
| 64 $Loader = "ELILO" if $loader =~ /^elilo/io; |
| 65 |
| 66 |
| 67 # This should not point to /tmp, because of security risks. |
| 68 my $temp_file_name = "/var/log/$loader" . "_log.$$"; |
| 69 |
| 70 #known variables |
| 71 my @boilerplate = (); |
| 72 my @silotemplate = (); |
| 73 my @quiktemplate = (); |
| 74 my @palotemplate = (); |
| 75 my @vmelilotemplate = (); |
| 76 my $bootdevice = ''; |
| 77 my $rootdevice = ''; |
| 78 my $rootdisk = ''; |
| 79 my $rootpartition = ''; |
| 80 my $image_dest = "/"; |
| 81 my $realimageloc = "/$image_dir/"; |
| 82 my $have_conffile = ""; |
| 83 my $CONF_LOC = '/etc/kernel-img.conf'; |
| 84 my $relative_links = ''; |
| 85 my $silent_modules = ''; |
| 86 my $silent_loader = ''; |
| 87 my $warn_reboot = 'Yes'; # Warn that we are installing a version of |
| 88 # the kernel we are running |
| 89 |
| 90 chdir('/') or die "could not chdir to /:$!\n"; |
| 91 # remove multiple leading slashes; make sure there is at least one. |
| 92 $realimageloc =~ s|^/*|/|o; |
| 93 $realimageloc =~ s|/+|/|o; |
| 94 |
| 95 |
| 96 if (-r "$CONF_LOC" && -f "$CONF_LOC" ) { |
| 97 if (open(CONF, "$CONF_LOC")) { |
| 98 while (<CONF>) { |
| 99 chomp; |
| 100 s/\#.*$//g; |
| 101 next if /^\s*$/; |
| 102 |
| 103 $do_symlink = "" if /^\s*do_symlinks\s*=\s*(no|false|0)\s*$/ig; |
| 104 $no_symlink = "" if /^\s*no_symlinks\s*=\s*(no|false|0)\s*$/ig; |
| 105 $reverse_symlink = "" if /^\s*reverse_symlinks\s*=\s*(no|false|0)\s*$/ig; |
| 106 $link_in_boot = "" if /^\s*image_in_boot\s*=\s*(no|false|0)\s*$/ig; |
| 107 $link_in_boot = "" if /^\s*link_in_boot\s*=\s*(no|false|0)\s*$/ig; |
| 108 $move_image = "" if /^\s*move_image\s*=\s*(no|false|0)\s*$/ig; |
| 109 $clobber_modules = '' if /^\s*clobber_modules\s*=\s*(no|false|0)\s*$/ig; |
| 110 $do_boot_enable = '' if /^\s*do_boot_enable\s*=\s*(no|false|0)\s*$/ig; |
| 111 $do_bootfloppy = '' if /^\s*do_bootfloppy\s*=\s*(no|false|0)\s*$/ig; |
| 112 $relative_links = '' if /^\s*relative_links \s*=\s*(no|false|0)\s*$/ig; |
| 113 $do_bootloader = '' if /^\s*do_bootloader\s*=\s*(no|false|0)\s*$/ig; |
| 114 $do_initrd = '' if /^\s*do_initrd\s*=\s*(no|false|0)\s*$/ig; |
| 115 $warn_initrd = '' if /^\s*warn_initrd\s*=\s*(no|false|0)\s*$/ig; |
| 116 $use_hard_links = '' if /^\s*use_hard_links\s*=\s*(no|false|0)\s*$/ig; |
| 117 $silent_modules = '' if /^\s*silent_modules\s*=\s*(no|false|0)\s*$/ig; |
| 118 $silent_loader = '' if /^\s*silent_loader\s*=\s*(no|false|0)\s*$/ig; |
| 119 $warn_reboot = '' if /^\s*warn_reboot\s*=\s*(no|false|0)\s*$/ig; |
| 120 $minimal_swap = '' if /^\s*minimal_swap\s*=\s*(no|false|0)\s*$/ig; |
| 121 $ignore_depmod_err = '' if /^\s*ignore_depmod_err\s*=\s*(no|false|0)\s*$/i
g; |
| 122 $relink_build_link = '' if /^\s*relink_build_link\s*=\s*(no|false|0)\s*$/i
g; |
| 123 $force_build_link = '' if /^\s*force_build_link\s*=\s*(no|false|0)\s*$/ig; |
| 124 |
| 125 $do_symlink = "Yes" if /^\s*do_symlinks\s*=\s*(yes|true|1)\s*$/ig; |
| 126 $no_symlink = "Yes" if /^\s*no_symlinks\s*=\s*(yes|true|1)\s*$/ig; |
| 127 $reverse_symlink = "Yes" if /^\s*reverse_symlinks\s*=\s*(yes|true|1)\s*$/i
g; |
| 128 $link_in_boot = "Yes" if /^\s*image_in_boot\s*=\s*(yes|true|1)\s*$/ig; |
| 129 $link_in_boot = "Yes" if /^\s*link_in_boot\s*=\s*(yes|true|1)\s*$/ig; |
| 130 $move_image = "Yes" if /^\s*move_image\s*=\s*(yes|true|1)\s*$/ig; |
| 131 $clobber_modules = "Yes" if /^\s*clobber_modules\s*=\s*(yes|true|1)\s*$/ig
; |
| 132 $do_boot_enable = "Yes" if /^\s*do_boot_enable\s*=\s*(yes|true|1)\s*$/ig; |
| 133 $do_bootfloppy = "Yes" if /^\s*do_bootfloppy\s*=\s*(yes|true|1)\s*$/ig; |
| 134 $do_bootloader = "Yes" if /^\s*do_bootloader\s*=\s*(yes|true|1)\s*$/ig; |
| 135 $relative_links = "Yes" if /^\s*relative_links\s*=\s*(yes|true|1)\s*$/ig; |
| 136 $do_initrd = "Yes" if /^\s*do_initrd\s*=\s*(yes|true|1)\s*$/ig; |
| 137 $warn_initrd = "Yes" if /^\s*warn_initrd\s*=\s*(yes|true|1)\s*$/ig; |
| 138 $use_hard_links = "Yes" if /^\s*use_hard_links\s*=\s*(yes|true|1)\s*$/ig; |
| 139 $silent_modules = 'Yes' if /^\s*silent_modules\s*=\s*(yes|true|1)\s*$/ig; |
| 140 $silent_loader = 'Yes' if /^\s*silent_loader\s*=\s*(yes|true|1)\s*$/ig; |
| 141 $warn_reboot = 'Yes' if /^\s*warn_reboot\s*=\s*(yes|true|1)\s*$/ig; |
| 142 $minimal_swap = 'Yes' if /^\s*minimal_swap\s*=\s*(yes|true|1)\s*$/ig; |
| 143 $ignore_depmod_err = 'Yes' if /^\s*ignore_depmod_err\s*=\s*(yes|true|1)\s*
$/ig; |
| 144 $relink_build_link = 'Yes' if /^\s*relink_build_link\s*=\s*(yes|true|1)\s*
$/ig; |
| 145 $force_build_link = 'Yes' if /^\s*force_build_link\s*=\s*(yes|true|1)\s*$/
ig; |
| 146 |
| 147 $image_dest = "$1" if /^\s*image_dest\s*=\s*(\S+)/ig; |
| 148 $postinst_hook = "$1" if /^\s*postinst_hook\s*=\s*(\S+)/ig; |
| 149 $postrm_hook = "$1" if /^\s*postrm_hook\s*=\s*(\S+)/ig; |
| 150 $preinst_hook = "$1" if /^\s*preinst_hook\s*=\s*(\S+)/ig; |
| 151 $prerm_hook = "$1" if /^\s*prerm_hook\s*=\s*(\S+)/ig; |
| 152 $ramdisk = "$1" if /^\s*ramdisk\s*=\s*(.+)$/ig; |
| 153 } |
| 154 close CONF; |
| 155 $have_conffile = "Yes"; |
| 156 } |
| 157 } |
| 158 |
| 159 if ($link_in_boot) { |
| 160 $image_dest = "/$image_dir/"; |
| 161 $image_dest =~ s|^/*|/|o; |
| 162 } |
| 163 |
| 164 $image_dest = "$image_dest/"; |
| 165 $image_dest =~ s|/+$|/|o; |
| 166 |
| 167 # The destdir may be gone by now. |
| 168 if (-d "$image_dest") { |
| 169 chdir("$image_dest") or die "could not chdir to $image_dest:$!\n"; |
| 170 } |
| 171 |
| 172 # Paranoid check to make sure that the correct value is put in there |
| 173 if (! $kimage) {$kimage = "vmlinuz"} # Hmm. empty |
| 174 elsif ($kimage =~ m/^b?zImage$/o) {$kimage = "vmlinuz"} # these produce vmlinuz |
| 175 elsif ($kimage =~ m/^[iI]mage$/o) { my $nop = $kimage;} |
| 176 elsif ($kimage =~ m/^vmlinux$/o) { my $nop = $kimage;} |
| 177 else {$kimage = "vmlinuz"} # default |
| 178 |
| 179 $ENV{KERNEL_ARCH}=$kernel_arch if $kernel_arch; |
| 180 |
| 181 |
| 182 ###################################################################### |
| 183 ###################################################################### |
| 184 ############ |
| 185 ###################################################################### |
| 186 ###################################################################### |
| 187 sub remove_sym_link { |
| 188 my $bad_image = $_[0]; |
| 189 |
| 190 warn "Removing symbolic link $bad_image \n"; |
| 191 if ($loader =~ /lilo/i) |
| 192 { |
| 193 warn "Unless you used the optional flag in lilo, \n"; |
| 194 } |
| 195 warn " you may need to re-run your boot loader" . ($loader ? "[$loader]":"") |
| 196 . "\n"; |
| 197 # Remove the dangling link |
| 198 unlink "$bad_image"; |
| 199 } |
| 200 |
| 201 ###################################################################### |
| 202 ###################################################################### |
| 203 ############ |
| 204 ###################################################################### |
| 205 ###################################################################### |
| 206 sub CanonicalizePath { |
| 207 my $path = join '/', @_; |
| 208 my @work = split '/', $path; |
| 209 my @out; |
| 210 my $is_absolute; |
| 211 |
| 212 if (@work && $work[0] eq "") { $is_absolute = 1; shift @work; } |
| 213 |
| 214 while (@work) { |
| 215 my $seg = shift @work; |
| 216 if ($seg eq "." || $seg eq "") { |
| 217 } elsif ($seg eq "..") { |
| 218 if (@out && $out[-1] ne "..") { |
| 219 pop @out; |
| 220 } else { |
| 221 # Leading "..", or "../..", etc. |
| 222 push @out, $seg; |
| 223 } |
| 224 } else { |
| 225 push @out, $seg; |
| 226 } |
| 227 } |
| 228 |
| 229 unshift @out, "" if $is_absolute; |
| 230 return join('/', @out); |
| 231 } |
| 232 |
| 233 ###################################################################### |
| 234 ###################################################################### |
| 235 ############ |
| 236 ###################################################################### |
| 237 ###################################################################### |
| 238 # This removes dangling symlinks. What do we do about hard links? Surely a |
| 239 # something with the nane $image_dest . "$kimage" ought not to be left behind? |
| 240 sub image_magic { |
| 241 my $kimage = $_[0]; |
| 242 my $image_dest = $_[1]; |
| 243 |
| 244 if (-l "$kimage") { |
| 245 # There is a symbolic link |
| 246 my $force_move = 0; |
| 247 my $vmlinuz_target = readlink "$kimage"; |
| 248 my $real_target = ''; |
| 249 $real_target = abs_path($vmlinuz_target) if defined ($vmlinuz_target); |
| 250 if (!defined($vmlinuz_target) || ! -f "$real_target") { |
| 251 # what, a dangling symlink? |
| 252 warn "The link " . $image_dest . "$kimage is a damaged link\n"; |
| 253 # Remove the dangling link |
| 254 &remove_sym_link("$kimage"); |
| 255 } |
| 256 else { |
| 257 my $canonical_target = CanonicalizePath("$vmlinuz_target"); |
| 258 if (! -e $canonical_target) { |
| 259 warn "The link " . $image_dest . "$kimage is a dangling link\n"; |
| 260 &remove_sym_link("$kimage"); |
| 261 } |
| 262 } |
| 263 } |
| 264 } |
| 265 |
| 266 # set the env var stem |
| 267 $ENV{'STEM'} = "linux"; |
| 268 |
| 269 sub exec_script { |
| 270 my $type = shift; |
| 271 my $script = shift; |
| 272 print STDERR "Running $type hook script $script.\n"; |
| 273 system ("$script $version $realimageloc$kimage-$version") && |
| 274 print STDERR "User $type hook script [$script] "; |
| 275 if ($?) { |
| 276 if ($? == -1) { |
| 277 print STDERR "failed to execute: $!\n"; |
| 278 } |
| 279 elsif ($? & 127) { |
| 280 printf STDERR "died with signal %d, %s coredump\n", |
| 281 ($? & 127), ($? & 128) ? 'with' : 'without'; |
| 282 } |
| 283 else { |
| 284 printf STDERR "exited with value %d\n", $? >> 8; |
| 285 } |
| 286 } |
| 287 } |
| 288 sub run_hook { |
| 289 my $type = shift; |
| 290 my $script = shift; |
| 291 if ($script =~ m,^/,) { |
| 292 # Full path provided for the hook script |
| 293 if (-x "$script") { |
| 294 &exec_script($type,$script); |
| 295 } |
| 296 else { |
| 297 warn "The provided $type hook script [$script] could not be run.\n"; |
| 298 } |
| 299 } |
| 300 else { |
| 301 # Look for it in a safe path |
| 302 for my $path ('/bin', '/sbin', '/usr/bin', '/usr/sbin') { |
| 303 if (-x "$path/$script") { |
| 304 &exec_script($type, "$path/$script"); |
| 305 return 0; |
| 306 } |
| 307 } |
| 308 # No luck |
| 309 print STDERR "Could not find $type hook script [$script].\n"; |
| 310 warn "Looked in: '/bin', '/sbin', '/usr/bin', '/usr/sbin'\n"; |
| 311 } |
| 312 } |
| 313 |
| 314 ## Run user hook script here, if any |
| 315 if ($postrm_hook) { |
| 316 &run_hook("postrm", $postrm_hook); |
| 317 } |
| 318 if (-d "/etc/kernel/postrm.d") { |
| 319 warn "Examining /etc/kernel/postrm.d .\n"; |
| 320 system ("run-parts --verbose --exit-on-error --arg=$version " . |
| 321 "--arg=$realimageloc$kimage-$version " . |
| 322 "/etc/kernel/postrm.d") && |
| 323 die "Failed to process /etc/kernel/postrm.d"; |
| 324 } |
| 325 if (-d "/etc/kernel/postrm.d/$version") { |
| 326 warn "Examining /etc/kernel/postrm.d/$version .\n"; |
| 327 system ("run-parts --verbose --exit-on-error --arg=$version " . |
| 328 "--arg=$realimageloc$kimage-$version " . |
| 329 "/etc/kernel/postrm.d/$version") && |
| 330 die "Failed to process /etc/kernel/postrm.d/$version"; |
| 331 } |
| 332 |
| 333 # check and remove damaged and dangling symlinks |
| 334 if ($ARGV[0] !~ /upgrade/) { |
| 335 system("$ramdisk -d -k " . $version . " > /dev/null 2>&1"); |
| 336 if (-f $realimageloc . "initrd.img-$version.bak") { |
| 337 unlink $realimageloc . "initrd.img-$version.bak"; |
| 338 } |
| 339 image_magic($kimage, $image_dest); |
| 340 image_magic($kimage . ".old", $image_dest); |
| 341 image_magic("initrd.img", $image_dest) if $initrd; |
| 342 image_magic("initrd.img.old", $image_dest) if $initrd; |
| 343 } |
| 344 |
| 345 exit 0; |
| 346 |
| 347 __END__ |
| 348 |
| 349 |
| 350 |
| 351 |
| 352 |
| 353 |
OLD | NEW |