imagetodir.in 8.2 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2012-2017 University of Utah and the Flux Group.
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
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file 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 Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
use English;
use strict;
use Getopt::Std;
use File::stat;
use File::Basename;

#
# Convert path based image to directory based image.
#
sub usage()
{
    print("Usage: imagetodir [-nq] <imageid>\n" .
36
	  "       imagetodir [-nq] -P pid\n" .
37 38
	  "       imagetodir [-nq] -a\n" .
	  "Options:\n".
39
	  "   -P pid    Do all images for a specific project\n".
40 41 42 43 44
	  "   -a        Do ALL images\n".
	  "   -n        Impotent mode\n".
	  "   -q        Quiet mode\n");
    exit(-1);
}
45
my $optlist   = "qnaP:";
46
my $impotent  = 0;
47 48
my $quiet     = 0;
my $doall     = 0;
49
my $doallpid;
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

#
# Configure variables
#
my $TB		 = "@prefix@";
my $WITHZFS      = @WITHZFS@;
my $ZFS_NOEXPORT = @ZFS_NOEXPORT@;
my $EXPORTSSETUP = "$TB/sbin/exports_setup";
my $PROJROOT     = "@PROJROOT_DIR@";

# 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'};

#
# We don't want to run this script unless you are root.
#
if ($UID != 0) {
    die("*** $0:\n".
	"    Must be root to run this script!\n");
}

#
# Turn off line buffering on output
#
$| = 1;

#
# Load the Testbed support stuff.
#
use lib "@prefix@/lib";
use emutil;
use Node;
88
use OSImage;
89
use User;
90
use Project;
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

#
# 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{"n"})) {
    $impotent = 1;
}
if (defined($options{"q"})) {
    $quiet = 1;
}
106 107 108 109 110 111 112 113 114 115
if (defined($options{"P"})) {
    if ($options{"P"} =~ /^([-\w]+)$/) {
	$doallpid = $1;
    } else {
	fatal("Invalid project name for -P");
    }
    if (!Project->Lookup($doallpid)) {
	fatal("No such project '$doallpid'");
    }
}
116 117 118 119
if (defined($options{"a"})) {
    $doall = 1;
}
usage() 
120
    if (!$doall && !$doallpid && @ARGV != 1);
121 122 123 124 125 126 127 128 129 130

sub ConvertImage($)
{
    my ($image) = @_;

    if ($image->IsDirPath()) {
	print STDERR "$image has already been converted\n";
	return 0;
    }
    my @images = ();
131
    if ($image->AllVersions(\@images, 1)) {
132 133
	print STDERR "Could not get list of (versions) for $image\n";
	return -1;
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
    }
    my $newpath = dirname($image->path()) . "/" . $image->imagename() . "/";
    if (-e $newpath && -f $newpath) {
	print STDERR "$newpath already exists and is a file\n";
	return -1;
    }
    if (! -e $newpath) {
	my ($uid,$gid);
	
	#
	# Project/User info might be gone, so use stat to find out
	# the owner and group of the file, use that for the directory.
	#
	if (-e $image->path()) {
	    $uid = stat($image->path())->uid;
	    $gid = stat($image->path())->gid;
	}
	else {
	    my $project = $image->GetProject();
	    if (!defined($project)) {
		print STDERR "No project for image\n";
		return -1;
	    }
	    $gid = $project->unix_gid();

	    my $user = User->Lookup($image->creator_idx());
160
	    if (!defined($user)) {
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
		print STDERR "No creator for image\n";
		return -1;
	    }
	    $uid = $user->unix_uid();
	}
	if (! (defined($uid) && defined($gid))) {
	    print STDERR "Could not get uid/gid from " . $image->path() . "\n";
	    return -1;
	}
	if ($impotent) {
	    print "Would create $newpath\n" if (! $quiet);
	}
	else {
	    if (! mkdir("$newpath", 0775)) {
		print STDERR "Could not mkdir $newpath: $!\n";
		return -1;
	    }
178 179 180 181
	    if (! chmod(0775, "$newpath")) {
		print STDERR "Could not chown $newpath: $!\n";
		return -1;
	    }
182 183 184 185 186 187 188 189 190 191 192 193 194
	    if (! chown($uid, $gid, $newpath)) {
		print STDERR "Could not chown($uid,$gid) $newpath: $!\n";
		return -1;
	    }
	}
    }
    foreach my $imageversion (@images) {
	my @todelete = ();
	my %torename = ();
	my $imagename= $imageversion->imagename();
	my $filename = $imageversion->path();
	my $basename = basename($filename);

195
	if ($imageversion->isdelta() || $imageversion->HaveDeltaImage()) {
196 197 198 199 200
	    $basename =~ s/\.ndz/\.ddz/;
	}

	push(@todelete, "$filename.bak");
	push(@todelete, "$filename.tmp");
201 202 203
	$torename{$filename} = "${newpath}${basename}";
	$torename{$filename . ".sig"}  = "${newpath}${basename}.sig";
	$torename{$filename . ".sha1"} = "${newpath}${basename}.sha1";
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

	foreach my $file (@todelete) {
	    if (-e $file) {
		if ($impotent) {
		    print "Would delete $file\n" if (! $quiet);
		    next;
		}
		if (! unlink($file)) {
		    print STDERR "Could not unlink $file\n";
		    return -1;
		}
	    }
	}
	foreach my $file (keys(%torename)) {
	    my $newname = $torename{$file};

	    next
		if (! -e $file);
		
	    if ($impotent) {
		if (! $quiet) {
		    print "Would rename $file to $newname\n";
		}
		next;
	    }
	    if (-e $file) {
		system("/bin/mv -fv $file $newname");
		if ($?) {
		    print STDERR "Could not rename $file to $newname\n";
		    return -1;
		}
	    }
	}
	if ($impotent) {
	    print "Would update $imageversion to new directory\n" if (! $quiet);
	    next;
	}
	if ($imageversion->Update({"path" => $newpath})) {
	    print STDERR "Could not update path for $imageversion\n";
	    return -1;
	}
    }
}

248 249
if ($doall || $doallpid) {
    my @images = OSImage->ListAll(undef, $doallpid);
250

251 252 253 254 255
    if ($doallpid && @images == 0) {
	print "No images in project '$doallpid'\n";
	exit(0);
    }

256
    my %lastactive = ();
257 258 259 260 261
    if ($WITHZFS && $ZFS_NOEXPORT) {
	#
	# Have to force the new directories to be exported.
	# See ZFS code in exports_setup
	#
262
	my %projs = ();
263
	foreach my $imagename (@images) {
264 265 266 267 268
	    # XXX OSImage->Lookup blows up on old multi-partition images
	    my $image = Image->Lookup($imagename);
	    if ($image && $image->ezid()) {
		$image = OSImage->Lookup($imagename);
	    }
269
	    my $project = $image->GetProject();
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
	    if (!exists($projs{$project->pid()})) {
		$projs{$project->pid()} = $project;
	    }
	}
	foreach my $pname (keys %projs) {
	    if (! -d "$PROJROOT/$pname/images") {
		my $project = $projs{$pname};
		$lastactive{$pname}{'obj'} = $project;
		$lastactive{$pname}{'last'} = $project->GetActivity();
		if (!$impotent) {
		    $project->BumpActivity();
		}
	    }
	}
	if (keys(%lastactive) > 0) {
	    my $activate = int(keys %lastactive);
	    print STDERR "Found $activate inactive projects, activating ...\n";
	    if (!$impotent) {
		system($EXPORTSSETUP) == 0 or
		    fatal("$EXPORTSSETUP failed");
	    }
291 292 293
	}
    }
    foreach my $imagename (@images) {
294 295 296 297 298
	# XXX OSImage->Lookup blows up on old multi-partition images
	my $image = Image->Lookup($imagename);
	if ($image && $image->ezid()) {
	    $image = OSImage->Lookup($imagename);
	}
299 300 301 302 303 304 305 306 307 308 309
	next
	    if (!defined($image->path()));
	
	if ($WITHZFS) {
	    my $project = $image->GetProject();
	    my $pid = $project->pid();
	    
	    #
	    # There is some lag before the automounter can mount the new volume.
	    #
	    if (emutil::waitForMount("$PROJROOT/$pid") < 0) {
310 311
		print STDERR "Could not access $PROJROOT/$pid, ignoring\n";
		next;
312 313 314 315 316
	    }
	}
	print "--> $image\n";
	ConvertImage($image);
    }
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332

    # Restore last legit activity timestamp for otherwise inactive projects
    if ($WITHZFS && $ZFS_NOEXPORT && keys(%lastactive) > 0) {
	print STDERR "Restoring last activity stamps ...\n";
	foreach my $pname (keys %lastactive) {
	    my $project = $lastactive{$pname}{'obj'};
	    my $last = $lastactive{$pname}{'last'};
	    if (!$impotent) {
		$project->SetActivity($last);
	    }
	}
	if (!$impotent) {
	    system($EXPORTSSETUP) == 0 or
		fatal("$EXPORTSSETUP failed");
	}
    }
333 334
}
else {
335
    my $image = OSImage->Lookup($ARGV[0]);
336 337 338 339 340 341 342 343 344 345 346 347 348 349
    if (!defined($image)) {
	fatal("No such image exists");
    }
    exit(ConvertImage($image));
}
exit(0);

sub fatal($)
{
    my ($mesg) = $_[0];

    die("*** $0:\n".
	"    $mesg\n");
}