-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpull:lxc.c
81 lines (67 loc) · 2.31 KB
/
pull:lxc.c
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
// Import an image from https://images.linuxcontainers.org/
// if --dry is set, the image url is printed but not imported
#define USAGE "usage: plash import-lxc DISTRIBUTION:RELEASE [--dry]\n"
#define HOME_URL "https://images.linuxcontainers.org"
#define INDEX_URL "https://images.linuxcontainers.org/meta/1.0/index-user"
#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/utsname.h>
#include <unistd.h>
#include <plash.h>
char *getarch() {
struct utsname unameData;
uname(&unameData);
if ((strcmp(unameData.machine, "x86_64")) == 0) {
return "amd64";
} else if ((strcmp(unameData.machine, "aarch64")) == 0) {
return "arm64";
}
char *dup = strdup(unameData.machine);
if (dup == NULL)
pl_fatal("strdup");
return dup;
}
int pull_lxc_main(int argc, char *argv[]) {
char *prefix, *url_part, *rootfs_url, *resp, *image_name, *distro, *version,
*line;
// read args
if (argc < 2) {
fputs(USAGE, stderr);
return EXIT_FAILURE;
}
image_name = argv[1];
int dry = argv[2] && strcmp(argv[2], "--dry") == 0;
// split the first arg
distro = strtok(image_name, ":");
version = strtok(NULL, ":");
if (version == NULL)
pl_fatal("also specify the release, example: debian:sid");
resp = pl_check_output((char *[]){"curl", "--progress-bar", "--fail",
"--location", INDEX_URL, NULL});
asprintf(&prefix, "%s;%s;%s;default;", distro, version, getarch()) != -1 ||
pl_fatal("asprintf");
line = strtok(resp, "\n");
while (line != NULL) {
if (strncmp(line, prefix, strlen(prefix)) == 0) {
url_part = line + strlen(prefix); // rewind away from our found prefix
url_part += strcspn(url_part, ";"); // go to the next ";"
url_part += 1; // chop that last ";"
asprintf(&rootfs_url, HOME_URL "%srootfs.tar.xz", url_part) != -1 ||
pl_fatal("asprintf");
// we got the root file system process with it how the user requested
if (dry) {
puts(rootfs_url);
return EXIT_SUCCESS;
} else {
execlp("/proc/self/exe", "plash", "pull:url", rootfs_url, NULL);
pl_fatal("execlp");
}
}
line = strtok(NULL, "\n");
}
errno = 0;
pl_fatal("%s:%s not listed in " HOME_URL "/", distro, version);
}