Received: from list by lists.gnu.org with archive (Exim 4.71) id 1cTtOf-0004uq-Ny for mharc-grub-devel@gnu.org; Wed, 18 Jan 2017 11:47:41 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49606) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cTtOd-0004sq-JI for grub-devel@gnu.org; Wed, 18 Jan 2017 11:47:40 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cTtOZ-0003gv-LE for grub-devel@gnu.org; Wed, 18 Jan 2017 11:47:39 -0500 Received: from mail-lf0-x243.google.com ([2a00:1450:4010:c07::243]:33087) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cTtOZ-0003fc-9z for grub-devel@gnu.org; Wed, 18 Jan 2017 11:47:35 -0500 Received: by mail-lf0-x243.google.com with SMTP id x1so2544024lff.0 for ; Wed, 18 Jan 2017 08:47:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=HIBZexgtgJQhY/Kvg+Qwy2msj8u85WJWfcC/eTAkETA=; b=dpW/YRER8ei0KOqCe+aZDibDU0N6sHePEXN4aAuuj1HsombDS8dLDUM7qz0c4o0tnv qYJva2eOhj6/i/R0jl3HnZeJjRbEAGx6TN5CAukVHhPRgh4A/fiWUx5ju8acLA41iPma iS6Ui39hi3lXD9MjCHVOHL49t9/bEV4Vnui2dsSYJ5AgN/t1qKw5QYkd5xw0ds9kgwOo z4DwmJuZCl0Q/Zf1Zsi5QCUChWyv52/fL/If4gSTIFOzxBhl3ZUDSm7WDp/B/swpS8Ce VyhOBUfuF5n+yAls0Rs9t7I+hJrZNCEI1GBJBO+Ch8LYiYSREblLOkRrlEuAXwV8ljms X1dA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=HIBZexgtgJQhY/Kvg+Qwy2msj8u85WJWfcC/eTAkETA=; b=JNCpyB3AkDsB53pgcE1q3wF23TzZzDtqGhwE6LMIN9xoi+jYFG5dRAV0G8SpxMHU4z bz0fGJAdSX4g02nMTtJNMEuPQYF4yHm+rak09f9pwC49W3LMxEUdkiODgZ3kM8xWQ41N FRAJdDFbCgwIbwznpz38iE65cfYjGsWnrNkSN57deSlK4DptdSml8g+oHox+BFtunJBs Jks3jk0rf4cMNTHJouiNhGRvTKO55NLsK8oRCO950jF+A7aZR31sOjzEqOC6Phy44IAB lrYsXqSy7t4Uxb0d50DE6HbscMuM87T2ZtDSfHrv/65UEoN7ofeUD0k7bqIKCOautmDh XxCw== X-Gm-Message-State: AIkVDXKN5O8jEqrT5lSbuBc237QBlRJa6dTcrvQbk1i9bHbNMAhR2yabhduFcamfTfHShg== X-Received: by 10.25.206.145 with SMTP id e139mr1752299lfg.168.1484758052453; Wed, 18 Jan 2017 08:47:32 -0800 (PST) Received: from localhost.localdomain (ppp109-252-90-129.pppoe.spdop.ru. [109.252.90.129]) by smtp.gmail.com with ESMTPSA id r192sm506687lfe.33.2017.01.18.08.47.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 18 Jan 2017 08:47:31 -0800 (PST) From: Andrei Borzenkov To: grub-devel@gnu.org Cc: felix.von.s@posteo.de Subject: [PATCH] osdep/linux: handle autofs entries in /proc/self/mountinfo Date: Wed, 18 Jan 2017 19:47:24 +0300 Message-Id: <1484758044-6258-1-git-send-email-arvidjaar@gmail.com> X-Mailer: git-send-email 2.7.4 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:4010:c07::243 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 18 Jan 2017 16:47:41 -0000 These entries have placeholder for device name and so are useless for our purpose. grub failed with something like grub-install: error: failed to get canonical path of `systemd-1'. When we see autofs entry, record it (to keep parent-child relationship) but continue to look for real mount. If it is found, we process it as usual. If only autofs entry exists, attempt to trigger mount by opening mount point and retry. Mount point itself is then kept open to avoid timeout. Recent systemd is by default using automount for /boot/efi so this should become more popular problem on EFI systems. Closes: 49942 --- @felix, could you give it a try? Works for me. Note that the problem happens only when probing for exact mount point. Otherwise realpath() triggers mounting before. It is theoretically possible that this will timeout before we scan mountinfo, although it is probably unlikely. grub-core/osdep/linux/getroot.c | 51 ++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/grub-core/osdep/linux/getroot.c b/grub-core/osdep/linux/getroot.c index 09e7e6e..d630cf3 100644 --- a/grub-core/osdep/linux/getroot.c +++ b/grub-core/osdep/linux/getroot.c @@ -383,21 +383,30 @@ grub_find_root_devices_from_mountinfo (const char *dir, char **relroot) FILE *fp; char *buf = NULL; size_t len = 0; - grub_size_t entry_len = 0, entry_max = 4; + grub_size_t entry_len, entry_max = 4; struct mountinfo_entry *entries; struct mountinfo_entry parent_entry = { 0, 0, 0, "", "", "", "" }; int i; + int retry = 0; + int dir_fd = -1; + char **ret = NULL; if (! *dir) dir = "/"; if (relroot) *relroot = NULL; + entries = xmalloc (entry_max * sizeof (*entries)); + +again: fp = grub_util_fopen ("/proc/self/mountinfo", "r"); if (! fp) - return NULL; /* fall through to other methods */ + { + free (entries); + return NULL; /* fall through to other methods */ + } - entries = xmalloc (entry_max * sizeof (*entries)); + entry_len = 0; /* First, build a list of relevant visible mounts. */ while (getline (&buf, &len, fp) > 0) @@ -458,6 +467,9 @@ grub_find_root_devices_from_mountinfo (const char *dir, char **relroot) } else { + /* If this is automounted directory, continue to search for real device */ + int autofs = (grub_strcmp (entry.fstype, "autofs") == 0); + for (i = entry_len - 1; i >= 0; i--) { if (entries[i].id == parent_entry.id) @@ -465,7 +477,8 @@ grub_find_root_devices_from_mountinfo (const char *dir, char **relroot) /* Insert at end, pruning anything previously above this. */ entry_len = i + 2; entries[i + 1] = entry; - break; + if (!autofs) + break; } else if (i == 0 && entries[i].id == entry.id) { @@ -475,7 +488,8 @@ grub_find_root_devices_from_mountinfo (const char *dir, char **relroot) (entry_len - 1) * sizeof (*entries)); entries[0] = parent_entry; entries[1] = entry; - break; + if (!autofs) + break; } } } @@ -484,7 +498,6 @@ grub_find_root_devices_from_mountinfo (const char *dir, char **relroot) /* Now scan visible mounts for the ones we're interested in. */ for (i = entry_len - 1; i >= 0; i--) { - char **ret = NULL; char *fs_prefix = NULL; if (!*entries[i].device) continue; @@ -515,6 +528,23 @@ grub_find_root_devices_from_mountinfo (const char *dir, char **relroot) ret = grub_find_root_devices_from_btrfs (dir); fs_prefix = get_btrfs_fs_prefix (entries[i].enc_path); } + else if (!retry && grub_strcmp (entries[i].fstype, "autofs") == 0) + { + /* If the best match is automounted, try to trigger mount. We cannot + simply return here because stat() on automounted directory does not + trigger mount and returns bogus (pseudo)device number instead. + We keep mountpoint open until end of scan to prevent timeout. */ + + int flags = O_RDONLY|O_DIRECTORY; + + fclose (fp); +#ifdef O_LARGEFILE + flags |= O_LARGEFILE; +#endif + dir_fd = open (entries[i].enc_path, flags); + retry = 1; + goto again; + } if (!ret) { ret = xmalloc (2 * sizeof (ret[0])); @@ -544,16 +574,15 @@ grub_find_root_devices_from_mountinfo (const char *dir, char **relroot) } if (fs_prefix != entries[i].enc_root) free (fs_prefix); - free (buf); - free (entries); - fclose (fp); - return ret; + break; } free (buf); free (entries); fclose (fp); - return NULL; + if (dir_fd != -1) + close (dir_fd); + return ret; } static char * -- tg: (972765f..) bug/49942 (depends on: master)