* [PATCH 2.6.28-rc2] gen_init_cpio expands shell variables in file names
@ 2008-10-29 13:54 Sally, Gene
2008-11-22 11:35 ` Sam Ravnborg
0 siblings, 1 reply; 2+ messages in thread
From: Sally, Gene @ 2008-10-29 13:54 UTC (permalink / raw)
To: linux-kernel
Hi,
This patch modifies gen_init_cpio so that lines that specify files can contain what looks like a shell variable that's expanded during processing.
For example:
file /sbin/kinit ${RFS_BASE}/usr/src/klibc/kinit/kinit 0755 0 0
given RFS_BASE is "/some/directory" in the environment
would be expanded to
file /sbin/kinit /some/directory/usr/src/klibc/kinit/kinit 0755 0 0
If several environment variables appear in a line, they are all expanded with processing happening from left to right. Undefined variables expand to a null string. Syntax errors stop processing, letting the existing error handling show the user offending line.
This patch helps embedded folks who frequently create several RFS directories and then switch between them as they're tuning an initramfs. Having just been such an embedded folk, this seems the most efficient solution.
I didn't see a maintainer for this area (forgive me if I overlooked) so I don't know who I should CC for this patch.
Regards,
gene
--- a/usr/gen_init_cpio.c
+++ b/usr/gen_init_cpio.c
@@ -370,6 +370,30 @@ error:
return rc;
}
+static char *cpio_replace_env(char *new_location)
+{
+ char expanded[PATH_MAX + 1];
+ char env_var[PATH_MAX + 1];
+ char *start;
+ char *end;
+
+ for (start = NULL; (start = strstr(new_location, "${")); ) {
+ end = strchr(start, '}');
+ if (start < end) {
+ *env_var = *expanded = '\0';
+ strncat(env_var, start + 2, end - start - 2);
+ strncat(expanded, new_location, start - new_location);
+ strncat(expanded, getenv(env_var), PATH_MAX);
+ strncat(expanded, end + 1, PATH_MAX);
+ strncpy(new_location, expanded, PATH_MAX);
+ } else
+ break;
+ }
+
+ return new_location;
+}
+
+
static int cpio_mkfile_line(const char *line)
{
char name[PATH_MAX + 1];
@@ -415,7 +439,8 @@ static int cpio_mkfile_line(const char *line)
} else {
dname = name;
}
- rc = cpio_mkfile(dname, location, mode, uid, gid, nlinks);
+ rc = cpio_mkfile(dname, cpio_replace_env(location),
+ mode, uid, gid, nlinks);
fail:
if (dname_len) free(dname);
return rc;
@@ -439,6 +464,7 @@ void usage(const char *prog)
"\n"
"<name> name of the file/dir/nod/etc in the archive\n"
"<location> location of the file in the current filesystem\n"
+ " expands shell variables quoted with ${}\n"
"<target> link target\n"
"<mode> mode/permissions of the file\n"
"<uid> user id (0=root)\n"
Signed-off-by: gene.sally@timesys.com
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH 2.6.28-rc2] gen_init_cpio expands shell variables in file names
2008-10-29 13:54 [PATCH 2.6.28-rc2] gen_init_cpio expands shell variables in file names Sally, Gene
@ 2008-11-22 11:35 ` Sam Ravnborg
0 siblings, 0 replies; 2+ messages in thread
From: Sam Ravnborg @ 2008-11-22 11:35 UTC (permalink / raw)
To: Sally, Gene; +Cc: linux-kernel
On Wed, Oct 29, 2008 at 09:54:17AM -0400, Sally, Gene wrote:
> Hi,
>
> This patch modifies gen_init_cpio so that lines that specify files can contain what looks like a shell variable that's expanded during processing.
>
> For example:
>
> file /sbin/kinit ${RFS_BASE}/usr/src/klibc/kinit/kinit 0755 0 0
>
> given RFS_BASE is "/some/directory" in the environment
>
> would be expanded to
>
> file /sbin/kinit /some/directory/usr/src/klibc/kinit/kinit 0755 0 0
>
> If several environment variables appear in a line, they are all expanded with processing happening from left to right. Undefined variables expand to a null string. Syntax errors stop processing, letting the existing error handling show the user offending line.
>
> This patch helps embedded folks who frequently create several RFS directories and then switch between them as they're tuning an initramfs. Having just been such an embedded folk, this seems the most efficient solution.
>
> I didn't see a maintainer for this area (forgive me if I overlooked) so I don't know who I should CC for this patch.
>
> Regards,
> gene
Hi gene.
Applied.
I fixed up the changelog (adding newlines and moving
your Signed-off-by:).
And I had to apply the patch manually as it was somehow broken.
Please see if you can improve this nect time you submit a patch.
Sam
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2008-11-22 11:36 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-10-29 13:54 [PATCH 2.6.28-rc2] gen_init_cpio expands shell variables in file names Sally, Gene
2008-11-22 11:35 ` Sam Ravnborg
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®