|
32 | 32 | #include "sd-bus.h" |
33 | 33 |
|
34 | 34 | #include "alloc-util.h" |
| 35 | +#include "bus-common-errors.h" |
35 | 36 | #include "bus-error.h" |
36 | 37 | #include "bus-unit-util.h" |
37 | 38 | #include "bus-util.h" |
@@ -1523,6 +1524,32 @@ static int read_only_image(int argc, char *argv[], void *userdata) { |
1523 | 1524 | return 0; |
1524 | 1525 | } |
1525 | 1526 |
|
| 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 | + |
1526 | 1553 | static int make_service_name(const char *name, char **ret) { |
1527 | 1554 | _cleanup_free_ char *e = NULL; |
1528 | 1555 | int r; |
@@ -1565,6 +1592,14 @@ static int start_machine(int argc, char *argv[], void *userdata) { |
1565 | 1592 | if (r < 0) |
1566 | 1593 | return r; |
1567 | 1594 |
|
| 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 | + |
1568 | 1603 | r = sd_bus_call_method( |
1569 | 1604 | bus, |
1570 | 1605 | "org.freedesktop.systemd1", |
@@ -1632,6 +1667,14 @@ static int enable_machine(int argc, char *argv[], void *userdata) { |
1632 | 1667 | if (r < 0) |
1633 | 1668 | return r; |
1634 | 1669 |
|
| 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 | + |
1635 | 1678 | r = sd_bus_message_append(m, "s", unit); |
1636 | 1679 | if (r < 0) |
1637 | 1680 | return bus_log_create_error(r); |
|
0 commit comments