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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
(* libguestfs OCaml bindings
* Copyright (C) 2012 Red Hat Inc.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*)
(* Test guestfs_mount_local, from a higher level language (it will
* mostly be used first from Python), in parallel threads. OCaml
* allows us to test this at a reasonable speed.
*)
open Unix
open Printf
let (//) = Filename.concat
(* See [exit.c]. *)
external _exit : int -> 'a = "ocaml_guestfs__exit"
(* Some settings. *)
let total_time = 60. (* seconds, excluding launch *)
let debug = true (* overview debugging messages *)
let min_threads = 2
let max_threads = 12
let mbytes_per_thread = 900
let clip low high v = min high (max low v)
let rec main () =
Random.self_init ();
(* Choose the number of threads based on the amount of free memory. *)
let nr_threads =
let mbytes =
let cmd = "LANG=C free -m | grep 'buffers/cache' | awk '{print $NF}'" in
let chan = open_process_in cmd in
let mbytes = input_line chan in
match close_process_in chan with
| WEXITED 0 -> Some (int_of_string mbytes)
| _ -> None in
match mbytes with
| None -> min_threads (* default *)
| Some mbytes ->
clip min_threads max_threads (mbytes / mbytes_per_thread) in
let threads = ref [] in
for i = 1 to nr_threads do
let filename = sprintf "test%d.img" i in
let mp = sprintf "mp%d" i in
(try rmdir mp with Unix_error _ -> ());
mkdir mp 0o700;
if debug then eprintf "%s : starting thread\n%!" mp;
let t = Thread.create start_thread (filename, mp) in
threads := (t, filename, mp) :: !threads
done;
(* Wait until the threads terminate and delete the files and mountpoints. *)
List.iter (
fun (t, filename, mp) ->
Thread.join t;
if debug then eprintf "%s : cleaning up thread\n%!" mp;
unlink filename;
rmdir mp
) !threads;
Gc.compact ()
and start_thread (filename, mp) =
(* Create a filesystem for the tests. *)
let g = new Guestfs.guestfs () in
let fd = openfile filename [O_WRONLY;O_CREAT;O_NOCTTY;O_TRUNC] 0o666 in
ftruncate fd (500 * 1024 * 1024);
close fd;
g#add_drive_opts filename;
g#launch ();
g#part_disk "/dev/sda" "mbr";
g#mkfs "ext2" "/dev/sda1";
g#mount "/dev/sda1" "/";
(* Randomly mount the filesystem and repeat. Keep going until we
* finish the test.
*)
let start_t = time () in
let rec loop () =
let t = time () in
if t -. start_t < total_time then (
if debug then eprintf "%s < mounting filesystem\n%!" mp;
g#mount_local mp;
(* Run test in an exec'd subprocess. *)
let args = [| Sys.executable_name; "--test"; mp |] in
let pid = fork () in
if pid = 0 then ( (* child *)
try execv Sys.executable_name args
with exn -> prerr_endline (Printexc.to_string exn); _exit 1
);
(* Run FUSE main loop. This processes requests until the
* subprocess unmounts the filesystem.
*)
g#mount_local_run ();
let _, status = waitpid [] pid in
(match status with
| WEXITED 0 -> ()
| WEXITED i ->
eprintf "test subprocess failed (exit code %d)\n" i;
exit 1
| WSIGNALED i | WSTOPPED i ->
eprintf "test subprocess signaled/stopped (signal %d)\n" i;
exit 1
);
loop ()
)
in
loop ();
g#close ()
(* This is run in a child program. *)
and test_mountpoint mp =
if debug then eprintf "%s | testing filesystem\n%!" mp;
(* Run through the same set of tests repeatedly a number of times.
* The aim of this stress test is repeated mount/unmount, not testing
* FUSE itself, so we don't do much here.
*)
for pass = 0 to Random.int 32 do
mkdir (mp // "tmp.d") 0o700;
let chan = open_out (mp // "file") in
let s = String.make (Random.int (128 * 1024)) (Char.chr (Random.int 256)) in
output_string chan s;
close_out chan;
rename (mp // "tmp.d") (mp // "newdir");
link (mp // "file") (mp // "newfile");
if Random.int 32 = 0 then sleep 1;
rmdir (mp // "newdir");
unlink (mp // "file");
unlink (mp // "newfile")
done;
if debug then eprintf "%s > unmounting filesystem\n%!" mp;
unmount mp
(* We may need to retry this a few times because of processes which
* run in the background jumping into mountpoints. Only display
* errors if it still fails after many retries.
*)
and unmount mp =
let logfile = sprintf "%s.fusermount.log" mp in
let unlink_logfile () =
try unlink logfile with Unix_error _ -> ()
in
unlink_logfile ();
let run_command () =
Sys.command (sprintf "fusermount -u %s >> %s 2>&1"
(Filename.quote mp) (Filename.quote logfile)) = 0
in
let rec loop tries =
if tries <= 5 then (
if not (run_command ()) then (
sleep 1;
loop (tries+1)
)
) else (
ignore (Sys.command (sprintf "cat %s" (Filename.quote logfile)));
eprintf "fusermount: %s: failed, see earlier error messages\n" mp;
exit 1
)
in
loop 0;
unlink_logfile ()
let () =
match Array.to_list Sys.argv with
| [ _; "--test"; mp ] -> test_mountpoint mp
| [ _ ] -> main ()
| _ ->
eprintf "%s: unknown arguments given to program\n" Sys.executable_name;
exit 1
|