Jaybanuan's Blog

どうせまた調べるハメになることをメモしていくブログ

Vagrant + libvirtでprivate_networkの設定を行うと、NICが増える件

説明

Vagrant + libvirtでprivate_networkの設定を行うと、ゲスト側のNICが増えるのが気になったが、そういうものらしい。

That's not how private_network works. It will always create an extra interface. Vagrant needs the first interface that comes up to sit on the management_network so that it can connect to it and manage the VM.

Vagrantの設計思想とのこと。

No, because it is entirely against the design philosophy. The management network is there to allow Vagrant access to the VM and by not using DHCP we cannot know the IP address of the booted VM because we cannot alther the box VM network configuration until after it booted.

参考

https://github.com/vagrant-libvirt/vagrant-libvirt/issues/613

libvirtで、ネットワークdefaultのDHCPの範囲を変更

環境

Ubuntu 16.04 LTS

$ uname -srvm
Linux 4.4.0-45-generic #66-Ubuntu SMP Wed Oct 19 14:12:37 UTC 2016 x86_64

$ dpkg -l | grep libvirt0
ii  libvirt0:amd64    1.3.1-1ubuntu amd64         library for interfacing with different 

設定手順

ネットワークの設定の編集コマンドを実行。

$ virsh net-edit default

以下のようなXMLがエディタでオープンされるので、それを編集。

<network>
  <name>default</name>
  <uuid>d5e58ece-d152-4a87-a768-4339b82940d5</uuid>
  <forward mode='nat'/>
  <bridge name='virbr0' stp='on' delay='0'/>
  <mac address='52:54:00:3b:67:93'/>
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.2' end='192.168.122.254'/>
    </dhcp>
  </ip>
</network>

DHCPIPアドレスの範囲を変更したいので、<range>を以下のように修正。

<range start='192.168.122.128' end='192.168.122.254'/>

編集完了後、ネットワークの再起動。

$ virsh net-destroy default
ネットワーク default は強制停止されました

$ virsh net-start default
ネットワーク default が起動されました

確認

ネットワークの状態の確認。

$ virsh net-list
 名前               状態     自動起動  永続
----------------------------------------------------------
 default              動作中  はい (yes)  はい (yes)

<range>の変更が反映されているかの確認。

$ virsh net-dumpxml default
<network connections='2'>
  <name>default</name>
  <uuid>d5e58ece-d152-4a87-a768-4339b82940d5</uuid>
  <forward mode='nat'>
    <nat>
      <port start='1024' end='65535'/>
    </nat>
  </forward>
  <bridge name='virbr0' stp='on' delay='0'/>
  <mac address='52:54:00:3b:67:93'/>
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.128' end='192.168.122.254'/>  ← 反映されている
    </dhcp>
  </ip>
</network>

補足

Ubuntuの場合、/etc/libvirt/qemu/networks/default.xmlにネットワークdefaultの設定があるが、コメント欄を読むと「直接編集するな、virsh使え」と書いてある。

<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
virsh net-edit default
or other application using the libvirt API.
-->

<network>
  <name>default</name>
  <uuid>d5e58ece-d152-4a87-a768-4339b82940d5</uuid>
  <forward mode='nat'/>
  <bridge name='virbr0' stp='on' delay='0'/>
  <mac address='52:54:00:3b:67:93'/>
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.2' end='192.168.122.254'/>
    </dhcp>
  </ip>
</network>

Tomcat 8でJAX-RS 2.0 (さらにCDIも利用)

はじめに

CDIを利用したJAX-RS 2.0のアプリを作成し、Tomcat 8で実行するサンプル。 web.xmlレスの情報が少ないので、まとめておく。 ビルドにはJDK 8とMaven 3を利用。 また、JAX-RS 2.0の実装としてJerseyを、CDIの実装としてWeldを利用。 実行にはServlet 3.0に対応したTomcat 7以降(ここで試したのはTomcat 8)が必要。

ディレクトリ構成

.
|-- pom.xml
`-- src
    `-- main
        |-- java
        |   `-- redj
        |       `-- hello
        |           `-- cdi
        |               `-- jersey
        |                   |-- ApplicationConfig.java
        |                   |-- Greeting.java
        |                   `-- GreetingResource.java
        `-- resources
            `-- META-INF
                `-- beans.xml

