Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smarter hostname #453

Merged
merged 31 commits into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
fbd7265
Port logic to generate hostname from ubiquity
meisenzahl Sep 16, 2020
a04e85d
Set hostname
meisenzahl Sep 16, 2020
2aba686
Satisfy linter
meisenzahl Sep 16, 2020
066cd87
Provide default value
meisenzahl Sep 16, 2020
096906e
Fallback to default with chassis
meisenzahl Sep 17, 2020
3757c6e
Also use machine-id in hostname
meisenzahl Sep 17, 2020
98867ec
Merge branch 'master' into smarter-hostname
meisenzahl Sep 19, 2020
1184434
Do not use dmidecode
meisenzahl Sep 19, 2020
243f534
Merge branch 'master' into smarter-hostname
meisenzahl Sep 27, 2020
4c0131c
Make methods less accessible
meisenzahl Sep 28, 2020
277b696
Merge branch 'smarter-hostname' of github.com:meisenzahl/installer in…
meisenzahl Sep 28, 2020
7329d74
Merge branch 'master' into smarter-hostname
meisenzahl Oct 21, 2020
95db52f
Merge branch 'master' into smarter-hostname
meisenzahl Oct 25, 2020
c4e0471
Merge branch 'master' into smarter-hostname
meisenzahl Oct 28, 2020
bf762a3
Use logic of Ubiquity
meisenzahl Nov 4, 2020
b15dcaf
Merge branch 'smarter-hostname' of github.com:meisenzahl/installer in…
meisenzahl Nov 4, 2020
331cfac
Merge branch 'master' into smarter-hostname
meisenzahl Nov 18, 2020
8a0cedc
Merge branch 'master' into smarter-hostname
meisenzahl Dec 14, 2020
49f44c6
Merge branch 'master' into smarter-hostname
meisenzahl Dec 28, 2020
ade5f2b
Merge branch 'master' into smarter-hostname
cassidyjames Jan 14, 2021
2b18cc0
Merge branch 'master' into smarter-hostname
meisenzahl Jan 30, 2021
d4fc217
Merge branch 'master' into smarter-hostname
meisenzahl Feb 15, 2021
48902f0
Merge branch 'master' into smarter-hostname
meisenzahl Feb 20, 2021
5c8ec60
Update copyright header
meisenzahl Feb 20, 2021
01c7a27
Merge branch 'master' into smarter-hostname
danirabbit Feb 23, 2021
110c980
Merge branch 'master' into smarter-hostname
meisenzahl Feb 27, 2021
428e24a
Fix logic
meisenzahl Mar 1, 2021
0c283a2
Merge branch 'master' into smarter-hostname
meisenzahl Mar 1, 2021
9e02724
Merge branch 'master' into smarter-hostname
meisenzahl Mar 6, 2021
f4c9b66
Throw better error message
meisenzahl Mar 6, 2021
5124959
Merge branch 'master' into smarter-hostname
danirabbit Mar 25, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 128 additions & 2 deletions src/Utils.vala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
/*-
* Copyright (c) 2016–2018 elementary LLC. (https://elementary.io)
*
* Copyright 2016–2021 elementary, Inc. (https://elementary.io)
* Copyright 2006-2021 ubiquity Developers (https://launchpad.net/ubiquity)
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
Expand All @@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored by: Corentin Noël <corentin@elementary.io>
* Marius Meisenzahl <mariusmeisenzahl@gmail.com>
*/

namespace Utils {
Expand Down Expand Up @@ -117,4 +118,129 @@ namespace Utils {

return seat_instance;
}

[DBus (name = "org.freedesktop.hostname1")]
interface HostnameInterface : Object {
public abstract string chassis { owned get; }
}

private static HostnameInterface? hostname_interface_instance;
private static void get_hostname_interface_instance () {
if (hostname_interface_instance == null) {
try {
hostname_interface_instance = Bus.get_proxy_sync (
BusType.SYSTEM,
"org.freedesktop.hostname1",
"/org/freedesktop/hostname1"
);
} catch (GLib.Error e) {
warning ("%s", e.message);
}
}
}

private string get_chassis () {
get_hostname_interface_instance ();

return hostname_interface_instance.chassis;
}

private string? get_machine_id () {
string machine_id;
try {
FileUtils.get_contents ("/etc/machine-id", out machine_id);
} catch (FileError e) {
warning ("%s", e.message);
return null;
}

return machine_id.strip ();
}

private static string? get_vendor () {
string vendor;
try {
FileUtils.get_contents ("/sys/devices/virtual/dmi/id/sys_vendor", out vendor);
} catch (FileError e) {
warning ("%s", e.message);
return null;
}

return vendor.strip ();
}

private static string? get_model () {
string model;
try {
FileUtils.get_contents ("/sys/devices/virtual/dmi/id/product_name", out model);
} catch (FileError e) {
warning ("%s", e.message);
return null;
}

return model.strip ();
}

// Based on https://git.launchpad.net/ubiquity/tree/ubiquity/misc.py?id=ae6415d224c2e76afa2274cc9f85997f38870419#n648
private static string? get_ubiquity_compatible_hostname () {
string model = get_model ();
string manufacturer = get_vendor ();

if (manufacturer.length == 0) {
return null;
}
manufacturer = manufacturer.down ();

if (manufacturer.contains ("to be filled")) {
// Don't bother with products in development.
return null;
}

if (manufacturer.contains ("bochs") || manufacturer.contains ("vmware")) {
model = "virtual machine";
// VirtualBox sets an appropriate system-product-name.
} else {
string key = "system-product-name";
danirabbit marked this conversation as resolved.
Show resolved Hide resolved
if (manufacturer.contains ("lenovo") || manufacturer.contains ("ibm")) {
key = "system-version";
}
}

try {
if (manufacturer.contains ("apple")) {
// MacBook4,1 - strip the 4,1
var re = new Regex ("[^a-zA-Z\\s]");
model = re.replace (model, model.length, 0, "");
}

// Replace each gap of non-alphanumeric characters with a dash.
// Ensure the resulting string does not begin or end with a dash.
var re = new Regex ("[^a-zA-Z0-9]+");
model = re.replace (model, model.length, 0, "-");
while (model[0] == '-') {
model = model.substring (1);
}
while (model[model.length - 1] == '-') {
model = model.substring (0, model.length - 1);
}

if (model.down () == "not-available") {
meisenzahl marked this conversation as resolved.
Show resolved Hide resolved
return null;
}
if (model.down () == "To be filled by O.E.M.".down ()) {
return null;
}
} catch (RegexError e) {
print ("Error: %s\n", e.message);
meisenzahl marked this conversation as resolved.
Show resolved Hide resolved
}

return model;
}

public static string get_hostname () {
string hostname = get_ubiquity_compatible_hostname () ?? ("elementary-os" + "-" + get_chassis ());
hostname += "-" + get_machine_id ().substring (0, 8);

return hostname;
}
}
2 changes: 1 addition & 1 deletion src/Views/ProgressView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public class ProgressView : AbstractInstallerView {

var config = InstallerDaemon.InstallConfig ();
config.flags = Distinst.MODIFY_BOOT_ORDER;
config.hostname = "elementary-os";
config.hostname = Utils.get_hostname ();
config.lang = "en_US.UTF-8";

unowned Configuration current_config = Configuration.get_default ();
Expand Down