summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Gilligan <gilligan@vyatta.com>2010-04-30 15:51:31 -0700
committerBob Gilligan <gilligan@vyatta.com>2010-04-30 15:51:31 -0700
commit1e912c8e385c94baff8e54b76532763ee161d575 (patch)
treeed2a65fb425e2e92b915e7f4106b8d6d15fd9aee
parente3759a21689e941a536b9d7f82c54cd7a537d3ac (diff)
downloadvyatta-op-1e912c8e385c94baff8e54b76532763ee161d575.tar.gz
vyatta-op-1e912c8e385c94baff8e54b76532763ee161d575.zip
Added an op-mode command to rename a system image.
Also added an op-mode command to display the Vyatta version strings along with the names of the installed images. This is useful if you rename an image from its default name, which is its version string.
-rw-r--r--Makefile.am1
-rw-r--r--scripts/rename-image.pl97
-rwxr-xr-xscripts/vyatta-boot-image.pl71
-rw-r--r--templates/rename/node.def1
-rw-r--r--templates/rename/system/image/node.def1
-rw-r--r--templates/rename/system/image/node.tag/node.def6
-rw-r--r--templates/rename/system/image/node.tag/node.tag/node.def7
-rw-r--r--templates/rename/system/node.def1
-rw-r--r--templates/show/system/image/version/node.def2
9 files changed, 179 insertions, 8 deletions
diff --git a/Makefile.am b/Makefile.am
index 7174493..0fda3f8 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -23,6 +23,7 @@ bin_SCRIPTS += scripts/show-dhcp-leases.pl
bin_SCRIPTS += scripts/vyatta-boot-image.pl
bin_SCRIPTS += scripts/vyatta-sudo
bin_SCRIPTS += scripts/vyatta-show-snmp.pl
+bin_SCRIPTS += scripts/rename-image.pl
bin_sudo_users_SCRIPTS = scripts/vyatta-identify-interface.pl
bin_sudo_users_SCRIPTS += scripts/vyatta-delete-log-file.sh
diff --git a/scripts/rename-image.pl b/scripts/rename-image.pl
new file mode 100644
index 0000000..d9fbd78
--- /dev/null
+++ b/scripts/rename-image.pl
@@ -0,0 +1,97 @@
+#!/usr/bin/perl
+
+# **** License ****
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# This code was originally developed by Vyatta, Inc.
+# Portions created by Vyatta are Copyright (C) 2010 Vyatta, Inc.
+# All Rights Reserved.
+#
+# Author: Bob Gilligan
+# Date: April 30, 2010
+# Description: Script to re-name a system image.
+#
+# **** End License ****
+
+use strict;
+use warnings;
+use Getopt::Long;
+use File::Temp qw/ tempfile tempdir /;
+
+my $old_name;
+my $new_name;
+
+GetOptions(
+ 'old_name:s' => \$old_name,
+ 'new_name:s' => \$new_name,
+ );
+
+if (!defined($old_name) || !defined($new_name)) {
+ printf("Must specify both old ane new name.\n");
+ exit 1;
+}
+
+my $image_path = "/live/image/boot";
+
+if (! -e "$image_path") {
+ # must be running on old non-image installed system
+ $image_path = "";
+}
+
+if (! -e "$image_path/$old_name") {
+ printf("Old name $old_name does not exist.\n");
+ exit 1;
+}
+
+if (("$new_name" eq "Old-non-image-installation") ||
+ ("$new_name" eq "grub") ||
+ ("$new_name" =~ /^initrd/) ||
+ ("$new_name" =~ /^vmlinuz/) ||
+ ("$new_name" =~ /^System\.map/) ||
+ ("$new_name" =~ /^config-/)) {
+ printf("Can't use reserved image name.\n");
+ exit 1;
+}
+
+if (-e "$image_path/$new_name") {
+ printf("New name $new_name already exists.\n");
+ exit 1;
+}
+
+printf("Renaming image $old_name to $new_name.\n");
+
+my $tmpfh;
+my $tmpfilename;
+($tmpfh, $tmpfilename) = tempfile();
+
+if (!open (GRUBFH, "<${image_path}/grub/grub.cfg")) {
+ printf("Can't open grub file.\n");
+ exit 1;
+}
+
+# This is sensitive to the format of menu entries and boot paths
+# in the grub config file.
+#
+my $line;
+while ($line = <GRUBFH>) {
+ $line =~ s/\/boot\/$old_name/\/boot\/$new_name/g;
+ $line =~ s/Vyatta $old_name/Vyatta $new_name/;
+ $line =~ s/Lost password change $old_name/Lost password change $new_name/;
+ printf($tmpfh $line);
+}
+
+close($tmpfh);
+close(GRUBFH);
+
+system("mv $image_path/$old_name $image_path/$new_name");
+system("cp $tmpfilename $image_path/grub/grub.cfg");
+
+printf("Done.\n");
+
diff --git a/scripts/vyatta-boot-image.pl b/scripts/vyatta-boot-image.pl
index d81a738..9431711 100755
--- a/scripts/vyatta-boot-image.pl
+++ b/scripts/vyatta-boot-image.pl
@@ -57,6 +57,8 @@ sub parseGrubCfg {
my @entries = ();
my $in_entry = 0;
my $idx = 0;
+ my $running_boot_cmd=`cat /proc/cmdline`;
+ $running_boot_cmd =~ s/BOOT_IMAGE=//;
while (<$fd>) {
if ($in_entry) {
if (/^}/) {
@@ -87,6 +89,11 @@ sub parseGrubCfg {
} else {
$ehash{'reset'} = 0;
}
+ if (/$running_boot_cmd/) {
+ $ehash{'running_vers'} = 1;
+ } else {
+ $ehash{'running_vers'} = 0;
+ }
push @entries, \%ehash;
}
} elsif (/^set default=(\d+)$/) {
@@ -179,23 +186,70 @@ sub getBootList {
}
+# Get the Vyatta version of a particular image, given its name.
+#
+sub image_vyatta_version {
+ my ($image_name) = @_;
+
+ my $vers;
+ my $dpkg_path = "var/lib/dpkg";
+
+ my $image_path;
+ if ($image_name eq $OLD_IMG_VER_STR) {
+ $image_path = "";
+ } else {
+ $image_path = "/live/image/boot/$image_name/live-rw";
+ }
+
+ $image_path .= "/var/lib/dpkg";
+
+ if ( -e $image_path ) {
+ $vers = `dpkg-query --admindir=$image_path --showformat='\${Version}' --show vyatta-version`;
+ return $vers;
+ } else {
+ if ($image_name eq $OLD_IMG_VER_STR) {
+ return "unknown";
+ }
+
+ my @squash_files = </live/image/boot/$image_name/*.squashfs>;
+ foreach my $squash_file (@squash_files) {
+ if (-e $squash_file) {
+ system("sudo mkdir /tmp/squash_mount");
+ system("sudo mount -o loop,ro -t squashfs $squash_file /tmp/squash_mount");
+ $image_path = "/tmp/squash_mount/var/lib/dpkg";
+ my $vers = `dpkg-query --admindir=$image_path --showformat='\${Version}' --show vyatta-version`;
+ system("sudo umount /tmp/squash_mount");
+ system("sudo rmdir /tmp/squash_mount");
+ return $vers;
+ }
+ }
+ # None found
+ return "unknown2"
+ }
+}
+
# Prints the boot list generated by getBootList(). If the argument
# $brief is set, display the entries for machine instead of human
# consumption: One entry per line showing the the version string only.
#
sub displayBootList {
- my ($didx, $entries, $brief) = @_;
- my $running_ver = curVer();
+ my ($didx, $entries, $brief, $show_version) = @_;
for my $i (0 .. $#{$entries}) {
my $di = $i + 1;
my $ver = $ {$entries}[$i]->{'ver'};
my $m = '';
+
+ if (defined $show_version) {
+ my $vyatta_vers = image_vyatta_version($ver);
+ $m .= " [$vyatta_vers]";
+ }
+
if ($didx == $ {$entries}[$i]->{'idx'}) {
- $m = ' (default boot)';
+ $m .= ' (default boot)';
}
- if ($ver eq $running_ver) {
- $m .= ' (running version)';
+ if ($ {$entries}[$i]->{'running_vers'} == 1) {
+ $m .= ' (running image)';
}
if (defined($brief)) {
@@ -416,13 +470,14 @@ sub doDelete {
# Main section
#
-my ($show, $del, $sel, $list) = (undef, undef, undef, undef);
+my ($show, $del, $sel, $list, $show_vers) = (undef, undef, undef, undef);
GetOptions(
'show' => \$show,
'delete:s' => \$del,
'select:s' => \$sel,
- 'list' => \$list
+ 'list' => \$list,
+ 'show_vers' => \$show_vers,
);
if (-e $UNION_GRUB_CFG) {
@@ -481,7 +536,7 @@ if (defined($del)) {
$msg = 'The following image(s) can be deleted:';
}
print "$msg\n\n";
-displayBootList($def_idx, $bentries);
+displayBootList($def_idx, $bentries, undef, $show_vers);
print "\n";
exit 0 if (defined($show) || (!defined($sel) && !defined($del))); # show-only
diff --git a/templates/rename/node.def b/templates/rename/node.def
new file mode 100644
index 0000000..b2270fd
--- /dev/null
+++ b/templates/rename/node.def
@@ -0,0 +1 @@
+help: Re-name something.
diff --git a/templates/rename/system/image/node.def b/templates/rename/system/image/node.def
new file mode 100644
index 0000000..00e9c81
--- /dev/null
+++ b/templates/rename/system/image/node.def
@@ -0,0 +1 @@
+help: Re-name a system image.
diff --git a/templates/rename/system/image/node.tag/node.def b/templates/rename/system/image/node.tag/node.def
new file mode 100644
index 0000000..eb25def
--- /dev/null
+++ b/templates/rename/system/image/node.tag/node.def
@@ -0,0 +1,6 @@
+help: System image to rename.
+
+allowed:
+ local -a images ;
+ images=`/opt/vyatta/bin/vyatta-boot-image.pl --list`
+ echo -n $images
diff --git a/templates/rename/system/image/node.tag/node.tag/node.def b/templates/rename/system/image/node.tag/node.tag/node.def
new file mode 100644
index 0000000..1f94c57
--- /dev/null
+++ b/templates/rename/system/image/node.tag/node.tag/node.def
@@ -0,0 +1,7 @@
+help: New name for system image.
+
+run:
+ old=$4
+ new=$5
+ sudo /opt/vyatta/bin/rename-image.pl --old_name $old --new_name $new
+
diff --git a/templates/rename/system/node.def b/templates/rename/system/node.def
new file mode 100644
index 0000000..1d86fc2
--- /dev/null
+++ b/templates/rename/system/node.def
@@ -0,0 +1 @@
+help: Re-name a system object.
diff --git a/templates/show/system/image/version/node.def b/templates/show/system/image/version/node.def
new file mode 100644
index 0000000..b488a12
--- /dev/null
+++ b/templates/show/system/image/version/node.def
@@ -0,0 +1,2 @@
+help: Show installed Vyatta images with Vyatta version number
+run: /opt/vyatta/bin/vyatta-boot-image.pl --show --show_vers