Commit 92b2bc19 authored by Leigh Stoller's avatar Leigh Stoller

New script to compute the hash of an image, create the .sha1 file, and

set the hash in the DB. This is helpful on system images where we save
the image off in /proj and copy it back later, and also for computing
the hash of the zillions of images that already exist.

Usage: imagehash [-d] [-n] <imageid>
       imagehash -p <imageid>
Options:
       -d     Turn on debug mode
       -p     Show the current hash in the DB
       -n     Impotent mode; compute hash but do not update
parent a1a28034
......@@ -29,7 +29,7 @@ SBIN_SCRIPTS = vlandiff vlansync withadminprivs export_tables cvsupd.pl \
archive-expinfo grantfeature emulabfeature addblob readblob \
prereserve grantimage getimages localize_mfs \
management_iface sharevlan check-shared-bw \
addspecialdevice addspecialiface
addspecialdevice addspecialiface imagehash
WEB_SBIN_SCRIPTS= webnewnode webdeletenode webspewconlog webarchive_list \
webwanodecheckin webspewimage
......
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2010-2012 University of Utah and the Flux Group.
# All rights reserved.
#
use English;
use strict;
use Getopt::Std;
use Data::Dumper;
#
# Set/Get the hash for an image.
#
sub usage()
{
print("Usage: imagehash [-d] [-n] <imageid>\n" .
" imagehash -p <imageid>\n" .
"Options:\n".
" -d Turn on debug mode\n".
" -p Show the current hash in the DB\n".
" -n Impotent mode; compute hash but do not update\n");
exit(-1);
}
my $optlist = "dnp";
my $debug = 0;
my $showhash = 0;
my $impotent = 0;
my $hashfile;
#
# Configure variables
#
my $TB = "@prefix@";
my $SHA1 = "/sbin/sha1";
# Protos
sub fatal($);
#
# Untaint the path
#
$ENV{'PATH'} = "$TB/bin:$TB/sbin:/bin:/usr/bin:/usr/bin:/usr/sbin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
#
# Turn off line buffering on output
#
$| = 1;
#
# Load the Testbed support stuff.
#
use lib "@prefix@/lib";
use Image;
use OSinfo;
use User;
#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
usage();
}
if (defined($options{"d"})) {
$debug = 1;
}
if (defined($options{"n"})) {
$impotent = 1;
}
if (defined($options{"p"})) {
$showhash = 1;
}
usage()
if (@ARGV != 1);
my $image = Image->Lookup($ARGV[0]);
if (!defined($image)) {
fatal("No such image exists");
}
my $imageid = $image->imageid();
my $path = $image->path();
my $hash = $image->hash();
if ($showhash) {
$hash = "No hash yet" if (!defined($hash));
print "$path = $hash\n";
exit(0);
}
fatal("$path does not exist")
if (! -e $path);
if ($path =~ /(.*)\.ndz$/) {
$hashfile = "${1}.sha1";
}
else {
$hashfile = "${path}.sha1";
}
my $filehash = `$SHA1 $path`;
if ($?) {
fatal("Could not generate sha1 hash of $path");
}
if ($filehash =~ /^SHA1.*= (\w*)$/) {
$hash = $1;
if (!$impotent) {
$image->SetHash($1) == 0
or fatal("Failed to set the hash for $image");
}
}
else {
fatal("Could not parse the sha1 hash: '$filehash'")
}
if (!$impotent) {
unlink($hashfile)
if (-e $hashfile);
open(HASH, ">$hashfile") or
fatal("Could not open $hashfile for writing: $!");
print HASH $filehash;
close($hashfile);
}
print "$path = $hash\n";
exit(0);
sub fatal($)
{
my ($mesg) = $_[0];
die("*** $0:\n".
" $mesg\n");
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment