Skip to content

Commit 4c5854f

Browse files
committed
test-data: phony fedora: Add simple static /bin/sh
This is able to do enough to run commands via g#sh and related calls inside the phony Fedora image.
1 parent 48749b7 commit 4c5854f

File tree

3 files changed

+76
-7
lines changed

3 files changed

+76
-7
lines changed

cat/test-virt-ls.sh

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ skip_if_skipped
2525
# Read out the test directory using virt-ls.
2626
if [ "$($VG virt-ls --format=raw -a ../test-data/phony-guests/fedora.img /bin)" != "ls
2727
rpm
28+
sh
2829
test1
2930
test2
3031
test3

test-data/phony-guests/fedora.c

+71-5
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717
*/
1818

19-
/* This is "just enough" of a binary to look like RPM and dracut, as
20-
* far as virt-v2v is concerned.
19+
/* This is "just enough" of a binary to look like /bin/sh, RPM and
20+
* dracut, as far as virt-v2v is concerned.
2121
*/
2222

2323
#include <stdio.h>
2424
#include <stdlib.h>
2525
#include <string.h>
26+
#include <unistd.h>
2627

2728
/* NB: This is also defined in make-fedora-img.pl */
2829
#define KVER "5.19.0-0.rc1.14.fc37.x86_64"
@@ -34,6 +35,27 @@ get_basename (const char *str)
3435
return ret == NULL ? str : ret + 1;
3536
}
3637

38+
static void
39+
add_str (char ***argv, size_t *argc, char *str)
40+
{
41+
(*argc)++;
42+
(*argv) = realloc (*argv, *argc * sizeof (char *));
43+
(*argv)[*argc-1] = str;
44+
}
45+
46+
static void
47+
add_null (char ***argv, size_t *argc)
48+
{
49+
add_str (argv, argc, NULL);
50+
}
51+
52+
static void
53+
add (char ***argv, size_t *argc, const char *s, size_t len)
54+
{
55+
char *copy = strndup (s, len);
56+
add_str (argv, argc, copy);
57+
}
58+
3759
int
3860
main (int argc, char *argv[])
3961
{
@@ -57,10 +79,54 @@ main (int argc, char *argv[])
5779
strcmp (get_basename (argv[0]), "dracut") == 0) {
5880
// do nothing, pretend to rebuild the initramfs
5981
}
82+
else if (argc == 3 &&
83+
strcmp (get_basename (argv[0]), "sh") == 0 &&
84+
strcmp (argv[1], "-c") == 0) {
85+
/* Split the command and execute it. Only handles trivial cases. */
86+
char *cmd = argv[2];
87+
char **cmdv = NULL;
88+
size_t i, cmdvlen = 0, n;
89+
const size_t len = strlen (cmd);
90+
91+
for (i = 0; i < len;) {
92+
switch (cmd[i]) {
93+
case ' ': case '\t':
94+
i++;
95+
continue;
96+
97+
case '"':
98+
n = strcspn (&cmd[i+1], "\"");
99+
add (&cmdv, &cmdvlen, &cmd[i+1], n);
100+
i += n+2;
101+
break;
102+
103+
case '\'':
104+
n = strcspn (&cmd[i+1], "'");
105+
add (&cmdv, &cmdvlen, &cmd[i+1], n);
106+
i += n+2;
107+
break;
108+
109+
default:
110+
n = strcspn (&cmd[i], " \t");
111+
add (&cmdv, &cmdvlen, &cmd[i], n);
112+
i += n;
113+
}
114+
}
115+
add_null (&cmdv, &cmdvlen);
116+
117+
execvp (cmdv[0], cmdv);
118+
perror (cmdv[0]);
119+
exit (EXIT_FAILURE);
120+
}
60121
else {
61-
fprintf (stderr, "phony Fedora: unknown command\n");
62-
exit (1);
122+
int i;
123+
124+
fprintf (stderr, "ERROR: test-data/phony-guests/fedora.c: "
125+
"unexpected command:\n");
126+
for (i = 0; i < argc; ++i)
127+
fprintf (stderr, "argv[%d] = %s\n", i, argv[i]);
128+
exit (EXIT_FAILURE);
63129
}
64130

65-
exit (0);
131+
exit (EXIT_SUCCESS);
66132
}

test-data/phony-guests/make-fedora-img.pl

+4-2
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,10 @@ sub init_lvm_root {
369369
$g->mkfifo (0777, '/bin/test6');
370370
$g->mknod (0777, 10, 10, '/bin/test7');
371371

372-
# Virt-v2v needs an RPM command, or at least something which acts
373-
# similarly, and also a dracut command.
372+
# Virt-v2v needs a /bin/sh, an RPM command and a dracut command, or at
373+
# least something which acts similarly to those.
374+
$g->upload ('fedora-static-bin', '/bin/sh');
375+
$g->chmod (0777, '/bin/sh');
374376
$g->upload ('fedora-static-bin', '/bin/rpm');
375377
$g->chmod (0777, '/bin/rpm');
376378
$g->upload ('fedora-static-bin', '/sbin/dracut');

0 commit comments

Comments
 (0)