From patchwork Tue Mar 28 06:32:08 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= X-Patchwork-Id: 744093 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3vsh0X0hhgz9s1h for ; Tue, 28 Mar 2017 17:32:58 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="js8RISq8"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="G9zTAvXX"; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Subject:MIME-Version:Message-Id:Date:To :From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=cZgtcBPEmkRXOFxo5efNFxydiBv/tzj/xT6xA4uVCiw=; b=js8RISq8ONzodv kr4OLulsijyWyFCbOBCQ6mk96Q82BSx3Nl3DEV4cNfb4ImpM65aPv1hGwsxgfwDsh/jSvXPkkvAYr qq4/alPu032P8VnYB+FCog0veJA9utzlBwtsOwPhzF3TEeuvl69/2laFhNwAmcHSIBTfUYoOyEFkZ xPqE/CdqCBzB+LSy3c9H2OoVdrqRQ8xB0WnDFbgK0vU7VxtlorIf8O9QOuheLPVTXuvPJgeHyyRsr G/ogMj9zgpSdJJNLb9DTk0ZlB96GrO6tAwyUikArwArCK6ZgOr0hEyGTE18miGijTx17DcB1I+EgD na5mTYnhcORm0v1jRPbg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1cskgS-0004o8-GR; Tue, 28 Mar 2017 06:32:48 +0000 Received: from mail-lf0-x241.google.com ([2a00:1450:4010:c07::241]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1cskgN-0004mw-ED for lede-dev@lists.infradead.org; Tue, 28 Mar 2017 06:32:45 +0000 Received: by mail-lf0-x241.google.com with SMTP id v2so9472018lfi.2 for ; Mon, 27 Mar 2017 23:32:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=AFT/BbAfWAksLrAGxmKQRPX6KKRmkbISL0BN+1psR74=; b=G9zTAvXX3xPvUlza6qDaDmaPX0mP9TghHgIWXyObraW29X1nsfvPnJtqCdqq3eiVjN v9Ynbbz/az9RnPI6V11Q34b0380R1XRkSzbEaYn9hjocwVPVRT+P404AwwHAEtGVe94Z yDymG1yAsfR1xinsEHJnoLE9TyqFCfIc/kdCzpPwML3fvi0Vb56kvWpgVJZy6bLYRs9v i8Dc4DWRu6pTXIss3eCcGI4tlUOnyfwJl64GgggEPJ1lveEobT8djbr/IUhIdHoC6G1r 4icQ69s4Q224KSCbzotSI0oM2n6ehgBxS1StY3BToQR/VJAqsOUPXgsd/KszXR+04NXZ N0eA== 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:mime-version :content-transfer-encoding; bh=AFT/BbAfWAksLrAGxmKQRPX6KKRmkbISL0BN+1psR74=; b=j1SztrqkKGPqtQJKT3cQ2U43OApWYA145iOQu5m3nvOUrAVI8zEotjE9XLKYm0M6lL y0IXC+ByTNBlOw6X3umro9wp1LY2T0N8yWLEqXGMOu5fWKPgxHT4VrOGFnAqPauodo3V K5tACO4dhLMpc/qZtEwzu2fSuGUaCG30Kwo3Nln3E9gUsWS5F09fe1Q9v5T2BC4P+5b7 U/zJMUL4QLkh5yYSOSe+S+8WS69wSK+onXo/KEGwdKMntHvqRhuCMsi2xXfiQo7OHZwy uwXMADYv99wWIzYPf92W9OQOVJbpYAcueGo28DEYSxZyC7ITJlOg+eNa/clnJS6n4nw6 uKFQ== X-Gm-Message-State: AFeK/H0YTDa0wjHj03SG+RrUnTkCo8dbFusPhWe5Nu6CPY9Wl9Kgb4x2s+015yjjNY1gNQ== X-Received: by 10.25.221.195 with SMTP id w64mr10565832lfi.31.1490682740571; Mon, 27 Mar 2017 23:32:20 -0700 (PDT) Received: from linux-samsung.lan (ip-194-187-74-233.konfederacka.maverick.com.pl. [194.187.74.233]) by smtp.gmail.com with ESMTPSA id h7sm515219lji.10.2017.03.27.23.32.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 27 Mar 2017 23:32:19 -0700 (PDT) From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= To: lede-dev@lists.infradead.org Date: Tue, 28 Mar 2017 08:32:08 +0200 Message-Id: <20170328063208.27537-1-zajec5@gmail.com> X-Mailer: git-send-email 2.11.0 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170327_233243_678882_BC2970AC X-CRM114-Status: GOOD ( 17.23 ) X-Spam-Score: -2.5 (--) X-Spam-Report: SpamAssassin version 3.4.1 on bombadil.infradead.org summary: Content analysis details: (-2.5 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [2a00:1450:4010:c07:0:0:0:241 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (zajec5[at]gmail.com) 0.2 FREEMAIL_ENVFROM_END_DIGIT Envelope-from freemail username ends in digit (zajec5[at]gmail.com) -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature Subject: [LEDE-DEV] [PATCH RFC] uinit: add package with init for initramfs X-BeenThere: lede-dev@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= Sender: "Lede-dev" Errors-To: lede-dev-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org From: Rafał Miłecki In OpenWrt/LEDE we always got hacks for mounting rootfs. They were needed because we couldn't use bootargs properly due to various reasons (e.g. dynamically created partitions). The proper solution for this (instead of hacking a kernel) is to use initramfs with init that will initialize rootfs. Right now this init implementation only mounts "rootfs" parition and switches to it. So far this allows dropping kernel patch setting ROOT_DEV. The next step is to implement ubi support (attaching & mounting a "rootfs" volume). In future this will also allow us to implement more feature like: 1) Cleaning/preparing UBI partition (important for bootloaders that don't empty whole partition before flashing). 2) Resizing data partition to use whole space (important for images for hard drives or SD cards). This package installs init to the $(TARGET_ROOTFS_DIR)/initramfs/ . This will require integrating with LEDE in order to: 1) Always use initramfs 2) Add above directory for non-initramfs images to the CONFIG_INITRAMFS_SOURCE Signed-off-by: Rafał Miłecki --- Apart from obvious questions (is this OK? what to change? how to package it properly?) I'm wondering about license. Do you think GPLv2 is fine for this? --- package/system/uinit/Makefile | 27 +++++ package/system/uinit/src/CMakeLists.txt | 8 ++ package/system/uinit/src/main.c | 187 ++++++++++++++++++++++++++++++++ 3 files changed, 222 insertions(+) create mode 100644 package/system/uinit/Makefile create mode 100644 package/system/uinit/src/CMakeLists.txt create mode 100644 package/system/uinit/src/main.c diff --git a/package/system/uinit/Makefile b/package/system/uinit/Makefile new file mode 100644 index 0000000000..0b636dbbb4 --- /dev/null +++ b/package/system/uinit/Makefile @@ -0,0 +1,27 @@ +include $(TOPDIR)/rules.mk + +PKG_NAME:=uinit +PKG_RELEASE:=1 + +CMAKE_INSTALL:=1 + +include $(INCLUDE_DIR)/package.mk +include $(INCLUDE_DIR)/cmake.mk + +define Package/uinit + SECTION:=base + CATEGORY:=Base system + TITLE:=uinit for initramfs +endef + +define Build/Prepare + mkdir -p $(PKG_BUILD_DIR) + $(CP) ./src/* $(PKG_BUILD_DIR)/ +endef + +define Package/uinit/install + $(INSTALL_DIR) $(TARGET_ROOTFS_DIR)/initramfs + $(INSTALL_BIN) $(PKG_BUILD_DIR)/init $(TARGET_ROOTFS_DIR)/initramfs/ +endef + +$(eval $(call BuildPackage,uinit)) diff --git a/package/system/uinit/src/CMakeLists.txt b/package/system/uinit/src/CMakeLists.txt new file mode 100644 index 0000000000..991c58f662 --- /dev/null +++ b/package/system/uinit/src/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 2.6) + +project(uinit) +set(CMAKE_C_FLAGS "-Wall") + +add_executable(init main.c) +target_compile_definitions(init PUBLIC _GNU_SOURCE) +install(TARGETS init RUNTIME DESTINATION /) diff --git a/package/system/uinit/src/main.c b/package/system/uinit/src/main.c new file mode 100644 index 0000000000..298f12268a --- /dev/null +++ b/package/system/uinit/src/main.c @@ -0,0 +1,187 @@ +/* + * Copyright (C) 2017 Rafał Miłecki + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include +#include +#include +#include +#include +#include + +#if 0 +#include +static int ls(const char *path) +{ + DIR *dir; + struct dirent *dp; + + dir = opendir(path); + if (!dir) + return errno; + + printf("Listing %s\n", path); + while ((dp = readdir(dir))) { + struct stat fileStat; + + if (stat(dp->d_name, &fileStat)) { + fprintf(stderr, "Failed to stat %s\n", dp->d_name); + continue; + } + printf((S_ISDIR(fileStat.st_mode)) ? "d" : "-"); + printf((fileStat.st_mode & S_IRUSR) ? "r" : "-"); + printf((fileStat.st_mode & S_IWUSR) ? "w" : "-"); + printf((fileStat.st_mode & S_IXUSR) ? "x" : "-"); + printf((fileStat.st_mode & S_IRGRP) ? "r" : "-"); + printf((fileStat.st_mode & S_IWGRP) ? "w" : "-"); + printf((fileStat.st_mode & S_IXGRP) ? "x" : "-"); + printf((fileStat.st_mode & S_IROTH) ? "r" : "-"); + printf((fileStat.st_mode & S_IWOTH) ? "w" : "-"); + printf((fileStat.st_mode & S_IXOTH) ? "x" : "-"); + printf("\t%s\n", dp->d_name); + } + + closedir(dir); + + return 0; +} + +static int cat(const char *path) +{ + FILE *fp; + char line[64]; + + fp = fopen(path, "r"); + if (!fp) + return -EIO; + + printf("Content of %s:\n", path); + while (fgets(line, sizeof(line), fp)) { + printf("%s", line); + } + printf("\n"); + + fclose(fp); + + return 0; +} +#endif + +static int mount_basics() +{ + if (mount("proc", "/proc", "proc", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0)) + return errno; + + if (mount("sysfs", "/sys", "sysfs", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0)) + return errno; + + if (mount("devtmpfs", "/dev", "devtmpfs", MS_NOATIME | MS_NOSUID, "mode=0755,size=512K")) + return errno; + + return 0; +} + +static int find_block_mtd(char *name, char *mtdblock, size_t size) +{ + FILE *fp; + char line[64]; + int index = -ENOENT; + + fp = fopen("/proc/mtd", "r"); + if (!fp) + return -EIO; + + while (fgets(line, sizeof(line), fp)) { + char dev[8]; + int i; + + if (sscanf(line, "mtd%d: %*x %*x \"%7[^\"]", &i, dev) == 2) { + if (!strcmp(dev, name)) { + index = i; + if (mtdblock) + snprintf(mtdblock, size, "/dev/mtdblock%d", index); + break; + } + } + } + + fclose(fp); + + return index; +} + +static int mount_rootfs() +{ + char mtdblock[32]; + int index; + + index = find_block_mtd("rootfs", mtdblock, sizeof(mtdblock)); + if (index < 0) { + fprintf(stderr, "Failed to find rootfs: %d\n", index); + return index; + } + + if (mount(mtdblock, "/mnt", "squashfs", MS_RDONLY, 0)) + return errno; + + return 0; +} + +static int switch_root() +{ + if (mount("/proc", "/mnt/proc", NULL, MS_NOATIME | MS_MOVE, NULL)) + return errno; + + if (mount("/sys", "/mnt/sys", NULL, MS_NOATIME | MS_MOVE, NULL)) + return errno; + + if (mount("/dev", "/mnt/dev", NULL, MS_NOATIME | MS_MOVE, NULL)) + return errno; + + if (chdir("/mnt")) + return errno; + + if (mount(".", "/", NULL, MS_MOVE, NULL)) + return errno; + + if (chroot(".")) + return errno; + + if (chdir("/")) + return errno; + + return 0; +} + +int main(int argc, char **argv) +{ + int err; + + err = mount_basics(); + if (err) { + fprintf(stderr, "Failed to mount basic points: %d\n", err); + return err; + } + + err = mount_rootfs(); + if (err) { + fprintf(stderr, "Failed to mount rootfs: %d\n", err); + return err; + } + + err = switch_root(); + if (err) { + fprintf(stderr, "Failed to switch to the new root: %d\n", err); + return err; + } + + printf("Successfully switched to the mounted rootfs\n"); + + execl("/sbin/init", "/sbin/init", (char *)NULL); + + return 0; +}