xref: /kvmtool/builtin-setup.c (revision f9ea40eaee4922eb8e309647a4e91517c552ffb9)
1 #include <kvm/util.h>
2 #include <kvm/kvm-cmd.h>
3 #include <kvm/builtin-setup.h>
4 #include <kvm/kvm.h>
5 #include <kvm/parse-options.h>
6 
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <limits.h>
10 #include <signal.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <sys/types.h>
16 #include <sys/mman.h>
17 #include <sys/stat.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <fcntl.h>
21 
22 static const char *instance_name;
23 
24 static const char * const setup_usage[] = {
25 	"kvm setup [name]",
26 	NULL
27 };
28 
29 static const struct option setup_options[] = {
30 	OPT_END()
31 };
32 
33 static void parse_setup_options(int argc, const char **argv)
34 {
35 	while (argc != 0) {
36 		argc = parse_options(argc, argv, setup_options, setup_usage,
37 				PARSE_OPT_STOP_AT_NON_OPTION);
38 		if (argc != 0 && instance_name)
39 			kvm_setup_help();
40 		else
41 			instance_name = argv[0];
42 		argv++;
43 		argc--;
44 	}
45 }
46 
47 void kvm_setup_help(void)
48 {
49 	printf("\nkvm setup creates a new rootfs under %s.\n"
50 		"This can be used later by the '-d' parameter of 'kvm run'.\n",
51 		kvm__get_dir());
52 	usage_with_options(setup_usage, setup_options);
53 }
54 
55 static int copy_file(const char *from, const char *to)
56 {
57 	int in_fd, out_fd;
58 	void *src, *dst;
59 	struct stat st;
60 	int err = -1;
61 
62 	in_fd = open(from, O_RDONLY);
63 	if (in_fd < 0)
64 		return err;
65 
66 	if (fstat(in_fd, &st) < 0)
67 		goto error_close_in;
68 
69 	out_fd = open(to, O_RDWR | O_CREAT | O_TRUNC, st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO));
70 	if (out_fd < 0)
71 		goto error_close_in;
72 
73 	src = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, in_fd, 0);
74 	if (src == MAP_FAILED)
75 		goto error_close_out;
76 
77 	if (ftruncate(out_fd, st.st_size) < 0)
78 		goto error_munmap_src;
79 
80 	dst = mmap(NULL, st.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, out_fd, 0);
81 	if (dst == MAP_FAILED)
82 		goto error_munmap_src;
83 
84 	memcpy(dst, src, st.st_size);
85 
86 	if (fsync(out_fd) < 0)
87 		goto error_munmap_dst;
88 
89 	err = 0;
90 
91 error_munmap_dst:
92 	munmap(dst, st.st_size);
93 error_munmap_src:
94 	munmap(src, st.st_size);
95 error_close_out:
96 	close(out_fd);
97 error_close_in:
98 	close(in_fd);
99 
100 	return err;
101 }
102 
103 static const char *guestfs_dirs[] = {
104 	"/dev",
105 	"/etc",
106 	"/home",
107 	"/host",
108 	"/proc",
109 	"/root",
110 	"/sys",
111 	"/tmp",
112 	"/var",
113 	"/var/lib",
114 	"/virt",
115 };
116 
117 static const char *guestfs_symlinks[] = {
118 	"/bin",
119 	"/lib",
120 	"/lib64",
121 	"/sbin",
122 	"/usr",
123 };
124 
125 static int copy_init(const char *guestfs_name)
126 {
127 	char path[PATH_MAX];
128 
129 	snprintf(path, PATH_MAX, "%s%s/virt/init", kvm__get_dir(), guestfs_name);
130 
131 	return copy_file("guest/init", path);
132 }
133 
134 static int make_guestfs_symlink(const char *guestfs_name, const char *path)
135 {
136 	char target[PATH_MAX];
137 	char name[PATH_MAX];
138 
139 	snprintf(name, PATH_MAX, "%s%s%s", kvm__get_dir(), guestfs_name, path);
140 
141 	snprintf(target, PATH_MAX, "/host%s", path);
142 
143 	return symlink(target, name);
144 }
145 
146 static void make_root_dir(void)
147 {
148 	char name[PATH_MAX];
149 
150 	snprintf(name, PATH_MAX, "%s", kvm__get_dir());
151 
152 	mkdir(name, 0777);
153 }
154 
155 static int make_dir(const char *dir)
156 {
157 	char name[PATH_MAX];
158 
159 	snprintf(name, PATH_MAX, "%s%s", kvm__get_dir(), dir);
160 
161 	return mkdir(name, 0777);
162 }
163 
164 static void make_guestfs_dir(const char *guestfs_name, const char *dir)
165 {
166 	char name[PATH_MAX];
167 
168 	snprintf(name, PATH_MAX, "%s%s", guestfs_name, dir);
169 
170 	make_dir(name);
171 }
172 
173 void kvm_setup_resolv(const char *guestfs_name)
174 {
175 	char path[PATH_MAX];
176 
177 	snprintf(path, PATH_MAX, "%s%s/etc/resolv.conf", kvm__get_dir(), guestfs_name);
178 
179 	copy_file("/etc/resolv.conf", path);
180 }
181 
182 static int do_setup(const char *guestfs_name)
183 {
184 	unsigned int i;
185 	int ret;
186 
187 	make_root_dir();
188 
189 	ret = make_dir(guestfs_name);
190 	if (ret < 0)
191 		return ret;
192 
193 	for (i = 0; i < ARRAY_SIZE(guestfs_dirs); i++)
194 		make_guestfs_dir(guestfs_name, guestfs_dirs[i]);
195 
196 	for (i = 0; i < ARRAY_SIZE(guestfs_symlinks); i++) {
197 		make_guestfs_symlink(guestfs_name, guestfs_symlinks[i]);
198 	}
199 
200 	return copy_init(guestfs_name);
201 }
202 
203 int kvm_setup_create_new(const char *guestfs_name)
204 {
205 	return do_setup(guestfs_name);
206 }
207 
208 int kvm_cmd_setup(int argc, const char **argv, const char *prefix)
209 {
210 	int r;
211 
212 	parse_setup_options(argc, argv);
213 
214 	if (instance_name == NULL)
215 		kvm_setup_help();
216 
217 	r = do_setup(instance_name);
218 	if (r == 0)
219 		printf("A new rootfs '%s' has been created in '%s%s'.\n\n"
220 			"You can now start it by running the following command:\n\n"
221 			"  kvm run -d %s\n",
222 			instance_name, kvm__get_dir(), instance_name, instance_name);
223 	else
224 		printf("Unable to create rootfs in %s%s: %s\n",
225 			kvm__get_dir(), instance_name, strerror(errno));
226 
227 	return r;
228 }
229