1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2011 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <sys/types.h>
35 #define BUS_PATH "/sys/bus/virtio/drivers/9pnet_virtio"
37 static char *read_whole_file (const char *filename);
39 /* https://bugzilla.redhat.com/show_bug.cgi?id=714981#c1 */
44 int size = 0, alloc = 0;
49 dir = opendir (BUS_PATH);
51 perror ("opendir: " BUS_PATH);
55 /* If this directory doesn't exist, it probably means that
56 * the virtio driver isn't loaded. Don't return an error
57 * in this case, but return an empty list.
59 if (add_string (&r, &size, &alloc, NULL) == -1)
67 struct dirent *d = readdir (dir);
70 if (STRPREFIX (d->d_name, "virtio")) {
71 char mount_tag_path[256];
72 snprintf (mount_tag_path, sizeof mount_tag_path,
73 BUS_PATH "/%s/mount_tag", d->d_name);
75 /* A bit unclear, but it looks like the virtio transport allows
76 * the mount tag length to be unlimited (or up to 65536 bytes).
77 * See: linux/include/linux/virtio_9p.h
79 char *mount_tag = read_whole_file (mount_tag_path);
83 if (add_string (&r, &size, &alloc, mount_tag) == -1) {
85 free_stringslen (r, size);
94 /* Check readdir didn't fail */
96 reply_with_perror ("readdir: /sys/block");
97 free_stringslen (r, size);
102 /* Close the directory handle */
103 if (closedir (dir) == -1) {
104 reply_with_perror ("closedir: /sys/block");
105 free_stringslen (r, size);
109 /* Sort the tags. Note that r might be NULL if there are no tags. */
111 sort_strings (r, size);
113 /* NULL terminate the list */
114 if (add_string (&r, &size, &alloc, NULL) == -1)
120 /* Read whole file into dynamically allocated array. If there is an
121 * error, DON'T call reply_with_perror, just return NULL. Returns a
122 * \0-terminated string.
125 read_whole_file (const char *filename)
128 size_t alloc = 0, size = 0;
131 fd = open (filename, O_RDONLY);
139 char *r2 = realloc (r, alloc);
147 /* The '- 1' in the size calculation ensures there is space below
148 * to add \0 to the end of the input.
150 ssize_t n = read (fd, r + size, alloc - size - 1);
161 if (close (fd) == -1) {
172 /* Takes optional arguments, consult optargs_bitmask. */
174 do_mount_9p (const char *mount_tag, const char *mountpoint, const char *options)
176 char *mp = NULL, *opts = NULL, *err = NULL;
180 ABS_PATH (mountpoint, , return -1);
182 mp = sysroot_path (mountpoint);
184 reply_with_perror ("malloc");
188 /* Check the mountpoint exists and is a directory. */
189 if (stat (mp, &statbuf) == -1) {
190 reply_with_perror ("%s", mountpoint);
193 if (!S_ISDIR (statbuf.st_mode)) {
194 reply_with_perror ("%s: mount point is not a directory", mountpoint);
198 /* Add trans=virtio to the options. */
199 if ((optargs_bitmask & GUESTFS_MOUNT_9P_OPTIONS_BITMASK) &&
200 STRNEQ (options, "")) {
201 if (asprintf (&opts, "trans=virtio,%s", options) == -1) {
202 reply_with_perror ("asprintf");
207 opts = strdup ("trans=virtio");
209 reply_with_perror ("strdup");
214 r = command (NULL, &err,
215 "mount", "-o", opts, "-t", "9p", mount_tag, mp, NULL);
217 reply_with_error ("%s on %s: %s", mount_tag, mountpoint, err);