Previously cloud-init set VM hostname if EC2/GCE was used as source. But
it ignored hostname if it was specified  via yaml file or
contextualization image. The commit fixes this, so that hostname can be
set via yaml file too.

The example cloud-init.yaml file is updated with commented-out examples
for setting hostname and NFS mounts.

Signed-off-by: Justin Cinkelj <justin.cink...@xlab.si>
---
 modules/cloud-init/cloud-init.cc   |  7 +++++++
 modules/cloud-init/cloud-init.hh   | 10 ++++++++++
 modules/cloud-init/cloud-init.yaml |  7 +++++++
 modules/cloud-init/main.cc         |  1 +
 4 files changed, 25 insertions(+)

diff --git a/modules/cloud-init/cloud-init.cc b/modules/cloud-init/cloud-init.cc
index 6cf9b42..e135c4b 100644
--- a/modules/cloud-init/cloud-init.cc
+++ b/modules/cloud-init/cloud-init.cc
@@ -225,6 +225,13 @@ void mount_module::handle(const YAML::Node& doc)
     }
 }
 
+void hostname_module::handle(const YAML::Node& doc)
+{
+    auto hostname = doc.as<string>();
+    debug("cloudinit hostname: %s\n", hostname.c_str());
+    sethostname(hostname.c_str(), hostname.size());
+}
+
 void osvinit::add_module(std::shared_ptr<config_module> module)
 {
     _modules[module->get_label()] = module;
diff --git a/modules/cloud-init/cloud-init.hh b/modules/cloud-init/cloud-init.hh
index f25bb4c..60b5a5f 100644
--- a/modules/cloud-init/cloud-init.hh
+++ b/modules/cloud-init/cloud-init.hh
@@ -134,6 +134,16 @@ private:
     void yaml_to_request(const YAML::Node& node, http::server::request& req);
 };
 
+class hostname_module : public config_module {
+public:
+    virtual void handle(const YAML::Node& node) override;
+
+    virtual std::string get_label() override
+    {
+        return "hostname";
+    }
+};
+
 }
 
 #endif /* OSVINIT_HH_ */
diff --git a/modules/cloud-init/cloud-init.yaml 
b/modules/cloud-init/cloud-init.yaml
index 885d074..ac7bd7c 100644
--- a/modules/cloud-init/cloud-init.yaml
+++ b/modules/cloud-init/cloud-init.yaml
@@ -1,3 +1,10 @@
 #open httpserver for input
 run:
     - GET: /os/version
+
+# mount NFS export example
+#mounts:
+# - [ nfs://192.168.122.1/ggg, /ggg, auto, uid=0 ]
+
+# set hostname example
+#hostname: uber-vm
diff --git a/modules/cloud-init/main.cc b/modules/cloud-init/main.cc
index ac609aa..a64d342 100644
--- a/modules/cloud-init/main.cc
+++ b/modules/cloud-init/main.cc
@@ -98,6 +98,7 @@ int main(int argc, char* argv[])
         auto scripts = make_shared<script_module>();
         init.add_module(scripts);
         init.add_module(make_shared<mount_module>());
+        init.add_module(make_shared<hostname_module>());
         init.add_module(make_shared<files_module>());
         init.add_module(make_shared<server_module>());
         init.add_module(make_shared<include_module>(init));
-- 
2.5.5

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to osv-dev+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to