All of lore.kernel.org
 help / color / mirror / Atom feed
* [uml-devel] Re: [uml-user] best way for init-script to start/stop UML
       [not found] <c7aquk$trh$1@sea.gmane.org>
@ 2004-05-19 22:13 ` roland
  2004-05-20  0:08   ` Sven Köhler
  0 siblings, 1 reply; 3+ messages in thread
From: roland @ 2004-05-19 22:13 UTC (permalink / raw)
  To: user-mode-linux-user, Sven Köhler; +Cc: user-mode-linux-devel

> How can i gracefully halt a UML-machine? Is there something more simular
i think changing the cad-handler in /etc/inittab would do the trick:
in line:
ca::ctraltdel...
change
"-r" to "-h"

then you should be able to shutdown your uml with "uml_mconsole cad"

imho, it should be possible to issue init-commands to a running uml via uml_mconsole, to switch uml guest systems to whatever
init-level we like. is such feature existing or being planned ?

regards
roland



----- Original Message ----- 
From: "Sven Köhler" <skoehler@upb.de>
To: <user-mode-linux-user@lists.sourceforge.net>
Sent: Wednesday, May 05, 2004 3:41 PM
Subject: [uml-user] best way for init-script to start/stop UML


> hi,
>
> i'm about to write some init-scripts that are supposed to start/stop UML
> machines if the computer started/stopped. I think i can easily start the
> UML-kernel, but stopping it is a problem for me.
>
> I can imagine to do it with mconsole, since there is a halt-command, but
> after the issuing the halt-command in mconsole, the UML-kernel stopps
> running immediatly. I worry about the filesystems of the UML-machine
> that might get damaged.
>
> How can i gracefully halt a UML-machine? Is there something more simular
> to the issuing a halt-command inside the UML?
>
> Since using mconsole is kind of tricky, i'd like to use the
> old-fashioned way with signals. What about SIGINT, SIGTERM, etc.
> Is it safe to send any of those to the UML-kernel? Is any of the signals
> simular to mconsole's halt-command?
>
> Another way could be to send the halt-command directly to the
> UNIX-Socket that mconsole would use. Is there any way of soing that from
> the command-line?
>
> Thx
>    Sven
>
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by: Oracle 10g
> Get certified on the hottest thing ever to hit the market... Oracle 10g.
> Take an Oracle 10g class now, and we'll give you the exam FREE.
> http://ads.osdn.com/?ad_id=3149&alloc_id=8166&op=click
> _______________________________________________
> User-mode-linux-user mailing list
> User-mode-linux-user@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/user-mode-linux-user
>



-------------------------------------------------------
This SF.Net email is sponsored by: Oracle 10g
Get certified on the hottest thing ever to hit the market... Oracle 10g. 
Take an Oracle 10g class now, and we'll give you the exam FREE.
http://ads.osdn.com/?ad_id=3149&alloc_id=8166&op=click
_______________________________________________
User-mode-linux-devel mailing list
User-mode-linux-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/user-mode-linux-devel

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [uml-devel] Re: [uml-user] best way for init-script to start/stop UML
  2004-05-19 22:13 ` [uml-devel] Re: [uml-user] best way for init-script to start/stop UML roland
@ 2004-05-20  0:08   ` Sven Köhler
  2004-06-02 11:17     ` BlaisorBlade
  0 siblings, 1 reply; 3+ messages in thread
From: Sven Köhler @ 2004-05-20  0:08 UTC (permalink / raw)
  To: roland; +Cc: user-mode-linux-user, user-mode-linux-devel

> imho, it should be possible to issue init-commands to a running uml via uml_mconsole, to switch uml guest systems to whatever
> init-level we like. is such feature existing or being planned ?

That would be a good if not the best sollution. Are there any distos 
that don't use init? i don't think so (having that said, i should better 
check gentoo, cause it has a different run-level concept etc.).



-------------------------------------------------------
This SF.Net email is sponsored by: Oracle 10g
Get certified on the hottest thing ever to hit the market... Oracle 10g. 
Take an Oracle 10g class now, and we'll give you the exam FREE.
http://ads.osdn.com/?ad_id=3149&alloc_id=8166&op=click
_______________________________________________
User-mode-linux-devel mailing list
User-mode-linux-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/user-mode-linux-devel

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [uml-devel] Re: [uml-user] best way for init-script to start/stop UML
  2004-05-20  0:08   ` Sven Köhler
