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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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) {
93 /* Check readdir didn't fail */
95 reply_with_perror ("readdir: /sys/block");
96 free_stringslen (r, size);
101 /* Close the directory handle */
102 if (closedir (dir) == -1) {
103 reply_with_perror ("closedir: /sys/block");
104 free_stringslen (r, size);
108 /* Sort the tags. Note that r might be NULL if there are no tags. */
110 sort_strings (r, size);
112 /* NULL terminate the list */
113 if (add_string (&r, &size, &alloc, NULL) == -1)
119 /* Read whole file into dynamically allocated array. If there is an
120 * error, DON'T call reply_with_perror, just return NULL. Returns a
121 * \0-terminated string.
124 read_whole_file (const char *filename)
127 size_t alloc = 0, size = 0;
130 fd = open (filename, O_RDONLY);
138 char *r2 = realloc (r, alloc);
146 /* The '- 1' in the size calculation ensures there is space below
147 * to add \0 to the end of the input.
149 ssize_t n = read (fd, r + size, alloc - size - 1);
160 if (close (fd) == -1) {
171 /* Takes optional arguments, consult optargs_bitmask. */
173 do_mount_9p (const char *mount_tag, const char *mountpoint, const char *options)
175 char *mp = NULL, *opts = NULL, *err = NULL;
179 ABS_PATH (mountpoint, , return -1);
181 mp = sysroot_path (mountpoint);
183 reply_with_perror ("malloc");
187 /* Check the mountpoint exists and is a directory. */
188 if (stat (mp, &statbuf) == -1) {
189 reply_with_perror ("%s", mountpoint);
192 if (!S_ISDIR (statbuf.st_mode)) {
193 reply_with_perror ("%s: mount point is not a directory", mountpoint);
197 /* Add trans=virtio to the options. */
198 if ((optargs_bitmask & GUESTFS_MOUNT_9P_OPTIONS_BITMASK) &&
199 STRNEQ (options, "")) {
200 if (asprintf (&opts, "trans=virtio,%s", options) == -1) {
201 reply_with_perror ("asprintf");
206 opts = strdup ("trans=virtio");
208 reply_with_perror ("strdup");
213 r = command (NULL, &err,
214 "mount", "-o", opts, "-t", "9p", mount_tag, mp, NULL);
216 reply_with_error ("%s on %s: %s", mount_tag, mountpoint, err);