ソース

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>redj</groupId>
    <artifactId>hello-cdi-jersey</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.22.1</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.ext.cdi</groupId>
            <artifactId>jersey-cdi1x</artifactId>
            <version>2.22.1</version>
        </dependency>
        
        <dependency>
            <groupId>org.jboss.weld.servlet</groupId>
            <artifactId>weld-servlet-core</artifactId>
            <version>2.3.2.Final</version>
        </dependency>
    </dependencies>
</project>

weld-servlet-coreには、WebコンテナでCDIを実現するServletが含まれている。 jersey-cdi1xは、Jersey内のCDIの実装をHK2からCDIに切り替える。

src/main/java/redj/hello/cdi/jersey/Greeting.java

package redj.hello.cdi.jersey;

import javax.enterprise.context.RequestScoped;

@RequestScoped
public class Greeting {

    public String sayHello(String name) {
        return "hello," + name + "!";
    }
}

src/main/java/redj/hello/cdi/jersey/GreetingResource.java

package redj.hello.cdi.jersey;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

@Path("greeting/{name}")
public class GreetingResource {

    @Inject
    private Greeting greeting;

    @GET
    @Produces("text/plain")
    public String sayHello(@PathParam("name") String name) {
        return greeting.sayHello(name);
    }
}

src/main/java/redj/hello/cdi/jersey/ApplicationConfig.java

package redj.hello.cdi.jersey;

import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("hello-cdi-jersey")
public class ApplicationConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<>();
        classes.add(GreetingResource.class);

        return classes;
    }
}

src/main/resources/META-INF/beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans/>

weld-servlet-coreでCDIを有効化している場合、Managed Beanを全てアノテーションで定義していたとしても空のbeans.xmlが必要になる。

https://docs.jboss.org/weld/reference/latest/en-US/html/environments.html

In general, an implicit bean archive does not have to contain a beans.xml descriptor. However, such a bean archive is not supported by Weld Servlet, i.e. it’s excluded from discovery.

ビルド

$ mvn clean install

デプロイ

$ cp target/hello-cdi-jersey-1.0-SNAPSHOT.war $CATALINA_HOME/webapps/

実行

$ curl http://localhost:8080/hello-cdi-jersey-1.0-SNAPSHOT/hello-cdi-jersey/greeting/world
hello,world!

参考

Tomcat 8でJAX-RS 2.0

はじめに

JAX-RS 2.0のアプリを作成し、Tomcat 8で実行するサンプル。 web.xmlレスの情報が少ないので、まとめておく。 ビルドにはJDK 8とMaven 3を利用。 また、JAX-RS 2.0の実装としてJerseyを利用。 実行にはServlet 3.0に対応したTomcat 7以降(ここで試したのはTomcat 8)が必要。

ディレクトリ構成

.
|-- pom.xml
`-- src
    `-- main
        `-- java
            `-- redj
                `-- hello
                    `-- jersey
                        |-- ApplicationConfig.java
                        `-- GreetingResource.java

ソース

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>redj</groupId>
    <artifactId>hello-jersey</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.22.1</version>
        </dependency>
    </dependencies>
</project>

src/main/java/redj/hello/jersey/GreetingResource.java

package redj.hello.jersey;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

@Path("greeting/{name}")
public class GreetingResource {

    @GET
    @Produces("text/plain")
    public String sayHello(@PathParam("name") String name) {
        return "hello," + name + "!";
    }
}

src/main/java/redj/hello/jersey/ApplicationConfig.java

package redj.hello.jersey;

import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("hello-jersey")
public class ApplicationConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<>();
        classes.add(GreetingResource.class);

        return classes;
    }
}

ビルド

$ mvn clean install

デプロイ

$ cp target/hello-jersey-1.0-SNAPSHOT.war $CATALINA_HOME/webapps/

実行

$ curl http://localhost:8080/hello-jersey-1.0-SNAPSHOT/hello-jersey/greeting/world
hello,world!

参考