Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.75 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. # liloconfig - creating a new lilo.conf file
  4. #
  5. #
  6. # Copyright 2011-2015 Joachim Wiedorn <joodevel at joonet.de>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  21. # MA 02110-1301, USA.
  22.  
  23. #---- some modules
  24. use strict;
  25. use warnings;
  26. use Getopt::Std;
  27. use Pod::Usage;
  28. use File::Copy;
  29.  
  30.  
  31. #---- global variables
  32. my $prog = $0;
  33. $prog =~ s#.*/##;
  34. my $version = "0.5";
  35.  
  36. #---- parameter check
  37. # h: help, v: verbose, f: force
  38. our $opt_h = 0;
  39. our $opt_v = 0;
  40. our $opt_f = 0;
  41. our $opt_u = 0;
  42. getopts('hvfu');
  43. # define perldoc usage
  44. pod2usage(1) if $opt_h;
  45.  
  46. #---- other variables
  47. our $liloconf = "/etc/lilo.conf";
  48. our $conftmp_1 = "/tmp/lilotmp1";
  49. our $conftmp_2 = "/tmp/lilotmp2";
  50. our $liloconfold = $liloconf . ".old";
  51. our $liloconfnew = $liloconf . ".new";
  52. our $fstabconf = "/etc/fstab";
  53.  
  54. our $idpath = "/dev/disk/by-id";
  55. our $uuidpath = "/dev/disk/by-uuid";
  56. our $lblpath = "/dev/disk/by-label";
  57. our $template = "/usr/share/doc/lilo/examples/lilo.example.conf.gz";
  58.  
  59. our $rootpart; # found root part
  60. our $root_dev; # /dev/hdX9, /dev/sdX9, /dev/md/*
  61. our $root_id; # UUID, LABEL, ID
  62. our $boot_dev; # /dev/hdX, /dev/sdX, /dev/md
  63. our $boot_id; # DISK-ID
  64.  
  65. #-------------------- main program --------------------
  66.  
  67. sub main {
  68.  
  69. my $exit = 0;
  70.  
  71. if (@ARGV == 1) {
  72. $liloconf = "$ARGV[0]";
  73. $liloconfold = $liloconf . ".old";
  74. $liloconfnew = $liloconf . ".new";
  75. }
  76. if (-f $liloconf and not $opt_f and not $opt_u) {
  77. print $prog .": " . $liloconf .
  78. " already exist! Please force overwriting with '-f' or '-u'.\n";
  79. $exit = 1;
  80. }
  81. else {
  82. $exit = create_lilo_conf();
  83. }
  84. return $exit;
  85. }
  86.  
  87. #-------------------- subroutines --------------------
  88.  
  89. sub create_lilo_conf {
  90.  
  91. my $exit = 1;
  92. my $found1 = 0;
  93. my $found2 = 0;
  94. my $found3 = 0;
  95.  
  96. # read fstab and search for root device
  97. $found1 = read_fstab();
  98.  
  99. ## STEP 1 ##
  100. if ($found1 == 0)
  101. # nothing found, exit by error
  102. { $found2 = 0;}
  103. elsif ($found1 == 1)
  104. # identify root device: root_dev and root_id
  105. { $found2 = convert_root_device(); }
  106. elsif ($found1 == 2)
  107. # nothing found, but exit successfully
  108. { $exit = 0; $found2 = 0;}
  109.  
  110. ## STEP 2 ##
  111. if ($found2 == 0)
  112. # nothing found, exit by error
  113. { $found3 = 0;}
  114. elsif ($found2 == 1)
  115. # convert root device to boot device
  116. { $found3 = convert_boot_device(); }
  117. elsif ($found2 == 2)
  118. # nothing found, but exit successfully
  119. { $exit = 0; $found2 = 3;}
  120.  
  121. ## STEP 3 ##
  122. if ($found3 == 0)
  123. # nothing found, exit by error
  124. { $exit = 1;}
  125. elsif ($found3 == 1) {
  126. if ((-f $liloconf) and not (-w $liloconf)) {
  127. print "E: Have no write permissions to $liloconf !\n";
  128. $exit = 1;
  129. }
  130. else {
  131. # finally write new/updated lilo.conf file
  132. if ($opt_u) { $exit = update_lilo_conf(); }
  133. else { $exit = write_lilo_conf(); }
  134. }
  135. }
  136. elsif ($found3 == 2)
  137. # nothing found, but exit successfully
  138. { $exit = 0;}
  139.  
  140. return $exit;
  141. }
  142.  
  143. sub read_fstab {
  144.  
  145. my $root_part;
  146. my $mountpoint;
  147. my $broken_fstab = 1;
  148. my $base_fstab = 0;
  149. my $found = 1;
  150.  
  151. # check fstab for root device
  152. if (-f $fstabconf) {
  153. # Parsing fstab for the root partition
  154. open(FSTAB, "<$fstabconf") or die "$prog: couldn't open $fstabconf: $!\n";
  155.  
  156. while (<FSTAB>) {
  157. # Search magic string which indicates a base filesystem
  158. $base_fstab = 1 if /^# UNCONFIGURED FSTAB FOR BASE SYSTEM/;
  159. next if /^#/; # ignore comment lines
  160.  
  161. s/^[ \t]+//; # remove space or tab at begin of the line
  162. ($root_part,$mountpoint) = split(/[ \t]+/);
  163. next unless defined $mountpoint; # ignore empty lines too
  164.  
  165. # stop if we found the root device...
  166. if ($mountpoint eq '/') {
  167. $broken_fstab = 0;
  168. last;
  169. }
  170. }
  171. close(FSTAB) or die "$prog: couldn't close $fstabconf: $!\n";
  172. }
  173.  
  174. if ($base_fstab) {
  175. print "E: It seems you want configure the base filesystem \n" .
  176. "and I'm therefore simply going to exit successfully \n" .
  177. "without trying to actually configure LILO properly. \n";
  178. $found = 2;
  179. }
  180. if ($broken_fstab) {
  181. print "E: It seems the file /etc/fstab is not properly \n" .
  182. "configured: no root partition '/' found! \n";
  183. $found = 0;
  184. }
  185. # save the found root device
  186. $rootpart = $root_part;
  187.  
  188. return $found;
  189. }
  190.  
  191. sub convert_root_device {
  192.  
  193. my $found = 1;
  194. my $root_disk = '';
  195. my $root_link;
  196. # global variables: $root_dev, $root_id
  197.  
  198. if ($rootpart =~ /\/dev\//) {
  199. $root_disk = $rootpart;
  200.  
  201. if (-b $root_disk) {
  202. $root_dev = $root_disk;
  203. if($opt_v) { print "Convert root option $root_disk into UUID\n"; }
  204. $root_id = find_id_link($root_disk,$uuidpath);
  205.  
  206. if (not -l "$uuidpath/$root_id") {
  207. if($opt_v) { print "W: could not find UUID for $root_disk!\n"; }
  208. # than we will use root_dev in lilo.conf
  209. }
  210. else {
  211. # finally add uuid label
  212. $root_id = "UUID=" . $root_id;
  213. }
  214. }
  215. else {
  216. if($opt_v) { print "E: cannot check $root_disk: device does not exist!\n"; }
  217. $found = 0;
  218. }
  219. }
  220. elsif ($rootpart =~ /^UUID/ or $rootpart =~ /^LABEL/) {
  221. $root_link = $rootpart;
  222. $root_link =~ s{\"}{}g;
  223. $root_link =~ s{^LABEL=}{/dev/disk/by-label/};
  224. $root_link =~ s{^UUID=}{/dev/disk/by-uuid/};
  225.  
  226. if (-l $root_link) {
  227. $root_id = $rootpart;
  228. $root_disk = readlink($root_link);
  229. $root_disk =~ s{\.\./\.\./}{/dev/};
  230.  
  231. if (-b $root_disk) { $root_dev = $root_disk; }
  232. else {
  233. if($opt_v) { print "E: cannot check $root_link: link does not exist!\n"; }
  234. $found = 0;
  235. }
  236. }
  237. else {
  238. print "E: cannot check $root_link: link does not exist!\n";
  239. $found = 0;
  240. }
  241. }
  242. else {
  243. print "E: cannot use uncommon $rootpart found as root device!\n";
  244. $found = 0;
  245. }
  246.  
  247. return $found;
  248. }
  249.  
  250. sub find_id_link {
  251.  
  252. my $olddev = $_[0];
  253. my $path_id = $_[1];
  254. my @sellinks;
  255. my $_idlink;
  256. my $_actlink;
  257. my $newdevid = '';
  258.  
  259. opendir(MYDH, "$path_id") or die("cannot open $path_id: $! \n");
  260. @sellinks = grep(!/\-part\d\d?$/, grep(!/^\.\.?$/, readdir(MYDH)));
  261. @sellinks = sort(@sellinks);
  262. closedir(MYDH);
  263.  
  264. foreach $_idlink (@sellinks) {
  265. chomp $_idlink;
  266. if(not $_idlink =~ /^usb/ and length($_idlink) > 10) {
  267. $_actlink = readlink("$path_id/$_idlink");
  268. $_actlink =~ s{\.\./\.\./}{/dev/};
  269. if($opt_v) { print "** try: $_actlink => $_idlink \n"; }
  270.  
  271. # stop if we find the right link...
  272. if($_actlink eq $olddev) {
  273. $newdevid = $_idlink;
  274. if($opt_v) { print "** convert: $_actlink => $path_id/$_idlink \n\n"; }
  275. last;
  276. }
  277. }
  278. }
  279.  
  280. if(not $newdevid) {
  281. if($opt_v) { print "W: $olddev not converted: link not useful\n\n"; }
  282. }
  283.  
  284. return ($newdevid);
  285. }
  286.  
  287. sub convert_boot_device {
  288.  
  289. my $found = 1;
  290. my $boot_disk = '';
  291. my $boot_link;
  292. # global variables: $boot_dev, $boot_id
  293.  
  294. if (-b $root_dev) {
  295. if ($root_dev =~ /\/dev\/md/) {
  296. # search if the found partition is a raid volume
  297. $boot_disk = check_raid($root_dev);
  298. }
  299. else {
  300. # find the right block device name
  301. $boot_disk = $root_dev;
  302. $boot_disk =~ s/\d+$//;
  303. }
  304.  
  305. if (-b $boot_disk) {
  306. # set global variable boot_dev
  307. $boot_dev = $boot_disk;
  308. }
  309. else {
  310. print "E: boot device $boot_disk does not exist! \n";
  311. $found = 0;
  312. }
  313. }
  314. else {
  315. print "E: could not find root device $root_dev! \n";
  316. $found = 0;
  317. }
  318.  
  319. if ($found) {
  320. if($opt_v) { print "Convert boot option $boot_disk into DISK ID\n"; }
  321. $boot_id = $idpath . "/" . find_id_link($boot_disk,$idpath);
  322.  
  323. if(not -l "$boot_id") {
  324. if($opt_v) { print "W: could not find DISK ID for $boot_disk!\n"; }
  325. # not so important. Then using boot_dev in lilo.conf
  326. }
  327. }
  328.  
  329. return $found;
  330. }
  331.  
  332. sub check_raid {
  333.  
  334. my $part = $_[0];
  335. my $mdname;
  336. my $md;
  337. my @devices;
  338.  
  339. # check if the found partition is a raid volume
  340. if($part =~ /\/dev\/md/)
  341. {
  342. $mdname = $part;
  343. $mdname =~ s/\/dev\///;
  344. $mdname =~ s/\///;
  345. $md = `grep $mdname /proc/mdstat`;
  346.  
  347. @devices = split(" ", $md);
  348. @devices = sort(@devices[4..$#devices]);
  349. $part = "/dev/" . $devices[0];
  350. $part =~ s/\[.*$//;
  351. }
  352.  
  353. return $part;
  354. }
  355.  
  356. sub write_lilo_conf {
  357.  
  358. my @status;
  359. my $exit = copy_template();
  360.  
  361. if (not $exit) {
  362. # create lilo.conf.new
  363. write_bootroot_option();
  364. if ( not write_image_config() ) {
  365. if ( not write_imagelinks_config() ) {
  366. print "E: Cannot find any images or image symlinks!\n";
  367. $exit = 1;
  368. }
  369. }
  370. }
  371.  
  372. if (-f $liloconf and not -f $liloconfold) {
  373. # move old lilo.conf to lilo.conf.old
  374. @status = stat($liloconf);
  375. move ($liloconf, $liloconfold) or die "Cannot rename file: $!\n";
  376. utime ($status[9],$status[9],$liloconfold);
  377. chmod (0600,$liloconfold);
  378. print "Old file moved to: $liloconfold \n";
  379. }
  380. if (-f $liloconfnew) {
  381. move ($liloconfnew, $liloconf) or die "Cannot move file: $!\n";
  382. chmod (0600,$liloconf);
  383. print "New file created as: $liloconf \n";
  384. print "Now you must execute '/sbin/lilo' to " .
  385. "activate this new configuation!\n\n";
  386. }
  387. else {
  388. print "E: Cannot find temporary file $conftmp_1!\n";
  389. $exit = 1;
  390. }
  391.  
  392. return $exit;
  393. }
  394.  
  395. sub copy_template {
  396.  
  397. my $endreached = 0;
  398. my $exit = 0;
  399.  
  400. # copy template config
  401. if (-f $template) {
  402. system("gzip -d -c $template >$conftmp_1") if ($template =~ /\.gz$/);
  403. system("cat $template >$conftmp_1") if ($template =~ /\.conf$/);
  404.  
  405. open(CONFTMP1, "<$conftmp_1") or die "$prog: couldn't open $conftmp_1: $!\n";
  406. open(CONFTMP2, ">$conftmp_2") or die "$prog: couldn't open $conftmp_2: $!\n";
  407.  
  408. while (<CONFTMP1>) {
  409. if (/first\ example/) {
  410. $endreached = 1;
  411. }
  412. unless ($endreached) {
  413. print CONFTMP2 $_;
  414. }
  415. }
  416. close(CONFTMP1) or die "$prog: couldn't close $conftmp_1: $!\n";;
  417. close(CONFTMP2) or die "$prog: couldn't close $conftmp_2: $!\n";;
  418. }
  419. else {
  420. open(CONFTMP2, ">$conftmp_2") or die "$prog: couldn't open $conftmp_2: $!\n";
  421. print CONFTMP2 "# /etc/lilo.conf
  422.  
  423. ### LILO global section ###
  424.  
  425. #large-memory
  426. lba32
  427. boot = /dev/sda
  428. root = /dev/sda1
  429. map = /boot/map
  430. install = menu
  431. menu-scheme = Wb:Yr:Wb:Wb
  432. prompt
  433. timeout = 100
  434. vga = normal
  435. #default = Linux
  436.  
  437. ### LILO per-image section ###
  438.  
  439. ";
  440. close(CONFTMP2) or die "$prog: couldn't close $conftmp_2: $!\n";;
  441. }
  442.  
  443. return $exit;
  444. }
  445.  
  446. sub update_lilo_conf {
  447.  
  448. my @status;
  449. my $exit = 0;
  450.  
  451. if (-f $liloconf) {
  452. # copy old config
  453. system("cat $liloconf >$conftmp_2");
  454.  
  455. # create lilo.conf.new
  456. update_bootroot_option();
  457. }
  458.  
  459. if (-f $liloconf and not -f $liloconfold) {
  460. # move old lilo.conf to lilo.conf.old
  461. @status = stat($liloconf);
  462. move ($liloconf, $liloconfold) or die "Cannot rename file: $!\n";
  463. utime ($status[9],$status[9],$liloconfold);
  464. chmod (0600,$liloconfold);
  465. print "Old file moved to: $liloconfold \n";
  466. }
  467. if (-f $liloconfnew) {
  468. move ($liloconfnew, $liloconf) or die "Cannot move file: $!\n";
  469. chmod (0600,$liloconf);
  470. print "New file created as: $liloconf \n";
  471. print "Now you must execute '/sbin/lilo' to " .
  472. "activate this new configuation!\n\n";
  473. }
  474. else {
  475. print "E: Cannot find temporary file $conftmp_1!\n";
  476. $exit = 1;
  477. }
  478.  
  479. return $exit;
  480. }
  481.  
  482. sub write_bootroot_option {
  483.  
  484. my $oldline = '';
  485. my $newline = '';
  486. my $ok = 0;
  487.  
  488. open(MYFH_NEW, "> $liloconfnew") or die "Cannot open file: $!";
  489. open(MYFH_TMP, "< $conftmp_2") or die "Cannot read file: $!";
  490.  
  491. while (<MYFH_TMP>) {
  492. # line read from MYFH_TMP
  493. $oldline = $_;
  494.  
  495. # lines beginning direct with boot option
  496. if (/^boot/ and $ok == 0) {
  497. if ($boot_id) {
  498. $newline = "#boot = " . $boot_dev . "\n";
  499. print MYFH_NEW $newline;
  500. if($opt_v) { print $newline; }
  501. $newline = "boot = " . $boot_id . "\n";
  502. }
  503. else {
  504. $newline = "boot = " . $boot_dev . "\n";
  505. }
  506. print MYFH_NEW $newline;
  507. if($opt_v) { print $newline; print "\n";}
  508. # convert only one time
  509. $ok = 1;
  510. }
  511. # lines beginning direct with root option
  512. elsif (/^root\ =/) {
  513. if ($root_id) {
  514. $newline = '#root = ' . $root_dev . "\n";
  515. print MYFH_NEW $newline;
  516. if($opt_v) { print $newline; }
  517. $newline = 'root = "' . $root_id . '"' . "\n";
  518. }
  519. else {
  520. $newline = 'root = ' . $root_dev . "\n";
  521. }
  522. print MYFH_NEW $newline;
  523. if($opt_v) { print $newline; print "\n";}
  524. }
  525. # print the rest into file, but not old commented root lines
  526. elsif ( not (/^\#boot\ =/ or /^\#root\ =/) ) {
  527. print MYFH_NEW $oldline;
  528. }
  529. }
  530.  
  531. close(MYFH_TMP);
  532. close(MYFH_NEW);
  533. }
  534.  
  535. sub update_bootroot_option {
  536.  
  537. my $oldline = '';
  538. my $newline = '';
  539. my $ok = 0;
  540.  
  541. open(MYFH_NEW, "> $liloconfnew") or die "Cannot open file: $!";
  542. open(MYFH_TMP, "< $conftmp_2") or die "Cannot read file: $!";
  543.  
  544. while (<MYFH_TMP>) {
  545. # read (old) line from MYFH_TMP
  546. $oldline = $_;
  547.  
  548. # lines beginning direct with boot option
  549. if (/^boot/ and $ok == 0) {
  550. if ($boot_id) {
  551. $newline = "#boot = " . $boot_dev . "\n";
  552. print MYFH_NEW $newline;
  553. if($opt_v) { print $newline; }
  554. $newline = "boot = " . $boot_id . "\n";
  555. }
  556. else {
  557. $newline = "boot = " . $boot_dev . "\n";
  558. }
  559. print MYFH_NEW $newline;
  560. if($opt_v) { print $newline; print "\n";}
  561. # convert only one time
  562. $ok = 1;
  563. }
  564. # lines beginning direct with root option
  565. elsif (/^root\ =/) {
  566. if ($root_id) {
  567. $newline = '#root = ' . $root_dev . "\n";
  568. print MYFH_NEW $newline;
  569. if($opt_v) { print $newline; }
  570. $newline = 'root = "' . $root_id . '"' . "\n";
  571. }
  572. else {
  573. $newline = 'root = ' . $root_dev . "\n";
  574. }
  575. print MYFH_NEW $newline;
  576. if($opt_v) { print $newline; print "\n";}
  577. }
  578. # lines beginning with one tabulator or with two - eight spaces
  579. elsif (/^\troot\ =/ or /^\ {2,8}root\ =/) {
  580. if ($root_id) {
  581. $newline = "\t" . '#root = ' . $root_dev . "\n";
  582. print MYFH_NEW $newline;
  583. if($opt_v) { print $newline; }
  584. $newline = "\t" . 'root = "' . $root_id . '"' . "\n";
  585. }
  586. else {
  587. $newline = "\t" . 'root = ' . $root_dev . "\n";
  588. }
  589. print MYFH_NEW $newline;
  590. if($opt_v) { print $newline; print "\n";}
  591. }
  592. # print the rest into file, but not old commented root lines
  593. elsif ( not (/^\#boot\ =/ or /^\#root\ =/ or /^\t\#root\ =/ or /^\ {2,8}\#root\ =/) ) {
  594. print MYFH_NEW $oldline;
  595. }
  596. }
  597.  
  598. close(MYFH_TMP);
  599. close(MYFH_NEW);
  600. }
  601.  
  602. sub write_image_config {
  603.  
  604. my $image;
  605. my $initrd;
  606. my $initrd2;
  607. my $nr;
  608. my $nr2;
  609.  
  610. # append to new lilo.conf
  611. open(MYFH_NEW, ">> $liloconfnew") or die "Cannot open file: $!";
  612.  
  613. # search for kernel image files
  614. my @vmlinuz = readpipe("/bin/ls -t -1 /boot/vmlinuz* 2>/dev/null");
  615.  
  616. # create some lines for each kernel image
  617. $nr = 0;
  618. foreach $image (@vmlinuz) {
  619. # search for kernel initrd file
  620. chomp $image;
  621. $initrd = $image;
  622. $initrd =~ s/vmlinuz/initrd\.img/;
  623. $initrd2 = $initrd;
  624. $initrd2 =~ s/\.img//;
  625. $nr2 = $nr + 1;
  626.  
  627. print MYFH_NEW 'image = ' . $image . "\n";
  628. if($opt_v) { print 'image = ' . $image . "\n"; }
  629.  
  630. if ($nr == 0) {
  631. print MYFH_NEW "\t" . 'label = "Linux"' . "\n";
  632. if($opt_v) { print "\t" . 'label = "Linux"' . "\n"; }
  633. }
  634. elsif ($nr == 1) {
  635. print MYFH_NEW "\t" . 'label = "Linux Old"' . "\n";
  636. if($opt_v) { print "\t" . 'label = "Linux Old"' . "\n"; }
  637. }
  638.  
  639. print MYFH_NEW "\t" . 'read-only' . "\n";
  640. if($opt_v) { print "\t" . 'read-only' . "\n"; }
  641. print MYFH_NEW "#\t" . 'restricted' . "\n";
  642. if($opt_v) { print "#\t" . 'restricted' . "\n"; }
  643. print MYFH_NEW "#\t" . 'alias = ' . "$nr2" . "\n";
  644. if($opt_v) { print "#\t" . 'alias = ' . "$nr2" . "\n"; }
  645. print MYFH_NEW "#\t" . 'optional' . "\n";
  646. if($opt_v) { print "#\t" . 'optional' . "\n"; }
  647.  
  648. if (-e $initrd) {
  649. print MYFH_NEW "\t" . 'initrd = ' . $initrd . "\n";
  650. if($opt_v) { print "\t" . 'initrd = ' . $initrd . "\n"; }
  651. }
  652. elsif (-e $initrd2) {
  653. print MYFH_NEW "\t" . 'initrd = ' . $initrd2 . "\n";
  654. if($opt_v) { print "\t" . 'initrd = ' . $initrd2 . "\n"; }
  655. }
  656. else {
  657. if($opt_v) { print "W: initrd $initrd could not be found!\n" }
  658. }
  659.  
  660. print MYFH_NEW "\n";
  661. if($opt_v) { print "\n"; }
  662.  
  663. $nr++;
  664. last if ($nr > 1);
  665. }
  666.  
  667. close(MYFH_NEW);
  668.  
  669. if ($nr == 0) {
  670. print "No images '/boot/vmlinuz*' found!\n";
  671. if($opt_v) { print "\n"; }
  672. }
  673. elsif( not $opt_v ) {
  674. print "$nr images '/boot/vmlinuz*' found.\n";
  675. }
  676. return ($nr > 0); # if =0 this is an error
  677. }
  678.  
  679. sub write_imagelinks_config {
  680.  
  681. my $image;
  682. my $initrd;
  683. my $nr;
  684. my $nr2;
  685.  
  686. # append to new lilo.conf
  687. open(MYFH_NEW, ">> $liloconfnew") or die "Cannot open file: $!";
  688.  
  689. # search for kernel image files
  690. my @vmlinuz = readpipe("/bin/ls -t -1 /vmlinuz /vmlinuz.old 2>/dev/null");
  691.  
  692. # create some lines for each kernel image
  693. $nr = 0;
  694. foreach $image (@vmlinuz) {
  695. # search for kernel initrd file
  696. chomp $image;
  697. $initrd = $image;
  698. $initrd =~ s/vmlinuz/initrd\.img/;
  699. $nr2 = $nr + 1;
  700.  
  701. print MYFH_NEW 'image = ' . $image . "\n";
  702. if($opt_v) { print 'image = ' . $image . "\n"; }
  703.  
  704. if ($nr == 0) {
  705. print MYFH_NEW "\t" . 'label = "Linux"' . "\n";
  706. if($opt_v) { print "\t" . 'label = "Linux"' . "\n"; }
  707. }
  708. elsif ($nr == 1) {
  709. print MYFH_NEW "\t" . 'label = "Linux Old"' . "\n";
  710. if($opt_v) { print "\t" . 'label = "Linux Old"' . "\n"; }
  711. }
  712.  
  713. if ($root_id) {
  714. print MYFH_NEW "\t" . '#root = ' . $root_dev . "\n";
  715. if($opt_v) { print "\t" . '#root = ' . $root_dev . "\n"; }
  716. print MYFH_NEW "\t" . 'root = "' . $root_id . '"' . "\n";
  717. if($opt_v) { print "\t" . 'root = "' . $root_id . '"' . "\n"; }
  718. }
  719. else {
  720. print MYFH_NEW "\t" . 'root = ' . $root_dev . "\n";
  721. if($opt_v) { print "\t" . 'root = ' . $root_dev . "\n"; }
  722. }
  723.  
  724. print MYFH_NEW "\t" . 'read-only' . "\n";
  725. if($opt_v) { print "\t" . 'read-only' . "\n"; }
  726. print MYFH_NEW "#\t" . 'restricted' . "\n";
  727. if($opt_v) { print "#\t" . 'restricted' . "\n"; }
  728. print MYFH_NEW "#\t" . 'alias = ' . "$nr2" . "\n";
  729. if($opt_v) { print "#\t" . 'alias = ' . "$nr2" . "\n"; }
  730. print MYFH_NEW "#\t" . 'optional' . "\n";
  731. if($opt_v) { print "#\t" . 'optional' . "\n"; }
  732.  
  733. if (-e $initrd) {
  734. print MYFH_NEW "\t" . 'initrd = ' . $initrd . "\n";
  735. if($opt_v) { print "\t" . 'initrd = ' . $initrd . "\n"; }
  736. }
  737. else {
  738. if($opt_v) { print "W: initrd $initrd could not be found!\n" }
  739. }
  740.  
  741. print MYFH_NEW "\n";
  742. if($opt_v) { print "\n"; }
  743.  
  744. $nr++;
  745. last if ($nr > 1);
  746. }
  747.  
  748. close(MYFH_NEW);
  749.  
  750. if ($nr == 0) {
  751. print "No image symlinks '/vmlinuz*' found!\n";
  752. if($opt_v) { print "\n"; }
  753. }
  754. elsif( not $opt_v ) {
  755. print "$nr image symlinks '/vmlinuz*' found.\n";
  756. }
  757. return ($nr > 0); # if =0 this is an error
  758. }
  759.  
  760. main();
  761.  
  762. __END__
  763.  
  764.  
  765. =head1 NAME
  766.  
  767. liloconfig - create new lilo.conf file (with diskid and uuid)
  768.  
  769. =head1 SYNOPSIS
  770.  
  771. B<liloconfig> [B<-h>] [B<-v>] [B<-f>] [B<-u>] [B<lilo.conf>]
  772.  
  773. =head1 DESCRIPTION
  774.  
  775. liloconfig is an simple program for creating a new lilo.conf file.
  776. After creating the new configuration file you must execute '/sbin/lilo'.
  777.  
  778. liloconfig use the lilo.example.conf file as template. In the final
  779. lilo.conf file you find many useful comments for custom changes.
  780.  
  781. Please pay attention about error messages if liloconfig cannot find
  782. any images (/boot/vmlinuz*) oder image symlinks (/vmlinuz, /vmlinu.old).
  783. Then you need to search for images by ourself and make some changes
  784. in the '/etc/lilo.conf' file. Otherwise no bootloader can be installed
  785. with '/sbin/lilo'.
  786.  
  787. =head1 OPTIONS
  788.  
  789. =over 4
  790.  
  791. =item B<-h>
  792.  
  793. Print a brief help.
  794.  
  795. =item B<-v>
  796.  
  797. Print verbose messages.
  798.  
  799. =item B<-f>
  800.  
  801. Force overriding existing lilo.conf.
  802.  
  803. =item B<-u>
  804.  
  805. Force overriding/update of boot line in lilo.conf.
  806.  
  807. =back
  808.  
  809. =head1 EXAMPLES
  810.  
  811. Lines in the configuration file /etc/lilo.conf:
  812.  
  813. ### LILO global section ###
  814.  
  815. #large-memory
  816. lba32
  817. #boot = /dev/sda
  818. boot = /dev/disk/by-id/ata-SAMSUNG_SV1604N_S01FJ10X999999
  819. #root = /dev/sda1
  820. root = "UUID=18843936-00f9-4df0-a373-000d05a5dd44"
  821. map = /boot/map
  822. install = menu
  823. menu-scheme = Wb:Yr:Wb:Wb
  824. prompt
  825. timeout = 100
  826. vga = normal
  827. #password =
  828. #default = Linux
  829.  
  830. ### LILO per-image section ###
  831.  
  832. image = /boot/vmlinuz-3.17-trunk-686
  833. label = "Linux"
  834. #root = /dev/sda1
  835. #root = "UUID=18843936-00f9-4df0-a373-000d05a5dd44"
  836. read-only
  837. # restricted
  838. # alias = 1
  839. # optional
  840. initrd = /boot/initrd.img-3.17-trunk-686
  841.  
  842. image = /boot/vmlinuz-3.14-trunk-686
  843. label = "Linux Old"
  844. #root = /dev/sda1
  845. #root = "UUID=18843936-00f9-4df0-a373-000d05a5dd44"
  846. read-only
  847. # restricted
  848. # alias = 2
  849. # optional
  850. initrd = /boot/initrd.img-3.14-trunk-686
  851.  
  852. =head1 COPYRIGHT and LICENSE
  853.  
  854. Copyright (C) 2011-2015 Joachim Wiedorn
  855.  
  856. This script is free software; you can redistribute it and/or modify
  857. it under the terms of the GNU General Public License as published by
  858. the Free Software Foundation; either version 2 of the License, or
  859. (at your option) any later version.
  860.  
  861. =head1 AUTHOR
  862.  
  863. B<liloconfig> was written by Joachim Wiedorn.
  864.  
  865. This manual page was written by Joachim Wiedorn <joodevel at joonet.de>.
  866.  
  867. =head1 SEE ALSO
  868.  
  869. B<lilo>(8), B<update-lilo>(8), B<lilo-uuid-diskid>(8)
  870.  
  871. =cut
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement