File: diskimage.pl

package info (click to toggle)
bochs 2.3-2etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 14,116 kB
  • ctags: 16,927
  • sloc: cpp: 130,524; ansic: 18,822; sh: 7,922; makefile: 3,836; yacc: 1,056; asm: 463; perl: 381; lex: 280; csh: 3
file content (117 lines) | stat: -rwxr-xr-x 3,233 bytes parent folder | download | duplicates (12)
1
2
3
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/perl

#
#
#	Copyright (C) 1991-2002 and beyond by Bungie Studios, Inc.
#	and the "Aleph One" developers.
#
#	This program is free software; you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation; either version 2 of the License, or
#	(at your option) any later version.
#
#	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 license is contained in the file "COPYING",
#	which is included with this source code; it is available online at
#	http://www.gnu.org/licenses/gpl.html
#
#

my $fullfolderPath = shift(@ARGV);
die "err: No folder specified" unless defined($fullfolderPath) && length($fullfolderPath);

$fullfolderPath =~ s{/$}{};
$fullfolderPath =~ m{([^/]+)$};

local $folderName   = $1;
local $folderSize   = undef;
local $imageName    = "'$fullfolderPath.dmg'";
local $imageSectors = undef;
local $imageTemp    = "'$fullfolderPath-tmp.dmg'";

local $mount_point = "bochs-image-mount";

die "err: $folderName is not a directory\n" if(!-d $fullfolderPath);

# Know a better way to get the first value from du? 
($folderSize) = split(m/ /, `du -s "$fullfolderPath"`);
die "err: du failed with $?\n" if($?);

# Inflate $folderSize for disk image overhead. Minimum 5 MB disk
local $fiveMBImage=20*(2048);
# BBD: I had to raise this to 20meg or the ditto command would run 
# out of disk space.  Apparently the technique of measuring the
# amount of space required is not working right.

$imageSectors = $folderSize + int($folderSize * 0.15);
if($imageSectors < $fiveMBImage)
{
	$imageSectors = $fiveMBImage;
}
print "Minimum sectors = $fiveMBImage\n";
print "Folder sectors = $folderSize\n";
print "Image sectors = $imageSectors\n";

# Create image, overwriting prior version
`hdiutil create -ov -sectors $imageSectors $imageTemp`;
die "err: hdiutil create failed with $?\n" if($?);

# Initialize the image
local $hdid_info=`hdid -nomount $imageTemp`;
die "err: hdid  -nomount failed with $?\n" if($?);

$hdid_info =~ s/( |\t|\n)+/~!/g;
local (@hdid_info) = split(m/~!/, $hdid_info);

local ($disk_dev, $hfs_dev);

$disk_dev = $hdid_info[0];
$hfs_dev = $hdid_info[4];
$mount_dev = $hdid_info[4];

$disk_dev =~ s{/dev/}{};
$hfs_dev =~ s/disk/rdisk/;

`newfs_hfs -v "$folderName" $hfs_dev`;
if($?)
{
	local $err = $?;
	`hdiutil eject $disk_dev`;
	die "err: newfs_hfs failed with $err\n";
}

# Fill the image

`mkdir $mount_point`;
`/sbin/mount -t hfs $mount_dev $mount_point`;
if($?)
{
	local $err = $?;
	`hdiutil eject $disk_dev`;
	die "err: mount failed with $err\n";
}

`ditto -rsrcFork "$fullfolderPath" $mount_point`;
if($?)
{
	local $err = $?;
	`umount $mount_dev`;
	`hdiutil eject $disk_dev`;
	`rmdir $mount_point`;
	die "err: ditto failed with $err\n";
}
`umount $mount_dev`;
`hdiutil eject $disk_dev`;
`rmdir $mount_point`;

# Create the compressed image
`hdiutil convert $imageTemp -format UDCO -o $imageName`;
die "err: hdiutil convert failed with $?\n" if($?);

`rm $imageTemp`;

print "$imageName is your new diskimage\n";