Skip to content

Instantly share code, notes, and snippets.

@nojima
Created June 22, 2014 09:17
Show Gist options
  • Save nojima/d59cbd199de86aa6e0ee to your computer and use it in GitHub Desktop.
Save nojima/d59cbd199de86aa6e0ee to your computer and use it in GitHub Desktop.
LXC の Ubuntu テンプレートを改造して /etc/hosts にコンテナの IP アドレスを書いておけば、lxc-create の時に自動的にそれを $rootfs/etc/network/interfaces に設定するようにしてみた。(※ netmask や gateway や dns-nameserver のアドレスは決め打ちなので、流用する場合はネットワークの設定に合わせて変更してください)
--- /usr/share/lxc/templates/lxc-ubuntu 2014-04-15 00:49:58.000000000 +0900
+++ /usr/share/lxc/templates/lxc-ubuntu-nojima 2014-06-22 18:03:15.209620112 +0900
@@ -53,21 +53,41 @@
release=$3
user=$4
password=$5
# configure the network using the dhcp
- cat <<EOF > $rootfs/etc/network/interfaces
+ ipaddr=$(sed -e 's/#.*$//' /etc/hosts | awk "\$2~/^${hostname}\$/ { print \$1 }")
+ if [ -z "$ipaddr" ]; then
+ cat <<EOF > $rootfs/etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
EOF
+ else
+ echo "Set the IP address of $hostname to $ipaddr"
+ cat <<EOF > $rootfs/etc/network/interfaces
+# This file describes the network interfaces available on your system
+# and how to activate them. For more information, see interfaces(5).
+
+# The loopback network interface
+auto lo
+iface lo inet loopback
+
+auto eth0
+iface eth0 inet static
+ address $ipaddr
+ netmask 255.255.0.0
+ gateway 10.0.0.1
+ dns-nameservers 10.0.0.6
+EOF
+ fi
# set the hostname
cat <<EOF > $rootfs/etc/hostname
$hostname
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment