Giter VIP home page Giter VIP logo

libvirt-hook-qemu's Introduction

image

Libvirt port-forwarding hook

Libvirt hook for setting up iptables port-forwarding rules when using NAT-ed networking.

Installation

To install the hook script and it's configuration files, simply use the Makefile:

$ sudo make install

Restart your libvirt daemon after installing:

$ sudo service libvirtd restart

Afterwards, customize /etc/libvirt/hooks/hooks.json to your needs. The file includes documentation on how to set up the port forwards, but changes to the file will be reflected only when the guest VM stops and starts again.

The Makefile target can be invoked multiple times, already installed configuration files won't be touched. The files can be removed again with:

$ sudo make uninstall

Testing

To run unit tests use the test target of the Makefile:

$ make test

Or use the Python unittest module to discover tests directly:

python -m unittest discover

Networking

This section describes the theory behind the generated iptables statements.

Packets arriving on the public interface are DNATed to the virtual machine. This implements the actual port-forwarding. Due to the way iptables is implemented, the DNAT must occur in two chains: nat:PREROUTING for packets arriving on the public interface, and nat:OUTPUT for packets originating on the host.

We also add rules to the FORWARD chain to ensure the responses return.

Finally, packets originating on the guest and sent to the host's public IP address need special handling. They are DNATed back to the guest like all other packets but, because the destination is now the same as the source, the reply never leaves the guest. Therefore, the host SNATs these packets to ensure the reply returns over the bridge.

To see a real-world example, the test_setup function in test_qemu.py demonstrates a simple JSON configuration and the iptables rules that it produces.

Authors

  • Sascha Peilicke
  • Scott Bronson

libvirt-hook-qemu's People

Contributors

bronson avatar chebykinn avatar cybershadow avatar davidcassany avatar doggie52 avatar doralitze avatar noxer avatar overlordtm avatar perflyst avatar ponypc avatar ricariel avatar saschpe avatar vonshtirlitz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

libvirt-hook-qemu's Issues

Can't reach services from other host.

Hi
I'm using this hook to make for example https available to the world. When an other guest want to reach the webserver this timed out. What am i doing wrong or where can i fix this.

Thanks!

LICENSE.txt (or LICENSE.md) file is missing, is it intended?

The LICENSE.txt (or LICENSE.md) file is missing. According to https://help.github.com/en/github/creating-cloning-and-archiving-repositories/licensing-a-repository#what-happens-if-i-dont-choose-a-license : "[...] without a license, the default copyright laws apply, meaning that you retain all rights to your source code and no one may reproduce, distribute, or create derivative works from your work[...]". Is it what the authors indent? In other words: could you tell what's the license for the libvirt-hook-qemu, please?

Allow commenting in config file?

Hi, I wish I could include comments in my config file. Here's an example in cjson:

{
    "mailinabox": {
        "interface": "virbr1",
        "private_ip": "192.168.122.10",
        "port_map": {
            "udp": [[53, 53]],     // dns
            "tcp": [[25, 25],      // smtp
                    [53, 53],      // dns
                    [80, 80],      // http
                    [109, 109],    // pop
                    [110, 110],    // pop3
                    [143, 143],    // imap
                    [220, 220],    // imap3
                    [443, 443],    // https
                    [993, 993],    // imaps
                    [995, 995],    // pop3s
                    [4190, 4190]]  // sieve
        }
    }
}

