/
0003-Allow-changing-of-the-machine-name-on-create.patch
209 lines (185 loc) · 8.31 KB
/
0003-Allow-changing-of-the-machine-name-on-create.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
From 81972746485587285721a54b3559b8eb0f698af0 Mon Sep 17 00:00:00 2001
From: Jason Wessel <jason.wessel@windriver.com>
Date: Wed, 25 Jan 2017 15:04:27 -0800
Subject: [PATCH 3/6] Allow changing of the machine name on create
If instances can be uniquely named they are far easier to manage with
machinectl.
Signed-off-by: Jason Wessel <jason.wessel@windriver.com>
---
src/cmdline.c | 22 +++++++++++++++++++++-
src/cmdline.h | 4 ++++
src/machine.c | 8 ++++++--
src/machine.h | 2 +-
src/pflask.c | 24 ++++++++++++++++++++++--
5 files changed, 54 insertions(+), 6 deletions(-)
diff --git a/src/cmdline.c b/src/cmdline.c
index a50c574..a65a902 100644
--- a/src/cmdline.c
+++ b/src/cmdline.c
@@ -59,6 +59,7 @@ const char *gengetopt_args_info_help[] = {
" -P, --no-pidns Disable PID namespace support (default=off)",
" -C, --uncontain Grant the ability to see the parent namespace",
" -E, --escape-char=INT Set a different escape character (default=0)",
+ " -p, --p-name=STRING Pflask name for machinectl (default=pflask-%d)",
0
};
@@ -102,6 +103,7 @@ void clear_given (struct gengetopt_args_info *args_info)
args_info->detach_given = 0 ;
args_info->uncontain_given = 0 ;
args_info->escape_char_given = 0 ;
+ args_info->p_name_given = 0;
args_info->attach_given = 0 ;
args_info->setenv_given = 0 ;
args_info->keepenv_given = 0 ;
@@ -142,6 +144,8 @@ void clear_args (struct gengetopt_args_info *args_info)
args_info->uncontain_flag = 0;
args_info->escape_char_arg = '\0';
args_info->escape_char_orig = NULL;
+ args_info->p_name_arg = NULL;
+ args_info->p_name_orig = NULL;
args_info->attach_orig = NULL;
args_info->setenv_arg = NULL;
args_info->setenv_orig = NULL;
@@ -200,6 +204,7 @@ void init_args_info(struct gengetopt_args_info *args_info)
args_info->no_pidns_help = gengetopt_args_info_help[22] ;
args_info->uncontain_help = gengetopt_args_info_help[23] ;
args_info->escape_char_help = gengetopt_args_info_help[24] ;
+ args_info->p_name_help = gengetopt_args_info_help[25] ;
}
@@ -336,6 +341,8 @@ cmdline_parser_release (struct gengetopt_args_info *args_info)
free_string_field (&(args_info->chdir_orig));
free_string_field (&(args_info->hostname_arg));
free_string_field (&(args_info->hostname_orig));
+ free_string_field (&(args_info->p_name_arg));
+ free_string_field (&(args_info->p_name_orig));
free_multiple_string_field (args_info->mount_given, &(args_info->mount_arg), &(args_info->mount_orig));
free_multiple_string_field (args_info->netif_given, &(args_info->netif_arg), &(args_info->netif_orig));
free_string_field (&(args_info->user_arg));
@@ -410,6 +417,8 @@ cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
write_into_file(outfile, "uncontain", 0, 0 );
if (args_info->escape_char_given)
write_into_file(outfile, "escape-char", 0, 0 );
+ if (args_info->p_name_given)
+ write_into_file(outfile, "p_name", args_info->p_name_orig, 0);
if (args_info->attach_given)
write_into_file(outfile, "attach", args_info->attach_orig, 0);
write_multiple_into_file(outfile, args_info->setenv_given, "setenv", args_info->setenv_orig, 0);
@@ -1031,10 +1040,11 @@ cmdline_parser_internal (
{ "no-pidns", 0, NULL, 'P' },
{ "uncontain", 0, NULL, 'C' },
{ "escape-char", 1, NULL, 'E' },
+ { "p-name", 1, NULL, 'p' },
{ 0, 0, 0, 0 }
};
- c = getopt_long (argc, argv, "hVr:Cc:t:m:n::u:E:e:wg:b:da:s:kUMNIHP", long_options, &option_index);
+ c = getopt_long (argc, argv, "hVr:Cc:t:m:n::p:u:E:e:wg:b:da:s:kUMNIHP", long_options, &option_index);
if (c == -1) break; /* Exit from `while (1)' loop. */
@@ -1196,6 +1206,16 @@ cmdline_parser_internal (
goto failure;
args_info->escape_char_arg = tmp;
break;
+ case 'p': /* Set the pflask name for machinectl */
+
+ if (update_arg( (void *)&(args_info->p_name_arg),
+ &(args_info->p_name_orig), &(args_info->p_name_given),
+ &(local_args_info.p_name_given), optarg, 0, 0, ARG_STRING,
+ check_ambiguity, override, 0, 0,
+ "p-name", 't',
+ additional_error))
+ goto failure;
+ break;
case 'a': /* Attach to the specified detached process. */
diff --git a/src/cmdline.h b/src/cmdline.h
index ae05f82..712dada 100644
--- a/src/cmdline.h
+++ b/src/cmdline.h
@@ -88,6 +88,9 @@ struct gengetopt_args_info
char escape_char_arg; /**< @brief Set escape character for tty attach. */
char * escape_char_orig; /**< @brief Set escape character for tty attach original value given at command line. */
const char *escape_char_help; /**< @brief Set escape character for tty attach help description. */
+ char * p_name_arg; /**< @brief Set the container p_name. */
+ char * p_name_orig; /**< @brief Set the container p_name original value given at command line. */
+ const char *p_name_help; /**< @brief Set the container p_name help description. */
int attach_arg; /**< @brief Attach to the specified detached process. */
char * attach_orig; /**< @brief Attach to the specified detached process original value given at command line. */
const char *attach_help; /**< @brief Attach to the specified detached process help description. */
@@ -127,6 +130,7 @@ struct gengetopt_args_info
unsigned int detach_given ; /**< @brief Whether detach was given. */
unsigned int uncontain_given ; /**< @brief Whether uncontain was given. */
unsigned int escape_char_given ; /**< @brief Whether escape-char was given. */
+ unsigned int p_name_given ; /**< @brief Whether p_name was given. */
unsigned int attach_given ; /**< @brief Whether attach was given. */
unsigned int setenv_given ; /**< @brief Whether setenv was given. */
unsigned int keepenv_given ; /**< @brief Whether keepenv was given. */
diff --git a/src/machine.c b/src/machine.c
index d066721..ae870e4 100644
--- a/src/machine.c
+++ b/src/machine.c
@@ -32,11 +32,12 @@
#include <unistd.h>
#include <dbus/dbus.h>
+#include <string.h>
#include "printf.h"
#include "util.h"
-void register_machine(pid_t pid, const char *dest) {
+void register_machine(pid_t pid, const char *dest, const char *machine_name) {
int rc;
DBusError err;
@@ -55,7 +56,10 @@ void register_machine(pid_t pid, const char *dest) {
dbus_error_init(&err);
- rc = asprintf(&name, "pflask-%d", pid);
+ if (strstr(machine_name, "%d"))
+ rc = asprintf(&name, machine_name, pid);
+ else
+ rc = asprintf(&name, "%s", machine_name);
fail_if(rc < 0, "OOM");
conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
diff --git a/src/machine.h b/src/machine.h
index 032ced3..ea1cbd1 100644
--- a/src/machine.h
+++ b/src/machine.h
@@ -28,4 +28,4 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-void register_machine(pid_t pid, const char *dest);
+void register_machine(pid_t pid, const char *dest, const char *machine_name);
diff --git a/src/pflask.c b/src/pflask.c
index 886d7dc..0fbd462 100644
--- a/src/pflask.c
+++ b/src/pflask.c
@@ -165,6 +165,24 @@ int main(int argc, char *argv[]) {
capability_add(&caps, args.caps_arg[i]);
#endif
+ /* Check first if the machine name to create already exists */
+ if (args.p_name_arg) {
+ char *systemd_path = "/run/systemd/machines/";
+ int str_size = strlen(systemd_path)+strlen(args.p_name_arg)+1;
+ char *systemd_check_dir = (char *)malloc(str_size*sizeof(char));
+ struct stat sbuf;
+ if (!systemd_check_dir) {
+ printf("OOM\n");
+ exit(1);
+ }
+ snprintf(systemd_check_dir, str_size, "%s%s", systemd_path, args.p_name_arg);
+ if (!stat(systemd_check_dir, &sbuf)) {
+ printf("ERROR: Container %s is already in use\n", args.p_name_arg);
+ exit(1);
+ }
+ free(systemd_check_dir);
+ }
+
if (args.no_userns_flag)
clone_flags &= ~(CLONE_NEWUSER);
@@ -312,8 +330,10 @@ int main(int argc, char *argv[]) {
setup_netif(netifs, pid);
#ifdef HAVE_DBUS
- register_machine(pid, args.chroot_given ? args.chroot_arg : "");
- register_machine(pid, args.root_given ? args.root_arg : "");
+ register_machine(pid, args.chroot_given ? args.chroot_arg : "",
+ args.p_name_arg ? args.p_name_arg : "pflask-%d");
+ register_machine(pid, args.root_given ? args.root_arg : "",
+ args.p_name_arg ? args.p_name_arg : "pflask-%d");
#endif
if (clone_flags & CLONE_NEWUSER)
--
2.11.0