@ 2004-06-02 11:17     ` BlaisorBlade
  0 siblings, 0 replies; 3+ messages in thread
From: BlaisorBlade @ 2004-06-02 11:17 UTC (permalink / raw)
  To: Sven Köhler, roland; +Cc: user-mode-linux-user, user-mode-linux-devel

[-- Attachment #1: Type: text/plain, Size: 1914 bytes --]

Alle 02:08, giovedì 20 maggio 2004, Sven Köhler ha scritto:
> > imho, it should be possible to issue init-commands to a running uml via
> > uml_mconsole, to switch uml guest systems to whatever init-level we like.
> > is such feature existing or being planned ?

A lot of time ago I implemented (not in a very good way probably) a patch, 
which worked, to execute via Mconsole any command inside UML. It is attached. 
It is old, ugly and very bad. However, it's already published.

It has no support for console I/O to the command, so it's just a bit of a 
generalization to your request (think of modprobe, for instance). I planned a 
big change which could make it easier: at that time I wrote an ugly parsing 
routine to split the command line in the argv[] array; after that I thought 
that I could pass it to bash:

char *argv[] = {"/bin/sh", "-c", passedString};

exec_usermodehelper(3, argv, envp); /*a wrapper of execve which is normally 
used to call modprobe when needed*/

but I never implemented this, which could also allow a big simplification of 
the patch. This solution would allow I/O, if you use a redirection on the 
mconsole command line!

Btw: I didn't know at that time I could include uml-config.h and do #ifdef 
UML_CONFIG_MCONSOLE_EXEC. So I did another hack for this.

> That would be a good if not the best sollution. Are there any distos
> that don't use init? i don't think so (having that said, i should better
> check gentoo, cause it has a different run-level concept etc.).

Any distro uses init, but Slackware uses a BSD init by default (so that 
runlevels are meaningless except for what is specified inside /etc/inittab), 
Debian uses to run on runlevel 2 (IIRC, never used Debian: stable is old and 
unstable needs fast Internet connection + lot of braveness).

Bye
-- 
Paolo Giarrusso, aka Blaisorblade
Linux registered user n. 292729

[-- Attachment #2: mconsole_exec-04.patch --]
[-- Type: text/x-diff, Size: 7123 bytes --]

--- linuxUm-2.4.22/arch/um/drivers/mconsole_user.c.saved	2003-09-13 20:22:06.000000000 +0200
+++ linuxUm-2.4.22/arch/um/drivers/mconsole_user.c	2003-09-16 18:01:24.000000000 +0200
@@ -21,6 +21,7 @@
 	{ "version", mconsole_version, MCONSOLE_INTR },
 	{ "halt", mconsole_halt, MCONSOLE_PROC },
 	{ "reboot", mconsole_reboot, MCONSOLE_PROC },
+        { "exec", mconsole_exec, MCONSOLE_PROC },
 	{ "config", mconsole_config, MCONSOLE_PROC },
 	{ "remove", mconsole_remove, MCONSOLE_PROC },
 	{ "sysrq", mconsole_sysrq, MCONSOLE_INTR },
--- linuxUm-2.4.22/arch/um/drivers/mconsole_kern.c.saved	2003-09-13 20:22:06.000000000 +0200
+++ linuxUm-2.4.22/arch/um/drivers/mconsole_kern.c	2003-09-16 18:10:21.000000000 +0200
@@ -18,6 +18,7 @@
 #include "linux/file.h"
 #include "linux/fs.h"
 #include "linux/proc_fs.h"
+#include "linux/kmod.h"
 #include "asm/irq.h"
 #include "asm/uaccess.h"
 #include "user_util.h"
@@ -203,13 +204,19 @@
 	/* put_filesystem(proc); */
  out: ;
 }
+#ifdef CONFIG_MCONSOLE_EXEC
+#  define EXEC_HELPTEXT "\
+    exec - execute a command as root inside the UML\n"
+#else
+#  define EXEC_HELPTEXT ""
+#endif
 
 #define UML_MCONSOLE_HELPTEXT \
 "Commands: \n\
     version - Get kernel version \n\
     help - Print this message \n\
     halt - Halt UML \n\
-    reboot - Reboot UML \n\
+    reboot - Reboot UML \n" EXEC_HELPTEXT "\
     config <dev>=<config> - Add a new device to UML;  \n\
 	same syntax as command line \n\
     config <dev> - Query the configuration of a device \n\
@@ -265,6 +272,150 @@
 	reactivate_fd(req->originating_fd, MCONSOLE_IRQ);
 	mconsole_reply(req, "", 0, 0);
 }
+#ifdef CONFIG_MCONSOLE_EXEC
+/* This is the focal point of the exec patch. Much code comes from
+ * kernel/kmod.c. But I just couldn't use call_usermodehelper, because it must be 
+ * called from process context.*/
+struct subprocess_info {
+	char *command;
+	int retval;
+};
+
+/* This function parses arguments from buf to the argv array.
+ * When it goes on a space, starts a new arguments. If it starts with a quote,
+ * it will end at the first matching quote(of the same tipe) which is not escaped with a \.
+ * Otherwise, the arg ends at the first space and can freely contain quotes.
+ * After this, in each argument \<char> is replaced by char.*/
+static void parse_args(char *buf, size_t size, char **argv) {
+	char *start_arg = buf, *end_arg;
+	int i = 0, must_not_break = 1;
+
+	start_arg += strlen("exec");
+
+	while (i < size - 1 && must_not_break) {
+		while(isspace(*start_arg))
+			start_arg++;
+
+		if (*start_arg == '\0')
+			break;
+
+		end_arg = start_arg;
+		if (*start_arg == '\"' || *start_arg == '\'') { /*quoted arg*/
+			end_arg++;
+			/*while (1) {
+				while (*end_arg != *start_arg && *end_arg != '\0')
+					end_arg++;
+				if (*(end_arg - 1) != '\\' || *end_arg == '\0')
+					break;
+				end_arg++;
+			}*/
+			while ((*end_arg != *start_arg || *(end_arg - 1) == '\\')
+				&& *end_arg != '\0')
+				end_arg++;
+			start_arg++;	/*skips the first quote*/
+		} else {
+			while(!isspace(*end_arg) && *end_arg != '\0')
+				end_arg++;
+		}
+		argv[i++] = start_arg;
+
+		if (*end_arg == '\0')
+			must_not_break = 0; /*we have finished, but the break must
+					      be done after we strip '\' out.*/
+		*end_arg = '\0';
+
+		/*let's remove those '\'! The text is shifted back to cover the \'s*/
+		char *curs = start_arg;
+		int displacement = 0;
+		while (*curs != '\\' && *curs != '\0')
+			curs++;
+		while (*curs != '\0') {
+			displacement++;
+			curs++;
+			do {
+				*(curs-displacement) = *curs;
+				curs++;
+			} while (*curs != '\\' && *curs != '\0');
+		}
+		*(curs - displacement) = '\0';
+		start_arg = end_arg + 1;
+	}
+
+	argv[i] = NULL;
+}
+
+/* This code must be called in a new thread, and uses 2.4 exec_usermodehelper
+ * (which seems to be gone inside 2.6) to start the specified program.
+ * This function is like ____call_usermodehelper*/
+static int runner_thread(void * data)
+{
+	struct subprocess_info *sub_info = data;
+
+	char *envp[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
+
+	char *path, *argv[MCONSOLE_MAX_DATA / 2 + 1];
+	char buf[MCONSOLE_MAX_DATA];
+	int retval;
+
+	strcpy(buf, sub_info->command);
+
+	parse_args(buf, MCONSOLE_MAX_DATA / 2 + 1, argv);
+	path = argv[0];
+	if (*argv[0] == '\0')
+		sub_info->retval = -EINVAL; /*invalid command to execute, though this maybe 
+					      is not needed.*/
+	path = argv[0];
+
+	retval = exec_usermodehelper(path, argv, envp);
+
+	/* Exec failed? */
+	sub_info->retval = retval;
+	do_exit(0);
+}
+
+/* This is like __call_usermodehelper. Only exception is that it's not a callback of keventd, and that 
+ * it bundles the declaration of sub_info from call_usermodehelper.*/
+void mconsole_exec(struct mc_request *req)
+{
+	pid_t pid;
+
+	struct subprocess_info sub_info = {
+		command:	req->request.data,
+		retval:		0,
+	};
+
+	/*
+	 * CLONE_VFORK: wait until the usermode helper has execve'd successfully
+	 * We need the data structures to stay around until that is done.
+	 */
+	pid = kernel_thread(runner_thread, &sub_info, CLONE_VFORK | SIGCHLD);
+
+	if (pid < 0) {
+		char buf[60];
+		snprintf(buf, 60, "kernel_thread failed in mconsole_exec with error code: %d", -pid);
+		mconsole_reply(req, buf, 1, 0);
+		return;
+	}
+	if (sub_info.retval < 0) {
+		char buf[50];
+		snprintf(buf, 50, "execve failed in mconsole_exec with errno = %d", -sub_info.retval);
+		mconsole_reply(req, buf, 1, 0);
+		return;
+	}
+
+	mconsole_reply(req, "The command has been started successfully.", 0, 0);
+}
+#else
+/* I must put mconsole_exec in the commands table always, because there 
+ * linux/config.h cannot be included. So I pretend it doesn't exist by hiding
+ * it in the help text*/
+void mconsole_exec(struct mc_request *req)
+{
+	mconsole_reply(req, "Unknown command", 1, 0);
+	/*must be the same as in mconsole_reply*/
+}
+/* Patch end */
+#endif			/*CONFIG_MCONSOLE_EXEC*/
 
 /* This list is populated by __initcall routines. */
 
--- linuxUm-2.4.22/arch/um/config.in.saved	2003-09-13 20:22:06.000000000 +0200
+++ linuxUm-2.4.22/arch/um/config.in	2003-09-16 16:31:13.000000000 +0200
@@ -40,6 +40,7 @@
 tristate 'Host filesystem' CONFIG_HOSTFS
 tristate 'Honeypot proc filesystem' CONFIG_HPPFS
 bool 'Management console' CONFIG_MCONSOLE
+dep_bool 'Execution through management console' CONFIG_MCONSOLE_EXEC $CONFIG_MCONSOLE
 dep_bool 'Magic SysRq key' CONFIG_MAGIC_SYSRQ $CONFIG_MCONSOLE
 bool '2G/2G host address space split' CONFIG_HOST_2G_2G
 bool 'Symmetric multi-processing support' CONFIG_UML_SMP
--- linuxUm-2.4.22/arch/um/include/mconsole.h.saved	2003-09-13 20:22:07.000000000 +0200
+++ linuxUm-2.4.22/arch/um/include/mconsole.h	2003-09-16 18:11:48.000000000 +0200
@@ -73,6 +73,7 @@
 extern void mconsole_help(struct mc_request *req);
 extern void mconsole_halt(struct mc_request *req);
 extern void mconsole_reboot(struct mc_request *req);
+extern void mconsole_exec(struct mc_request *req);
 extern void mconsole_config(struct mc_request *req);
 extern void mconsole_remove(struct mc_request *req);
 extern void mconsole_sysrq(struct mc_request *req);

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-06-02 18:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <c7aquk$trh$1@sea.gmane.org>
2004-05-19 22:13 ` [uml-devel] Re: [uml-user] best way for init-script to start/stop UML roland
2004-05-20  0:08   ` Sven Köhler
2004-06-02 11:17     ` BlaisorBlade

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.