From patchwork Mon Jun 14 12:25:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Babic X-Patchwork-Id: 1491652 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::43a; helo=mail-wr1-x43a.google.com; envelope-from=swupdate+bncbd2zdgn6sekrbo4vtwdamgqe2hiouda@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=iSstkcQj; dkim-atps=neutral Received: from mail-wr1-x43a.google.com (mail-wr1-x43a.google.com [IPv6:2a00:1450:4864:20::43a]) (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 4G3Vz84Yrvz9sRN for ; Mon, 14 Jun 2021 22:25:39 +1000 (AEST) Received: by mail-wr1-x43a.google.com with SMTP id f9-20020a5d64c90000b029011a3c2a0337sf3738265wri.0 for ; Mon, 14 Jun 2021 05:25:39 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1623673532; cv=pass; d=google.com; s=arc-20160816; b=RckF05KyUxBhq2WUzIR3MLsKoHr+SetZvwwG2UwAd6vhV4bCmaSwoqKUQfreRrxkhJ 9o/i8zc4L+p2Q31rGbEwrQOKuOFQHazXIvN7Thi7E2W9e07qbyw18lvhM5wvxxB7LTfs pl41N8Dn0iLqfilSqCYWn4zSrrtkevlvCWXGeHNirpgO6fMZBiJLLtzLsseqo3m1W1H7 kh7nL+bA5mMhgR9EJ0PFhxOWprQ5bcgSNCStgmv+oN1EO0UJufAqvkn1UfFW5CZVljVX rz8uVoASAf6Wjy1dMnMgverutkZ/nai37frjN3rIud/msg63pyjYnU2wPg/SJt/Vr1rx Y05Q== 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=fnwR27iwZre4+GPx/xMDW6F6jbxZuSL1lNDre9+bQmY=; b=bClLVfMO1j+zaGuyd7WQMAr/vEq787+MWKoQwpP6Wtwa4/9u8cnah2zOJ1E7a97VBa Lgwe2REZiYRFR0fomDxJfZxiXjbEYlB4D2/qmSSiQu/BngMVm9zyGxOqXeuE6rW6qeFU Nebac2FBkiHEhsQ4liuJEP0egSUK0z4j1KKU3TXjRgMCECkvEsnJ3BwF/OVHilDkqWiL XQTdFekcFLK766VpE+u7VD41OQA1dayYMNxxU0wEkBe8TV/MEH5RJTNJ4zTUtxUTqNpb doMTBUplofUnIvxPm8Jg954r4ObSSdS5WO+JtGq3f7GS9ta1ovF7sG5L5MOeZ/b52NK+ B8jw== ARC-Authentication-Results: i=2; gmr-mx.google.com; spf=neutral (google.com: 212.18.0.10 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=fnwR27iwZre4+GPx/xMDW6F6jbxZuSL1lNDre9+bQmY=; b=iSstkcQjA46qz9GWaQNeYcwzYzG5UxAgGNTYvEYgIUiQC9yCJuplklPJzoNE1Ev9Yh TXoN5LcrSSnqYfmfpmwv95iG9k9twSQEXrniE95FtA0BYLTO2bpZiTA8DHWT8K81A2Hj UQrwbfClWiMlDQY3p/hPmbq4+n37TblZnEWOL0Pc9Pj96OW5xNZq4r95XOhg+d44Ci+K ZRMCV6egLOOPuw1t5Aec642icZTTeCdIU1KE9PfqdYtwoSV9YOhGP2NuKH71KS54kJ5Z fbWAcKKLa/rESWUbowh62a6cP8q09Cx1iC/+iM/Igb2PEsmP/QA+fsP2DqbSVHcSCJHS UJmg== 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=fnwR27iwZre4+GPx/xMDW6F6jbxZuSL1lNDre9+bQmY=; b=LDr1kKEk8pMoVQuktFVpgBmmcbsA6LEvehvPwIkB4DCKyqecQrDPK2l59SPixiN/Ca lIg7RB9OwW7K9a+lUnTc67VoaRn1Lm/e4VwsDoyj1rYGj0KH1QLgJEh6gG6kMUZKt2sU J4pJBJtNPh+LbHCmNG/YWOgiJP867hQcqNYe5rb9aqLjujX38l5F00FMBt6jkICuctZK w7rVMYc4z17dlqoqKbcvfq10DRYUI2OU+a+iWltIhG/T+gxVh4MUDby9DBF1EG4DhfYu ac/I7VoUBKVGLOF7PfuzqIB3vA9WjCpzz8VEvwmDAd9WcQ0LuXY2yOgWsvOSpFOvutB1 89Mw== Sender: swupdate@googlegroups.com X-Gm-Message-State: AOAM5313yu8D54jC9JUD/DHR8p96jkglwn+7D7y9ueNtxqhlVeZCu5Ze OFlErG86V3S9TmjzpDl+lHU= X-Google-Smtp-Source: ABdhPJy1ztK2Dh2QnfXY6wq6cjtF58nVH42m8+ixxRrzrAHVPTJHMMPa+zqtv/JbM/8EtBB/gDwk2A== X-Received: by 2002:adf:e943:: with SMTP id m3mr18190007wrn.384.1623673531991; Mon, 14 Jun 2021 05:25:31 -0700 (PDT) X-BeenThere: swupdate@googlegroups.com Received: by 2002:a05:600c:19d3:: with SMTP id u19ls7521738wmq.3.canary-gmail; Mon, 14 Jun 2021 05:25:31 -0700 (PDT) X-Received: by 2002:a05:600c:354f:: with SMTP id i15mr15743018wmq.38.1623673531231; Mon, 14 Jun 2021 05:25:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623673531; cv=none; d=google.com; s=arc-20160816; b=lGVTwljF2kLY0PU5rMrU/ELJAhilYJ5+q8iBecZ/E+d+TN1vqQ7qRm/5I4dEVnZuht rxdF3YLN970irog8oHq22AmJDptLqy5puDTZD+H2nFBwvZGA0Iv4GHiHoFL/Qk+NVLHd E8M2x250p8FJ92zZwiQdV1FuLvAzOJcpB9Vau0L4hFumBjhFys8YqqJI+Hkx+1xJHg3O MMl7AbMJuMRgXjUfdSruAnugjz2yyZJOTfaCZIhLk2ha28M171n/In14rD87eYXQZd2i XTDeUHVI9lpxmhDatVhHO34ArxmLFoUW4cqYKt4au3whiIfGInNKSpS6AmfwLXLiwA8J WYAQ== 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=MaM3HdcAwdWJHOpziBga2DWa0nHOgow4gYTkM3NhJ6c=; b=r6q7xj/0+jf2HFrG1MpBUL+Whcge5brzVsg+B656s5dtCZgaQNkezKUOVFilaCGSQ7 qynSIa51VWqeyXVepOBuzh8zZ/M2DTtf+mvw7VT6UnJOqY3OA7+ygwCvBjtSG5sEAT2T KWdmdUjqseG5zl0n0cI6tRUbTncV3grt4YHshJOp7EZSwHHIgEyvdJ1vaKUioKUx80fS oeZ9VNFA+voxlHJ4u4BmfvBugF+9uX4G7nzd/sa9rN/Ec2Z16qrWnCSG/AG0w89F8kpf haFM4DCFtrVXCH1i0qEZvln0eavy2Ltp9jcyyuik4l/6qFWCzviGU/wbaf4gfzCpV815 Vgvg== ARC-Authentication-Results: i=1; gmr-mx.google.com; spf=neutral (google.com: 212.18.0.10 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.10]) by gmr-mx.google.com with ESMTPS id m35si560415wms.2.2021.06.14.05.25.31 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 14 Jun 2021 05:25:31 -0700 (PDT) Received-SPF: neutral (google.com: 212.18.0.10 is neither permitted nor denied by best guess record for domain of stefano.babic@babic.homelinux.org) client-ip=212.18.0.10; Received: from frontend01.mail.m-online.net (unknown [192.168.8.182]) by mail-out.m-online.net (Postfix) with ESMTP id 4G3Vyy6bvLz1s9MQ; Mon, 14 Jun 2021 14:25:30 +0200 (CEST) Received: from localhost (dynscan1.mnet-online.de [192.168.6.70]) by mail.m-online.net (Postfix) with ESMTP id 4G3Vyy5s15z1r0wL; Mon, 14 Jun 2021 14:25:30 +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 MZ4Q0UfROxUC; Mon, 14 Jun 2021 14:25:29 +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; Mon, 14 Jun 2021 14:25:29 +0200 (CEST) Received: from localhost (mail.babic.homelinux.org [127.0.0.1]) by babic.homelinux.org (Postfix) with ESMTP id 65FA34541495; Mon, 14 Jun 2021 14:25:28 +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 yEaQVCST9GxD; Mon, 14 Jun 2021 14:25:26 +0200 (CEST) Received: from paperino.fritz.box (paperino.fritz.box [192.168.178.48]) by babic.homelinux.org (Postfix) with ESMTP id 1434C454093B; Mon, 14 Jun 2021 14:25:26 +0200 (CEST) From: stefano.babic@babic.homelinux.org To: swupdate@googlegroups.com Cc: Stefano Babic Subject: [swupdate] [PATCH V2] util: Extend get_root to find LUKS devices Date: Mon, 14 Jun 2021 14:25:22 +0200 Message-Id: <20210614122522.904662-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.10 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 --- Changes since V1: - initialize dirent to NULL to avoid segfault whren freed core/util.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/core/util.c b/core/util.c index 51a16b6..11e7d5a 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 = NULL; 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; @@ -872,7 +894,7 @@ static char *get_root_from_partitions(void) &major, &minor, &nblocks, &devname); if (ret != 4) continue; - if ((major == info.st_dev / 256) && (minor == info.st_dev % 256)) { + if ((major == dev_major) && (minor == dev_minor)) { fclose(fp); return devname; }