Improve VMware GSX Server support.
[pintos-anon] / src / utils / pintos
1 #! /usr/bin/perl
2
3 $sim = "bochs";
4 $debug = "";
5 $mem = 4;
6 $headless = 0;
7 while (@ARGV) {
8     my ($arg) = shift (@ARGV);
9     if ($arg =~ /--(bochs|qemu|gsx)$/) {
10         $sim = $1;
11     } elsif ($arg =~ /--(monitor|gdb)$/) {
12         $debug = $1;
13     } elsif ($arg eq 'run') {
14         run_vm (@ARGV);
15         exit 0;
16     } elsif ($arg =~ /^mem(?:ory)?=(\d+)/) {
17         $mem = $1;
18     } elsif ($arg eq '--headless') {
19         $headless = 1;
20     } elsif ($arg eq 'make-disk') {
21         my ($force) = @ARGV > 0 && $ARGV[0] eq '--force';
22         shift @ARGV if $force;
23         usage () if @ARGV != 2;
24         my ($file, $mb) = @ARGV;
25         usage () if $mb !~ /^\d+$/;
26         die "$file: already exists\n" if -e $file;
27
28         if ($mb != .5 && $mb != 1 && $mb != 1.5 && $mb != 2) {
29             print "$file: recommended sizes are .5, 1, 1.5, or 2 MB\n";
30             die "use --force to override\n" if !$force;
31         }
32
33         create_disk ($file, $mb * 1008);
34         exit 0;
35     } elsif ($arg eq 'put') {
36         usage () if @ARGV != 1 && @ARGV != 2;
37         my ($hostfn, $guestfn) = @ARGV;
38         $guestfn = $hostfn if !defined $guestfn;
39
40         # Create scratch disk from file.
41         die "$hostfn: $!\n" if ! -e $hostfn;
42         my ($size) = -s _;
43         copy_pad ($hostfn, "scratch.dsk", 512);
44
45         # Do copy.
46         run_vm ("-ci", $hostfn, $size, "-q");
47         exit 0;
48     } elsif ($arg eq 'get') {
49         usage () if @ARGV != 1 && @ARGV != 2;
50         my ($guestfn, $hostfn) = @ARGV;
51         $hostfn = $guestfn if !defined $hostfn;
52         die "$hostfn: already exists\n" if -e $file;
53
54         # Create scratch disk big enough for any file in the filesystem
55         # (modulo sparse files).
56         die "fs.dsk: $!\n" if ! -e "fs.dsk";
57         my ($fs_size) = -s _;
58         my ($scratch_size) = -s "scratch.dsk";
59         $scratch_size = 0 if !defined $scratch_size;
60         create_disk ("scratch.dsk", $fs_size / 1024 + 16)
61             if $scratch_size < $fs_size + 16384;
62
63         # Do copy.
64         run_vm ("-co", $guestfn, "-q");
65
66         # Read out scratch disk.
67         print "copying $guestfn from scratch.dsk to $hostfn...\n";
68         open (SRC, "<scratch.dsk") or die "scratch.dsk: open: $!\n";
69         open (DST, ">$hostfn") or die "$hostfn: create: $!\n";
70         my ($input);
71         read (SRC, $input, 512) == 512 or die "scratch.dsk: read error\n";
72         my ($size) = unpack ("%V", $input);
73         $size != 0xffffffff or die "$guestfn: too big for scratch.dsk?";
74         read (SRC, $src, $size) == $size or die "scratch.dsk: read error\n";
75         print DST $src or die "$hostfn: write error\n";
76         close (DST);
77         close (SRC);
78
79         exit 0;
80     } else {
81         die "unknown option `$arg'\n";
82     }
83 }
84 usage ();
85
86 sub usage {
87     my ($exitcode) = @_;
88     $exitcode = 1 unless defined $exitcode;
89     print "pintos, a utility for invoking Pintos in a simulator\n";
90     print "Usage: pintos [OPTION...] COMMAND [ARG...]\n";
91     print "where COMMAND is one of the following:\n";
92     print "  run [CMDLINE...]        run a VM in the simulator\n";
93     print "  make-disk FILE.DSK SIZE create FILE.DSK as empty SIZE MB disk\n";
94     print "  put HOSTFN [GUESTFN]    copy HOSTFN into VM (as GUESTFN)\n";
95     print "  get GUESTFN [HOSTFN]    copy GUESTFN out of VM (to HOSTFN)\n";
96     print "  help                    print this help message and exit\n";
97     print "Available options:\n";
98     print "  --bochs       Use Bochs as simulator (default)\n";
99     print "  --qemu        Use qemu as simulator\n";
100     print "  --gsx         Use VMware GSX Server 3.x as simulator\n";
101     print "  --monitor     Debug with simulator's monitor\n";
102     print "  --gdb         Debug with gdb\n";
103     print "  --mem=MB      Run VM with MB megabytes of physical memory\n";
104     print "  --headless    No VGA display.  Send VM output to stdout\n";
105     exit $exitcode;
106 }
107
108 sub copy_pad {
109     my ($src, $dst, $blocksize) = @_;
110     run_command ("dd", "if=$src", "of=$dst", "bs=$blocksize", "conv=sync");
111 }
112
113 sub create_disk {
114     my ($disk, $kb) = @_;
115     run_command ("dd", "if=/dev/zero", "of=$disk", "bs=1024", "count=$kb");
116 }
117
118 sub run_vm {
119     my (@disks) = (undef, undef, undef, undef);
120
121     $disks[0] = "os.dsk";
122     $disks[1] = "fs.dsk" if -e "fs.dsk";
123     $disks[2] = "scratch.dsk" if -e "scratch.dsk";
124     $disks[3] = "swap.dsk" if -e "swap.dsk";
125
126     die "$disks[0]: can't find OS disk\n" if ! -e $disks[0];
127     write_cmd_line ($disks[0], @_);
128
129     if ($sim eq 'bochs') {
130         if ($debug eq '') {
131             $bin = 'bochs';
132         } elsif ($debug eq 'monitor') {
133             $bin = 'bochs-dbg';
134         } elsif ($debug eq 'gdb') {
135             $bin = 'bochs-gdb';
136         }
137         $bochsbin = search_path ($bin);
138         $bochsshare = "$bochsbin/../share/bochs";
139         $romimage = "$bochsshare/BIOS-bochs-latest";
140         $vgaromimage = "$bochsshare/VGABIOS-lgpl-latest";
141
142         open (BOCHSRC, ">bochsrc.txt") or die "bochsrc.txt: create: $!\n";
143         print BOCHSRC "romimage: file=$romimage, address=0xf0000\n";
144         print BOCHSRC "vgaromimage: $vgaromimage\n";
145         print BOCHSRC bochs_disk_line ("ata0-master", $disks[0]);
146         print BOCHSRC bochs_disk_line ("ata0-slave", $disks[1]);
147         print BOCHSRC bochs_disk_line ("ata1-master", $disks[2]);
148         print BOCHSRC bochs_disk_line ("ata1-slave", $disks[3]);
149         print BOCHSRC "boot: c\n";
150         print BOCHSRC "ips: 1000000\n";
151         print BOCHSRC "clock: sync=none, time0=0\n";
152         print BOCHSRC "megs: $mem\n";
153         print BOCHSRC "com1: dev=/dev/tty\n" if $headless;
154         close (BOCHSRC);
155         run_command ($bin, '-q');
156     } elsif ($sim eq 'qemu') {
157         my (@cmd) = ('qemu');
158         push (@cmd, '-hda', $disks[0]) if defined $disks[0];
159         push (@cmd, '-hdb', $disks[1]) if defined $disks[1];
160         push (@cmd, '-hdc', $disks[2]) if defined $disks[2];
161         push (@cmd, '-hdd', $disks[3]) if defined $disks[3];
162         push (@cmd, '-m', $mem);
163         push (@cmd, '-nographic') if $headless;
164         push (@cmd, '-S') if $debug eq 'monitor';
165         push (@cmd, '-s') if $debug eq 'gdb';
166         run_command (@cmd);
167     } elsif ($sim eq 'gsx') {
168         print "warning: --$debug not supported by VMware GSX Server, ignoring"
169             if $debug ne '';
170
171         open (VMX, ">pintos.vmx") or die "pintos.vmx: create: $!\n";
172         chmod 0777 & ~umask, "pintos.vmx";
173         print VMX "#! /usr/bin/vmware -G\n";
174         print VMX "config.version = 6\n";
175         print VMX "guestOS = \"linux\"\n";
176         print VMX "floppy0.present = FALSE\n";
177
178         if (! -e 'null.bin') {
179             open (NULL, ">null.bin") or die "null.bin: create: $!\n";
180             close (NULL);
181         }
182
183         for (my ($i) = 0; $i < 4; $i++) {
184             my ($dsk) = $disks[$i];
185             next if !defined $dsk;
186             $device = "ide" . int ($i / 2) . ":" . ($i % 2);
187
188             my ($pln) = $dsk;
189             $pln =~ s/\.dsk//;
190             $pln .= ".pln";
191
192             print VMX "\n$device.present = TRUE\n";
193             print VMX "$device.deviceType = \"plainDisk\"\n";
194             print VMX "$device.fileName = \"$pln\"\n";
195
196             my (%geom) = disk_geometry ($dsk);
197             open (PLN, ">$pln") or die "$pln: create: $!\n";
198             print PLN "DRIVETYPE        ide\n";
199             print PLN "#vm|VERSION      2\n";
200             print PLN "#vm|TOOLSVERSION 2\n";
201             print PLN "CYLINDERS        $geom{C}\n";
202             print PLN "HEADS            $geom{H}\n";
203             print PLN "SECTORS          $geom{S}\n";
204             print PLN "#vm|CAPACITY     $geom{CAPACITY}\n";
205             print PLN "ACCESS \"$dsk\" 0 $geom{CAPACITY}\n";
206             close (PLN);
207         }
208         close (VMX);
209
210         use Cwd;
211         $vmx = getcwd () . "/pintos.vmx";
212         system ("vmware-cmd -s register $vmx >&/dev/null");
213         system ("vmware-cmd $vmx stop hard >&/dev/null");
214         system ("vmware -l -G -x -q $vmx");
215         system ("vmware-cmd $vmx stop hard >&/dev/null");
216     }
217 }
218
219 sub write_cmd_line {
220     my ($disk, @args) = @_;
221
222     die "command line includes empty string" if grep (/^$/, @args);
223     $args = join ("\0", @args) . "\0\0";
224     die "command line exceeds 128 bytes" if length ($args) > 128;
225     $args .= "\0" x (128 - length ($args));
226
227     print "writing command line to $disk...\n";
228     open (DISK, "+<$disk") or die "$disk: open: $!\n";
229     seek (DISK, 0x17e, 0) or die "$disk: seek: $!\n";
230     syswrite (DISK, $args) or die "$disk: write: $!\n";
231     close (DISK) or die "$disk: close: $!\n";
232 }
233
234 sub run_command {
235     print join (' ', @_), "\n";
236     die "command failed\n" if system (@_);
237 }
238
239 sub search_path {
240     my ($target) = @_;
241     for $dir (split (':', $ENV{PATH})) {
242         return $dir if -e "$dir/$target";
243     }
244     die "$target not in PATH\n";
245 }
246
247 sub bochs_disk_line {
248     my ($device, $file) = @_;
249     return "" if !defined $file;
250     my (%geom) = disk_geometry ($file);
251     return "$device: type=disk, path=$file, mode=flat, "
252         . " cylinders=$geom{C}, heads=$geom{H}, spt=$geom{S}, "
253         . "translation=none\n";
254 }
255
256 sub disk_geometry {
257     my ($file) = @_;
258     my ($size) = -s $file;
259     die "$file: stat: $!\n" if !defined $size;
260     die "$file: size not a multiple of 512 bytes\n" if $size % 512;
261     $cylinders = int ($size / (512 * 16 * 63));
262     $cylinders++ if $size % (512 * 16 * 63);
263
264     return (CAPACITY => $size / 512,
265             C => $cylinders,
266             H => 16,
267             S => 63);
268 }