From patchwork Wed Dec 11 06:43:10 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Marzinski X-Patchwork-Id: 3322631 X-Patchwork-Delegate: christophe.varoqui@free.fr Return-Path: X-Original-To: patchwork-dm-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 5EBBE9F37C for ; Wed, 11 Dec 2013 06:47:23 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 4FFB220396 for ; Wed, 11 Dec 2013 06:47:22 +0000 (UTC) Received: from mx3-phx2.redhat.com (mx3-phx2.redhat.com [209.132.183.24]) by mail.kernel.org (Postfix) with ESMTP id 403E52074E for ; Wed, 11 Dec 2013 06:47:21 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx3-phx2.redhat.com (8.13.8/8.13.8) with ESMTP id rBB6i3i3018639; Wed, 11 Dec 2013 01:44:03 -0500 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id rBB6hm7t021241 for ; Wed, 11 Dec 2013 01:43:48 -0500 Received: from dhcp80-209.msp.redhat.com (octiron.msp.redhat.com [10.15.80.209]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id rBB6hlba025689 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 11 Dec 2013 01:43:48 -0500 Received: from dhcp80-209.msp.redhat.com (localhost [127.0.0.1]) by dhcp80-209.msp.redhat.com (8.14.7/8.14.7) with ESMTP id rBB6hkfi001403; Wed, 11 Dec 2013 00:43:47 -0600 Received: (from bmarzins@localhost) by dhcp80-209.msp.redhat.com (8.14.7/8.14.7/Submit) id rBB6hklB001399; Wed, 11 Dec 2013 00:43:46 -0600 From: Benjamin Marzinski To: device-mapper development Date: Wed, 11 Dec 2013 00:43:10 -0600 Message-Id: <1386744190-1295-17-git-send-email-bmarzins@redhat.com> In-Reply-To: <1386744190-1295-1-git-send-email-bmarzins@redhat.com> References: <1386744190-1295-1-git-send-email-bmarzins@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-loop: dm-devel@redhat.com Cc: Christophe Varoqui Subject: [dm-devel] [PATCH 16/16] kpartx: autoload loop module on loop partition creation X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk Reply-To: device-mapper development List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: dm-devel-bounces@redhat.com Errors-To: dm-devel-bounces@redhat.com X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Currently kpartx doesn't do anything to force the loop module to autoload, so creating partitions over files fails if the loop module isn't already loaded. This patch makes kpartx try to find the next available loop device by ioctling /dev/loop-control, which will autoload kpartx. Signed-off-by: Benjamin Marzinski --- kpartx/lopart.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/kpartx/lopart.c b/kpartx/lopart.c index 9082ca8..6f83048 100644 --- a/kpartx/lopart.c +++ b/kpartx/lopart.c @@ -32,6 +32,10 @@ #include "lopart.h" #include "xstrncpy.h" +#ifndef LOOP_CTL_GET_FREE +#define LOOP_CTL_GET_FREE 0x4C82 +#endif + #if !defined (__alpha__) && !defined (__ia64__) && !defined (__x86_64__) \ && !defined (__s390x__) #define int2ptr(x) ((void *) ((int) x)) @@ -140,14 +144,23 @@ find_unused_loop_device (void) char dev[20]; char *loop_formats[] = { "/dev/loop%d", "/dev/loop/%d" }; - int i, j, fd, somedev = 0, someloop = 0, loop_known = 0; + int i, j, fd, first = 0, somedev = 0, someloop = 0, loop_known = 0; struct stat statbuf; struct loop_info loopinfo; FILE *procdev; + if (stat("/dev/loop-control", &statbuf) == 0 && + S_ISCHR(statbuf.st_mode)) { + fd = open("/dev/loop-control", O_RDWR); + if (fd >= 0) + first = ioctl(fd, LOOP_CTL_GET_FREE); + close(fd); + if (first < 0) + first = 0; + } for (j = 0; j < SIZE(loop_formats); j++) { - for(i = 0; i < 256; i++) { + for(i = first; i < 256; i++) { sprintf(dev, loop_formats[j], i); if (stat (dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {