Skip to content

Commit 68ce459

Browse files
poetteringmartinpitt
authored andcommitted
machinectl: verify image existance on "start" and "enable" (systemd#3579)
Let's make sure we catch early when a machine doesn't exist that is attempted to be started or enabled as system service.
1 parent 03fb333 commit 68ce459

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/machine/machinectl.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "sd-bus.h"
3333

3434
#include "alloc-util.h"
35+
#include "bus-common-errors.h"
3536
#include "bus-error.h"
3637
#include "bus-unit-util.h"
3738
#include "bus-util.h"
@@ -1523,6 +1524,32 @@ static int read_only_image(int argc, char *argv[], void *userdata) {
15231524
return 0;
15241525
}
15251526

1527+
static int image_exists(sd_bus *bus, const char *name) {
1528+
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1529+
int r;
1530+
1531+
assert(bus);
1532+
assert(name);
1533+
1534+
r = sd_bus_call_method(
1535+
bus,
1536+
"org.freedesktop.machine1",
1537+
"/org/freedesktop/machine1",
1538+
"org.freedesktop.machine1.Manager",
1539+
"GetImage",
1540+
&error,
1541+
NULL,
1542+
"s", name);
1543+
if (r < 0) {
1544+
if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_IMAGE))
1545+
return 0;
1546+
1547+
return log_error_errno(r, "Failed to check whether image %s exists: %s", name, bus_error_message(&error, -r));
1548+
}
1549+
1550+
return 1;
1551+
}
1552+
15261553
static int make_service_name(const char *name, char **ret) {
15271554
_cleanup_free_ char *e = NULL;
15281555
int r;
@@ -1565,6 +1592,14 @@ static int start_machine(int argc, char *argv[], void *userdata) {
15651592
if (r < 0)
15661593
return r;
15671594

1595+
r = image_exists(bus, argv[i]);
1596+
if (r < 0)
1597+
return r;
1598+
if (r == 0) {
1599+
log_error("Machine image '%s' does not exist.", argv[1]);
1600+
return -ENXIO;
1601+
}
1602+
15681603
r = sd_bus_call_method(
15691604
bus,
15701605
"org.freedesktop.systemd1",
@@ -1632,6 +1667,14 @@ static int enable_machine(int argc, char *argv[], void *userdata) {
16321667
if (r < 0)
16331668
return r;
16341669

1670+
r = image_exists(bus, argv[i]);
1671+
if (r < 0)
1672+
return r;
1673+
if (r == 0) {
1674+
log_error("Machine image '%s' does not exist.", argv[1]);
1675+
return -ENXIO;
1676+
}
1677+
16351678
r = sd_bus_message_append(m, "s", unit);
16361679
if (r < 0)
16371680
return bus_log_create_error(r);

0 commit comments

Comments
 (0)