But I suppose you could do something like this too (don't shoot the messenger):

[443, 443], "// https",
[993, 993], "// imaps",

Great script! It saved me a ton of time. They need to link to it from http://wiki.libvirt.org/page/Networking#Forwarding_Incoming_Connections . (I would have but I didn't see how to sign up)

Cannot find qemu.json.

The qemu.json file cannot be found in /etc/libvirt/hooks/.
However, hooks.json is located there. These are the contents of hooks.json:

{
    "cloud-admin": {
        "interface": "virbr1",            // you can use comments
        "private_ip": "192.168.124.10",   /* both styles */
        "source_ip": "8.8.8.8",
        "port_map": {
            "tcp": [[1100, 3000], 443]
        }
    },
    "cloud-node1": {
        "private_ip": "192.168.126.2",
        "port_map": {
            "tcp": [[1101, 80],
                    [1102, 443]]
        }
    }
}

Could anyone advise on how I can edit this file to suit my network configuration settings?
I am using an ethernet connection on the host machine(Ubuntu 16.04LTS), and a NAT configuration on Cent OS 7 VMs.

Use Range port

Hi,

I have been using this scripts for while and I am pretty satisfied with them, great job and so great you shared it. Anyhow some days ago I needed to open a port range so i modified a little the script and the schema in order to accept an entrance like:

"port_range": [{"range": [10000, 10128], "protocol": "udp"}]

Assuming that the mapping is done using the same port numbers. I was wondering if it would be of your interest to include such a modifications in your code, I can provide a Pull Request if you'd like. Some modifications or enhancements could be done, like provide start port number and the number of port to open (instead of min-max ports).

Thanks,
David

Port forwarding for a specific iface

Hi everyone,

Is it possible to setup port forwarding just for a specific network interface?
My case: I have two NICs: eth0, eth1. I want to have port forwarding just for incoming connections to eth0, but refuse all connection attempts to the same tcp port on eth1.
I've tried to play wth "public_ip" param, but unfortunately I didn't get it work, seems that I'm doint something wrong.

Thanks!

Multiple network interfaces for a vhost?

Is there any way to support multiple IP adresses for a VM?

I would like to do this because I need to have more than one Apache virtual host on a VM and allow access to port 80 on both addresses. I need to do that because one of the vhosts needs to be configured with some insecure settings ("HttpProtocolOptions Unsafe") which I wouldn't want to apply to the default vhost.

Right now I don't think I can have two IP addresses because the hooks script assumes that there is exactly one possible value of private_ip per VM, I think.

I can't make one of the Apache vhosts a CNAME (and thus need only one IP address) because this is against best practice for Apache httpd (see https://httpd.apache.org/docs/2.4/dns-caveats.html).

ValueError: need more than 0 values to unpack

Hey,

first of all, thanks for the script. Unfortunately though, it is not working for me. When starting the virtual machine it fails with error code 256. Starting the script from the shell results in the following error:

root@debiantest:/etc/libvirt/hooks# ./qemu
Traceback (most recent call last):
File "./qemu", line 96, in
vir_domain, action = sys.argv[1:3]
ValueError: need more than 0 values to unpack

This is on a debian wheezy machine with python 2.7.3.

oh, and test_qemu.py also fails:

root@debiantest:~/libvirt-hook-qemu-master# python test_qemu.py
Traceback (most recent call last):
File "test_qemu.py", line 5, in
import qemu # Our local libvirt hooks module
ImportError: No module named qemu

Could you give me a hint what is going on here?

Thanks!

make clean fails on Ubuntu 16.04

Hi great tool!

make clean fails on Ubuntu 16.04. Appears to be variable expansion with the {,.json,.schema.json} line.

root@ubuntu-io6-28:~/libvirt-hook-qemu# make clean
rm /etc/libvirt/hooks/hooks{.json,.schema.json}
rm: cannot remove '/etc/libvirt/hooks/hooks{.json,.schema.json}': No such file or directory
Makefile:12: recipe for target 'clean' failed
make: *** [clean] Error 1

Manually expanding those results in:
rm ${LIBVIRT_HOOKS_DIR}/hooks
rm ${LIBVIRT_HOOKS_DIR}/hooks.json
rm ${LIBVIRT_HOOKS_DIR}/hooks.schema.json
and this works as expected.

ips seem to be added wrongly

Hi,
I've got an issue where the host has two ips assigned, and the second of the two ips I assigned for guest's nat ports. However, some of the rules contain both the main ip and the proper nat ip, or in some cases just the main ip (and not the guest's ip). I've attached hooks.json and output of iptables-save, and as you can see, the public_ip is set properly, but the iptables rules still seem to be a bit wonky.
Any help?
-Michael.

iptables.txt

hooks.json.txt

18.04?

hi, has anyone confirmed libvirt-hooks works on 18.04 ubuntu?

I'm having some issues, it looks like the iptables is getting created ok, but maybe it's getting filtered somewhere?

thanks!
Andrew

No port forward rules in iptables

Hi,
after implementing your script and modifying hooks.json, there's not relevant entry when executing iptables -L and port redirection is not working.

I'm running SLES 12SP2.

THX

feature request: support for ipv6 DNAT

My host server only has a private ipv4 address and a global ipv6 address. I want to enable ipv6 forwarding so I can access my guest VM through global ipv6.

I have successfully defined a network with ipv6:

<ip family='ipv6' address='fd12:8848:a2a2:1::1' prefix='64'>
  <dhcp>
    <range start='fd12:8848:a2a2:1::4' end='fd12:8848:a2a2:1::ff'/>
  </dhcp>
</ip>

Then, the address of my guest VM is fd12:8848:a2a2:1::53 and I can connect to it with ssh from the host.

However, this script seemed to only add ipv4 DNAT in iptables. ip6tables -t nat -L did not have any DNAT rules. So, I added it by hand:

sudo ip6tables -t nat -A PREROUTING -p tcp --dport 4022 -j DNAT --to-destination '[fd12:8848:a2a2:1::53]:22'

And everything works perfectly.

But I still want to let this script manages it automatically and it would be appreciated if the developers of the script can add this feature.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.