Created
June 22, 2014 09:17
-
-
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 のアドレスは決め打ちなので、流用する場合はネットワークの設定に合わせて変更してください)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- /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