From patchwork Fri Jun 11 10:06:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Babic X-Patchwork-Id: 1490881 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=googlegroups.com (client-ip=2a00:1450:4864:20::13b; helo=mail-lf1-x13b.google.com; envelope-from=swupdate+bncbd2zdgn6sekrbn7lrsdamgqekhbds2i@googlegroups.com; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.a=rsa-sha256 header.s=20161025 header.b=qly5UnWj; dkim-atps=neutral Received: from mail-lf1-x13b.google.com (mail-lf1-x13b.google.com [IPv6:2a00:1450:4864:20::13b]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4G1c2L73qBz9sRf for ; Fri, 11 Jun 2021 20:06:50 +1000 (AEST) Received: by mail-lf1-x13b.google.com with SMTP id s19-20020a0565123153b02902dcd045ec03sf1450139lfi.13 for ; Fri, 11 Jun 2021 03:06:50 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1623406007; cv=pass; d=google.com; s=arc-20160816; b=BVeZKGif3xlFZ+gaYbLiAu1I4ECRp7NEOG7NknpyF2/InLEbeMZVxrkgOyd5dFB8oY D7M2wsqsOQRBwMp8WgxsEX5iNcfEj+LdOk4Sw2J6u88/67ber8SDahm+HQhRU7Xpwlla MH1tZWSpUOZXoUNDYQqekgad9yIRwI+0p4+YGGGI0Mpb+iygJUmESZsDekbdRdN7gjJy yXkz6x6o9D9V2BFEh1xpQAPJybTb/AKlmjyeJOHapt3ZSzfmWCZ3cxVzVOQFxWAUUq/2 pCGgidqB6nJU+nbDvNaaHx58OskKSaqoPshCgpsJq6JOmaeZQ/tV12XGpukMGckRfcfy EwXg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:mime-version:message-id:date :subject:cc:to:from:sender:dkim-signature; bh=tx8KK/98fRntzb4wqLT3OOfywI1HjLtUHOji+tSxT38=; b=QhoRyqEmZ+6WwCm2iFirMWdfyJzwZGp46rYcjMCvnKi1SNFdySyEcalfZxOPpGZAHm mV+HQJ9yfNsrxTVrjQpKflGwpZ5Zh8BkK/2oOignv7DMnOckrmFSnXOuulDbc9RD00L0 c8SsawVsqCLaMTUBYc+tNLFsK/ni9hgERJlswEIXEE+TSFY63G/rTsWIlqFYjAL9O6YC Z4qwlSUUFYvg+V3EeJS1dctOuXy5FJi+zFMopvQBvTPzIdImEQeGfNyfZZMP0lr4vngx lUtyZJ+1Z2okcqh24wEMzNA8KJCvlauEbCowAPdPrpTrqzomBRYpPHj1EE/eloMF97ZC G+fA== ARC-Authentication-Results: i=2; gmr-mx.google.com; spf=neutral (google.com: 212.18.0.9 is neither permitted nor denied by best guess record for domain of stefano.babic@babic.homelinux.org) smtp.mailfrom=stefano.babic@babic.homelinux.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:mime-version :x-original-sender:x-original-authentication-results:precedence :mailing-list:list-id:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=tx8KK/98fRntzb4wqLT3OOfywI1HjLtUHOji+tSxT38=; b=qly5UnWjAjjNnHSS1FOuKQxZp11R9Z+ZMiyqG+KT91h9pBwq2SPWQOF0UztR3+zngB UqUcO4R7NmPN3o2pTabqYjlLp0CyKdjHQCds2UsXyDh6SevLbSXq9fZK1tIE4Owg3DQL r6OdmOmH4eFxSt+za2oA5pi1Ir1zCotfB1/K6gJIaXngzi1k0KQmOBuGS2ZO09aJFSEP TJ84dGoy1fSSTxMwQuRz8zwzjQkMOh1zXDpO02l7DUQawU5wfrltx2kr55/muVQpDGuN 1aNxz2fwAaa8dczv66bdTOXRygXHizD5S/fku/zHq1Low15TckjeaOw+iFjco00CG00L WUcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=sender:x-gm-message-state:from:to:cc:subject:date:message-id :mime-version:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:x-spam-checked-in-group:list-post :list-help:list-archive:list-subscribe:list-unsubscribe; bh=tx8KK/98fRntzb4wqLT3OOfywI1HjLtUHOji+tSxT38=; b=ZR9vkcEcskJQKgsY7EK77Wa01a/kuYwXL2B789NDi84y+vtpD7TPmhZ7DSHzllZC/c OZ8h5ahE5SSHR5diqUEy0hERtBiLN5mcvUfXwifcNDYvBBZe5+KulRSJM5BGJC7+4RJe +4zDvM4+joEQ1NaoGxLYkRROFysWBsQqexj4jJF5ImXzVJH+IYKTLQZSqJDAOSHfW/EO q1K/F4AaiIH3QHuRLzPZNdT/H1fMe1Ao+Exh/soCMwODBcv3sCxQ/lrdTi2q0touK1FS g4/ey2/v/vbPiR8Cgw6hrTfLrXGYx9mGCXGB9baATYds+K/OADA4ZAVRA5U7+qIrDnSa PSiQ== Sender: swupdate@googlegroups.com X-Gm-Message-State: AOAM5311mlH5fWjkTjCez77JXvPrIy1FheFO7dGUyRjJ06BXN7W9zVsN ro/Er/XxtWhMVEKAk3DBTSk= X-Google-Smtp-Source: ABdhPJyxmj960cQnpbVnj9iPAZdeXaX/DbdaGkzyKf6ERYAgQozFUb5nT9C5rNKS7EDqxuW+JNNvOg== X-Received: by 2002:a2e:a4ca:: with SMTP id p10mr2375356ljm.271.1623406007373; Fri, 11 Jun 2021 03:06:47 -0700 (PDT) X-BeenThere: swupdate@googlegroups.com Received: by 2002:a05:6512:3f94:: with SMTP id x20ls495481lfa.0.gmail; Fri, 11 Jun 2021 03:06:46 -0700 (PDT) X-Received: by 2002:a05:6512:20c7:: with SMTP id u7mr2123496lfr.397.1623406006361; Fri, 11 Jun 2021 03:06:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623406006; cv=none; d=google.com; s=arc-20160816; b=VONId1IQRBjFZyZXRumx820jWbOJHKwFM4NvZQRd5th6KJ59DJ3tkFsWRUM6dgWF3d pMvRYk9bRTdONjl/xnWLkZXEjrqYiTPjWyV89eXm4mVfVNiC3Kia3d8D9A4hf2XNOddl qK6PO1TytRrbzujUdEaWrkfTPKgR7bKnXf6MB38gay3gxM3tgGVMswn3f3YSnF0EVgLP 9GkmHqOy0zzcEu5l5xpMqU3+oBXQuo1E/IEkMVE0D+dI+w+jXj+q1EHLt/MuN9vWRbIB Y9OznD8hu8grF2pAQU5oDWGZRMT636Eumuq1WrXHzrf6z3Ta+ugLAjnZ2emYa3qVqvFj zCWw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from; bh=/t0aJ5ZJhxcApcfBtGnLNWMckLJTru3vpm8waLrLxog=; b=uAxn8sTooCHF1Shp3NuFh3S2ZgSaLCRygruAhIpY65gS2zzFhEmuQHlZySrFFgy9n0 IaVlE0uLY6+oDK6kdVSYINq4JATmj8Dbcq9TPK1Yib0OtsiOgybw4KNoYPaFPPmdKOX3 xAhf0qA3HLu5Eqh0e5hnCtoiGB6iYyjGqvGrWmcGVJn7S9Iy8H3uWQIfh4OmtnXX9Wee J+bwlLMMplmcaoNnnYMKquAHN298fvUVkeoczB/JGJeNJIVc5AHpSlwAl1Wh3mnPKW7L 4UwC/jW6VROIofUFVfZ5dZA82NLwa2nH2ypk0FQaaX8Mzu5FiHoX65iZzp1jll7xig0y zB9g== ARC-Authentication-Results: i=1; gmr-mx.google.com; spf=neutral (google.com: 212.18.0.9 is neither permitted nor denied by best guess record for domain of stefano.babic@babic.homelinux.org) smtp.mailfrom=stefano.babic@babic.homelinux.org Received: from mail-out.m-online.net (mail-out.m-online.net. [212.18.0.9]) by gmr-mx.google.com with ESMTPS id w3si241514ljg.8.2021.06.11.03.06.46 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 11 Jun 2021 03:06:46 -0700 (PDT) Received-SPF: neutral (google.com: 212.18.0.9 is neither permitted nor denied by best guess record for domain of stefano.babic@babic.homelinux.org) client-ip=212.18.0.9; Received: from frontend01.mail.m-online.net (unknown [192.168.8.182]) by mail-out.m-online.net (Postfix) with ESMTP id 4G1c2F4Ggdz1qv9c; Fri, 11 Jun 2021 12:06:45 +0200 (CEST) Received: from localhost (dynscan1.mnet-online.de [192.168.6.70]) by mail.m-online.net (Postfix) with ESMTP id 4G1c2F3xMQz1qql9; Fri, 11 Jun 2021 12:06:45 +0200 (CEST) X-Virus-Scanned: amavisd-new at mnet-online.de Received: from mail.mnet-online.de ([192.168.8.182]) by localhost (dynscan1.mail.m-online.net [192.168.6.70]) (amavisd-new, port 10024) with ESMTP id lYBsHdBJ1Qt9; Fri, 11 Jun 2021 12:06:44 +0200 (CEST) Received: from babic.homelinux.org (host-88-217-136-221.customer.m-online.net [88.217.136.221]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.mnet-online.de (Postfix) with ESMTPS; Fri, 11 Jun 2021 12:06:44 +0200 (CEST) Received: from localhost (mail.babic.homelinux.org [127.0.0.1]) by babic.homelinux.org (Postfix) with ESMTP id 4EF004541495; Fri, 11 Jun 2021 12:06:43 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at babic.homelinux.org Received: from babic.homelinux.org ([127.0.0.1]) by localhost (mail.babic.homelinux.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id rR6qKbxZXzbn; Fri, 11 Jun 2021 12:06:39 +0200 (CEST) Received: from paperino.fritz.box (paperino.fritz.box [192.168.178.48]) by babic.homelinux.org (Postfix) with ESMTP id F1C234540E35; Fri, 11 Jun 2021 12:06:38 +0200 (CEST) From: stefano.babic@babic.homelinux.org To: swupdate@googlegroups.com Cc: Stefano Babic Subject: [swupdate] [PATCH] util: Extend get_root to find LUKS devices Date: Fri, 11 Jun 2021 12:06:37 +0200 Message-Id: <20210611100637.54475-1-stefano.babic@babic.homelinux.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Original-Sender: stefano.babic@babic.homelinux.org X-Original-Authentication-Results: gmr-mx.google.com; spf=neutral (google.com: 212.18.0.9 is neither permitted nor denied by best guess record for domain of stefano.babic@babic.homelinux.org) smtp.mailfrom=stefano.babic@babic.homelinux.org Precedence: list Mailing-list: list swupdate@googlegroups.com; contact swupdate+owners@googlegroups.com List-ID: X-Spam-Checked-In-Group: swupdate@googlegroups.com X-Google-Group-Id: 605343134186 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , From: Stefano Babic This helps in case of encrypted filesystem or device mapper. The returned device read from partitions is usually a dm-X device and this does not show which is the block device that contains it. Look in sysfs and check if the device has "slaves" entries, indicating the presence of an underlying device. If found, return this instead of the device returned parsing /proc/partitions. Signed-off-by: Stefano Babic --- core/util.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/core/util.c b/core/util.c index 51a16b6..f4cbbdf 100644 --- a/core/util.c +++ b/core/util.c @@ -24,6 +24,7 @@ #include #include #include +#include #if defined(__linux__) #include @@ -851,6 +852,10 @@ size_t snescape(char *dst, size_t n, const char *src) /* * This returns the device name where rootfs is mounted */ + +static int filter_slave(const struct dirent *ent) { + return (strcmp(ent->d_name, ".") && strcmp(ent->d_name, "..")); +} static char *get_root_from_partitions(void) { struct stat info; @@ -858,11 +863,28 @@ static char *get_root_from_partitions(void) char *devname = NULL; unsigned long major, minor, nblocks; char buf[256]; - int ret; + int ret, dev_major, dev_minor, n; + struct dirent **devlist; if (stat("/", &info) < 0) return NULL; + dev_major = info.st_dev / 256; + dev_minor = info.st_dev % 256; + + /* + * Check if this is just a container, for example in case of LUKS + * Search if the device has slaves pointing to another device + */ + snprintf(buf, sizeof(buf) - 1, "/sys/dev/block/%d:%d/slaves", dev_major, dev_minor); + n = scandir(buf, &devlist, filter_slave, NULL); + if (n == 1) { + devname = strdup(devlist[0]->d_name); + free(devlist); + return devname; + } + free(devlist); + fp = fopen("/proc/partitions", "r"); if (!fp) return NULL;