Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Download and configure a test image

  • Loading branch information...
commit 1d4d47db386900ace3552d846820000d95cb0657 1 parent ebe7b1d
@msantos authored
View
3  Makefile
@@ -1,4 +1,3 @@
-
REBAR=$(shell which rebar || echo ./rebar)
all: compile
@@ -15,3 +14,5 @@ compile: $(REBAR)
clean: $(REBAR)
@$(REBAR) clean
+test: $(REBAR) compile
+ @$(REBAR) eunit
View
95 bin/get_image.escript
@@ -0,0 +1,95 @@
+#!/usr/bin/env escript
+
+%%%
+%%% Download a test OS image and create the configuration
+%%%
+
+%% Environment Variables
+%%
+%% VERT_QEMU_BIN : path to the qemu binary (default: /usr/bin/kvm)
+%% VERT_BRIDGE_INTERFACE : bridge interface (default: user networking)
+
+main(_) ->
+ inets:start(),
+ ssl:start(),
+
+ URI = "http://downloads.openwrt.org/backfire/10.03.1/x86_generic/openwrt-x86-generic-combined-ext2.img.gz",
+ File = "priv/" ++ filename:rootname(filename:basename(URI)),
+
+ Cfg = "priv/example.xml",
+
+ download(URI, File),
+ config(Cfg, File).
+
+download(URI, File) ->
+ io:format("Checking download test image ...~n"),
+ case file:read_file_info(File) of
+ {ok, _} ->
+ ok;
+ {error, enoent} ->
+ io:format("Downloading image: ~p -> ~p~n", [URI, File]),
+ {ok, _} = httpc:request(get, {URI, []}, [], [{stream, File}]),
+ os:cmd("gunzip " ++ File ++ ".gz")
+ end.
+
+config(Cfg, File) ->
+ io:format("Checking configuration...~n"),
+ case file:read_file_info(Cfg) of
+ {ok, _} ->
+ ok;
+ {error, enoent} ->
+ Dist = Cfg ++ ".dist",
+
+ io:format("Creating config: ~p -> ~p~n", [Dist, Cfg]),
+ {ok, Dir} = file:get_cwd(),
+ {ok, Bin} = file:read_file(Dist),
+
+ Qemu = get_qemu_path(),
+ Interface = get_interface(),
+
+ Bin1 = lists:foldl(fun({Replace, With}, Buf) ->
+ io:format("~s: ~s = ~s~n", [Cfg, Replace, With]),
+ re:replace(Buf, Replace, With, [{return, binary}])
+ end,
+ Bin,
+ [
+ {"@PATH@", Dir ++ "/" ++ File},
+ {"@QEMU@", Qemu},
+ {"@INTERFACE@", Interface}
+ ]),
+
+ ok = file:write_file(Cfg, Bin1)
+ end.
+
+get_qemu_path() ->
+ Qemu = os:getenv("VERT_QEMU_BIN"),
+
+ case Qemu of
+ false -> "/usr/bin/kvm";
+ _ -> Qemu
+ end.
+
+get_interface() ->
+ Bridge = os:getenv("VERT_BRIDGE_INTERFACE"),
+ interface(Bridge).
+
+interface(false) ->
+ io_lib:format(
+ "<interface type='user'>
+ <mac address='~s'/>
+ </interface>", [macaddr()]
+ );
+interface(Bridge) ->
+ io_lib:format(
+ "<interface type='bridge'>
+ <mac address='~s'/>
+ <source bridge='~s'/>
+ <model type='virtio'/>
+ <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
+ </interface>", [macaddr(), Bridge]
+ ).
+
+macaddr() ->
+ "52:54:00:" ++ lists:flatten(string:join(
+ [ io_lib:format("~.16B", [N]) || <<N>> <= crypto:rand_bytes(3) ],
+ ":")).
View
37 priv/example.xml
@@ -0,0 +1,37 @@
+<domain type='qemu'>
+ <name>testvm</name>
+ <uuid>98da79eb-5b19-7be8-cb76-a1acff00e4d3</uuid>
+ <memory>219136</memory>
+ <currentMemory>219136</currentMemory>
+ <vcpu>1</vcpu>
+ <os>
+ <type arch='i686' machine='pc'>hvm</type>
+ <boot dev='hd'/>
+ </os>
+ <clock offset='utc'/>
+ <on_poweroff>destroy</on_poweroff>
+ <on_reboot>restart</on_reboot>
+ <on_crash>destroy</on_crash>
+ <devices>
+ <emulator>/usr/bin/kvm</emulator>
+ <disk type='file' device='disk'>
+ <driver name='qemu' type='raw'/>
+ <source file='/home/msantos/src/erlang/erlang-libvirt/priv/openwrt-x86-generic-combined-ext2.img' />
+ <target dev='hda' bus='ide'/>
+ <address type='drive' controller='0' bus='0' unit='0'/>
+ </disk>
+ <interface type='bridge'>
+ <mac address='52:54:00:34:F6:89'/>
+ <source bridge='br0'/>
+ <model type='virtio'/>
+ <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
+ </interface>
+ <serial type='pty'>
+ <target port='0'/>
+ </serial>
+ <console type='pty'>
+ <target type='serial' port='0'/>
+ </console>
+ <graphics type='vnc' port='-1'/>
+ </devices>
+</domain>
View
32 priv/example.xml.dist
@@ -0,0 +1,32 @@
+<domain type='qemu'>
+ <name>testvm</name>
+ <uuid>98da79eb-5b19-7be8-ca76-a1acff00e4d3</uuid>
+ <memory>219136</memory>
+ <currentMemory>219136</currentMemory>
+ <vcpu>1</vcpu>
+ <os>
+ <type arch='i686' machine='pc'>hvm</type>
+ <boot dev='hd'/>
+ </os>
+ <clock offset='utc'/>
+ <on_poweroff>destroy</on_poweroff>
+ <on_reboot>restart</on_reboot>
+ <on_crash>destroy</on_crash>
+ <devices>
+ <emulator>@QEMU@</emulator>
+ <disk type='file' device='disk'>
+ <driver name='qemu' type='raw'/>
+ <source file='@PATH@' />
+ <target dev='hda' bus='ide'/>
+ <address type='drive' controller='0' bus='0' unit='0'/>
+ </disk>
+ @INTERFACE@
+ <serial type='pty'>
+ <target port='0'/>
+ </serial>
+ <console type='pty'>
+ <target type='serial' port='0'/>
+ </console>
+ <graphics type='vnc' port='-1'/>
+ </devices>
+</domain>
Please sign in to comment.
Something went wrong with that request. Please try again.