Skip to content

Commit d9a090b

Browse files
committed
Merge pull request systemd#490 from pyssling/master
Add machine-id setting
2 parents 7e48712 + ee48dbd commit d9a090b

File tree

7 files changed

+71
-12
lines changed

7 files changed

+71
-12
lines changed

man/kernel-command-line.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
<term><varname>systemd.default_standard_output=</varname></term>
9292
<term><varname>systemd.default_standard_error=</varname></term>
9393
<term><varname>systemd.setenv=</varname></term>
94+
<term><varname>systemd.machine_id=</varname></term>
9495
<listitem>
9596
<para>Parameters understood by the system and service
9697
manager to control system behavior. For details, see

man/machine-id.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@
8484
at install time. Use
8585
<citerefentry><refentrytitle>systemd-firstboot</refentrytitle><manvolnum>1</manvolnum></citerefentry>
8686
to initialize it on mounted (but not booted) system images.</para>
87+
88+
<para>The machine-id may also be set, for example when network
89+
booting, by setting the <varname>systemd.machine_id=</varname>
90+
kernel command line parameter or passing the option
91+
<option>--machine-id=</option> to systemd. A machine-id may not
92+
be set to all zeros.</para>
8793
</refsect1>
8894

8995
<refsect1>

man/systemd.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,14 @@
255255
<option>inherit</option>.</para></listitem>
256256
</varlistentry>
257257

258+
<varlistentry>
259+
<term><option>--machine-id=</option></term>
260+
261+
<listitem><para>Override the machine-id set on the hard drive,
262+
userful for network booting or for containers. May not be set
263+
to all zeros.</para></listitem>
264+
</varlistentry>
265+
258266
<xi:include href="standard-options.xml" xpointer="help" />
259267
<xi:include href="standard-options.xml" xpointer="version" />
260268
</variablelist>
@@ -976,6 +984,15 @@
976984
than once to set multiple variables.</para></listitem>
977985
</varlistentry>
978986

987+
<varlistentry>
988+
<term><varname>systemd.machine_id=</varname></term>
989+
990+
<listitem><para>Takes a 32 character hex value to be
991+
used for setting the machine-id. Intended mostly for
992+
network booting where the same machine-id is desired
993+
for every boot.</para></listitem>
994+
</varlistentry>
995+
979996
<varlistentry>
980997
<term><varname>quiet</varname></term>
981998

src/core/machine-id-setup.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ static int generate_machine_id(char id[34], const char *root) {
198198
return 0;
199199
}
200200

201-
int machine_id_setup(const char *root) {
201+
int machine_id_setup(const char *root, sd_id128_t machine_id) {
202202
const char *etc_machine_id, *run_machine_id;
203203
_cleanup_close_ int fd = -1;
204204
bool writable = true;
@@ -248,15 +248,22 @@ int machine_id_setup(const char *root) {
248248
}
249249
}
250250

251-
if (read_machine_id(fd, id) >= 0)
252-
return 0;
251+
/* A machine id argument overrides all other machined-ids */
252+
if (!sd_id128_is_null(machine_id)) {
253+
sd_id128_to_string(machine_id, id);
254+
id[32] = '\n';
255+
id[33] = 0;
256+
} else {
257+
if (read_machine_id(fd, id) >= 0)
258+
return 0;
253259

254-
/* Hmm, so, the id currently stored is not useful, then let's
255-
* generate one */
260+
/* Hmm, so, the id currently stored is not useful, then let's
261+
* generate one */
256262

257-
r = generate_machine_id(id, root);
258-
if (r < 0)
259-
return r;
263+
r = generate_machine_id(id, root);
264+
if (r < 0)
265+
return r;
266+
}
260267

261268
if (writable)
262269
if (write_machine_id(fd, id) >= 0)

src/core/machine-id-setup.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@
2222
***/
2323

2424
int machine_id_commit(const char *root);
25-
int machine_id_setup(const char *root);
25+
int machine_id_setup(const char *root, sd_id128_t machine_id);

src/core/main.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ static bool arg_default_blockio_accounting = false;
127127
static bool arg_default_memory_accounting = false;
128128
static bool arg_default_tasks_accounting = true;
129129
static uint64_t arg_default_tasks_max = UINT64_C(512);
130+
static sd_id128_t arg_machine_id = {};
130131

131132
static void pager_open_if_enabled(void) {
132133

@@ -300,6 +301,17 @@ static int parse_crash_chvt(const char *value) {
300301
return 0;
301302
}
302303

304+
static int set_machine_id(const char *m) {
305+
306+
if (sd_id128_from_string(m, &arg_machine_id) < 0)
307+
return -EINVAL;
308+
309+
if (sd_id128_is_null(arg_machine_id))
310+
return -EINVAL;
311+
312+
return 0;
313+
}
314+
303315
static int parse_proc_cmdline_item(const char *key, const char *value) {
304316

305317
int r;
@@ -388,6 +400,12 @@ static int parse_proc_cmdline_item(const char *key, const char *value) {
388400
} else
389401
log_warning("Environment variable name '%s' is not valid. Ignoring.", value);
390402

403+
} else if (streq(key, "systemd.machine_id") && value) {
404+
405+
r = set_machine_id(value);
406+
if (r < 0)
407+
log_warning("MachineID '%s' is not valid. Ignoring.", value);
408+
391409
} else if (streq(key, "quiet") && !value) {
392410

393411
if (arg_show_status == _SHOW_STATUS_UNSET)
@@ -743,7 +761,8 @@ static int parse_argv(int argc, char *argv[]) {
743761
ARG_DESERIALIZE,
744762
ARG_SWITCHED_ROOT,
745763
ARG_DEFAULT_STD_OUTPUT,
746-
ARG_DEFAULT_STD_ERROR
764+
ARG_DEFAULT_STD_ERROR,
765+
ARG_MACHINE_ID
747766
};
748767

749768
static const struct option options[] = {
@@ -769,6 +788,7 @@ static int parse_argv(int argc, char *argv[]) {
769788
{ "switched-root", no_argument, NULL, ARG_SWITCHED_ROOT },
770789
{ "default-standard-output", required_argument, NULL, ARG_DEFAULT_STD_OUTPUT, },
771790
{ "default-standard-error", required_argument, NULL, ARG_DEFAULT_STD_ERROR, },
791+
{ "machine-id", required_argument, NULL, ARG_MACHINE_ID },
772792
{}
773793
};
774794

@@ -964,6 +984,14 @@ static int parse_argv(int argc, char *argv[]) {
964984
arg_switched_root = true;
965985
break;
966986

987+
case ARG_MACHINE_ID:
988+
r = set_machine_id(optarg);
989+
if (r < 0) {
990+
log_error("MachineID '%s' is not valid.", optarg);
991+
return r;
992+
}
993+
break;
994+
967995
case 'h':
968996
arg_action = ACTION_HELP;
969997
if (arg_no_pager < 0)
@@ -1617,7 +1645,7 @@ int main(int argc, char *argv[]) {
16171645
status_welcome();
16181646

16191647
hostname_setup();
1620-
machine_id_setup(NULL);
1648+
machine_id_setup(NULL, arg_machine_id);
16211649
loopback_setup();
16221650
bump_unix_max_dgram_qlen();
16231651

src/machine-id-setup/machine-id-setup-main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ int main(int argc, char *argv[]) {
112112
if (arg_commit)
113113
r = machine_id_commit(arg_root);
114114
else
115-
r = machine_id_setup(arg_root);
115+
r = machine_id_setup(arg_root, SD_ID128_NULL);
116116

117117
finish:
118118
free(arg_root);

0 commit comments

Comments
 (0)