Skip to content
Browse files

Register the virtual machine gen_server

The VM gen_server is now registered using its label so the VM can be
referenced after a supervisor restart.
  • Loading branch information...
1 parent 9b4dd51 commit 2800c193bfe1fa15d076f4a1eaa719f0b3730bb3 @msantos committed
Showing with 24 additions and 21 deletions.
  1. +3 −4 src/evm.erl
  2. +14 −12 src/evum.erl
  3. +7 −5 src/evum_srv.erl
View
7 src/evm.erl
@@ -54,7 +54,7 @@ create(Arg) when is_list(Arg) ->
Image = check_image(Dist),
start_evum(),
- {ok,Ref} = start_vm(atom_to_list(Label), Image, Dist),
+ {ok,Ref} = start_vm(Label, Image, Dist),
wait_prompt(),
@@ -92,10 +92,9 @@ start_evum() ->
start_vm(Label, Image, Dist) ->
Arg = evm_cfg:dist(args, Dist),
- Cow = Image ++ ".cow-" ++ Label ++ "," ++ Image,
+ Cow = Image ++ ".cow-" ++ atom_to_list(Label) ++ "," ++ Image,
Pid = self(),
- {ok,Ref} = supervisor:start_child(evum_sup, [Pid, Arg ++ [{ubd, Cow}]]),
- {ok,Ref}.
+ supervisor:start_child(evum_sup, [Pid, Label, Arg ++ [{ubd, Cow}]]).
mount_proc(Ref) ->
evum:send(Ref, <<"mount -t proc /proc /proc">>).
View
26 src/evum.erl
@@ -33,7 +33,7 @@
-include_lib("kernel/include/inet.hrl").
-export([ping/2, sniff/1, ifconfig/2]).
--export([start/0, start/1, start/2, stop/1, start_link/2]).
+-export([start/0, start/1, start/2, stop/1, start_link/2, start_link/3]).
-export([send/2, send/3, puts/2, system/2, reboot/1]).
-export([defaults/0]).
@@ -49,22 +49,22 @@
]).
-ifconfig(Ref, {IP, Netmask, Default}) when is_pid(Ref) ->
+ifconfig(Ref, {IP, Netmask, Default}) ->
send(Ref, "ifconfig eth0 " ++ IP ++ " netmask " ++ Netmask),
send(Ref, "route add default gw " ++ Default).
-ping(Ref, Hostname) when is_pid(Ref), is_list(Hostname) ->
+ping(Ref, Hostname) when is_list(Hostname) ->
Addr = case inet_res:gethostbyname(Hostname) of
{ok, #hostent{h_addr_list = [IP|_IPs]}} -> IP;
_ -> throw(badarg)
end,
ping(Ref, Addr);
-ping(Ref, {A,B,C,D} = IP) when is_pid(Ref), is_integer(A), is_integer(B), is_integer(C), is_integer(D) ->
+ping(Ref, {A,B,C,D} = IP) when is_integer(A), is_integer(B), is_integer(C), is_integer(D) ->
send(Ref, "ping -w 5 -c 1 " ++ inet_parse:ntoa(IP)).
-sniff(Ref) when is_pid(Ref) ->
+sniff(Ref) ->
send(Ref, "tcpdump -s 0 -w /tmp/sniff.out -i eth0 > /dev/null 2>&1 &").
@@ -80,22 +80,24 @@ start(Pid, Options) ->
start_link(Pid, Options).
start_link(Pid, Options) when is_pid(Pid), is_list(Options) ->
- evum_srv:start_link(Pid, Options).
+ evum_srv:start_link([Pid, Options]).
+start_link(Pid, Label, Options) when is_pid(Pid), is_list(Options) ->
+ evum_srv:start_link([Pid, Label, Options]).
stop(Ref) ->
gen_server:call(Ref, stop).
-reboot(Ref) when is_pid(Ref) ->
+reboot(Ref) ->
system(Ref, <<"reboot">>).
-system(Ref, Command) when is_pid(Ref), is_list(Command) ->
+system(Ref, Command) when is_list(Command) ->
system(Ref, list_to_binary(Command));
-system(Ref, Command) when is_pid(Ref), is_binary(Command) ->
+system(Ref, Command) when is_binary(Command) ->
gen_server:call(Ref, {system, Command}, infinity).
-send(Ref, Data) when is_pid(Ref), ( is_list(Data) orelse is_binary(Data) ) ->
+send(Ref, Data) ->
send(Ref, Data, infinity).
-send(Ref, Data, Timeout) when is_pid(Ref), ( is_list(Data) orelse is_binary(Data) ) ->
+send(Ref, Data, Timeout) when ( is_list(Data) orelse is_binary(Data) ) ->
case gen_server:call(Ref, {send, list_to_binary([Data, "\n"])}, infinity) of
ok ->
receive
@@ -106,5 +108,5 @@ send(Ref, Data, Timeout) when is_pid(Ref), ( is_list(Data) orelse is_binary(Data
Error -> Error
end.
-puts(Ref, Data) when is_pid(Ref), ( is_list(Data) orelse is_binary(Data) ) ->
+puts(Ref, Data) when ( is_list(Data) orelse is_binary(Data) ) ->
gen_server:call(Ref, {send, Data}, infinity).
View
12 src/evum_srv.erl
@@ -36,7 +36,7 @@
-define(PROGNAME, "priv/linux").
-export([start/2, stop/1]).
--export([start_link/2]).
+-export([start_link/1]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
@@ -49,14 +49,16 @@
start(Pid, Options) when is_pid(Pid), is_list(Options) ->
- start_link(Pid, Options).
+ start_link([Pid, Options]).
-stop(Ref) when is_pid(Ref) ->
+stop(Ref) ->
gen_server:call(Ref, stop).
-start_link(Pid, Options) ->
- gen_server:start_link(?MODULE, [Pid, Options], []).
+start_link([Pid, Options]) ->
+ gen_server:start_link(?MODULE, [Pid, Options], []);
+start_link([Pid, Label, Options]) ->
+ gen_server:start_link({local, Label}, ?MODULE, [Pid, Options], []).
init([Pid, Options]) ->

0 comments on commit 2800c19

Please sign in to comment.
Something went wrong with that request. Please try again.