Merge changes from topic "sys_rootdisk"

* changes:
  Init: add dev.mnt.blk.bootdevice to access device sysfs
  init: mount_handler: detect main block device more reliably
  init.rc: use /sys/class/block instead of /sys/devices/virtual/block
This commit is contained in:
Treehugger Robot 2022-03-16 22:44:01 +00:00 committed by Gerrit Code Review
commit 4c7dc7bd1d
3 changed files with 81 additions and 27 deletions

View file

@ -804,13 +804,18 @@ Init provides state information with the following properties.
`init.svc.<name>`
> State of a named service ("stopped", "stopping", "running", "restarting")
`dev.mnt.blk.<mount_point>`
`dev.mnt.dev.<mount_point>`, `dev.mnt.blk.<mount_point>`, `dev.mnt.rootdisk.<mount_point>`
> Block device base name associated with a *mount_point*.
The *mount_point* has / replaced by . and if referencing the root mount point
"/", it will use "/root", specifically `dev.mnt.blk.root`.
Meant for references to `/sys/device/block/${dev.mnt.blk.<mount_point>}/` and
`/sys/fs/ext4/${dev.mnt.blk.<mount_point>}/` to tune the block device
characteristics in a device agnostic manner.
"/", it will use "/root".
`dev.mnt.dev.<mount_point>` indicates a block device attached to filesystems.
(e.g., dm-N or sdaN/mmcblk0pN to access `/sys/fs/ext4/${dev.mnt.dev.<mount_point>}/`)
`dev.mnt.blk.<mount_point>` indicates the disk partition to the above block device.
(e.g., sdaN / mmcblk0pN to access `/sys/class/block/${dev.mnt.blk.<mount_point>}/`)
`dev.mnt.rootdisk.<mount_point>` indicates the root disk to contain the above disk partition.
(e.g., sda / mmcblk0 to access `/sys/class/block/${dev.mnt.rootdisk.<mount_point>}/queue`)
Init responds to properties that begin with `ctl.`. These properties take the format of
`ctl.[<target>_]<command>` and the _value_ of the system property is used as a parameter. The

View file

@ -25,6 +25,7 @@
#include <unistd.h>
#include <algorithm>
#include <filesystem>
#include <string>
#include <utility>
#include <vector>
@ -32,6 +33,7 @@
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/properties.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <fs_mgr.h>
#include <fstab/fstab.h>
@ -39,6 +41,9 @@
#include "epoll.h"
using android::base::Basename;
using android::base::StringPrintf;
namespace android {
namespace init {
@ -67,41 +72,82 @@ MountHandlerEntry ParseMount(const std::string& line) {
return MountHandlerEntry(fields[0], fields[1], fields[2]);
}
// return sda25 for dm-4, sda25 for sda25, or mmcblk0p24 for mmcblk0p24
std::string GetDiskPart(std::string blockdev) {
if (blockdev.find('/') != std::string::npos) return {};
while (android::base::StartsWith(blockdev, "dm-")) {
auto& dm = dm::DeviceMapper::Instance();
std::optional<std::string> parent = dm.GetParentBlockDeviceByPath("/dev/block/" + blockdev);
if (parent) {
blockdev = android::base::Basename(*parent);
} else {
return {};
}
}
return blockdev;
}
// return sda for sda25, or mmcblk0 for mmcblk0p24
std::string GetRootDisk(std::string blockdev) {
if (blockdev.empty()) return {};
if (blockdev.find('/') != std::string::npos) return {};
std::error_code ec;
for (const auto& entry : std::filesystem::directory_iterator("/sys/block", ec)) {
const std::string path = entry.path().string();
if (std::filesystem::exists(StringPrintf("%s/%s", path.c_str(), blockdev.c_str()))) {
return Basename(path);
}
}
return {};
}
void SetMountProperty(const MountHandlerEntry& entry, bool add) {
static constexpr char devblock[] = "/dev/block/";
if (!android::base::StartsWith(entry.blk_device, devblock)) return;
std::string value;
auto target = entry.blk_device.substr(strlen(devblock));
std::string diskpart, rootdisk;
if (add) {
value = entry.blk_device.substr(strlen(devblock));
if (android::base::StartsWith(value, "sd")) {
// All sd partitions inherit their queue characteristics
// from the whole device reference. Strip partition number.
auto it = std::find_if(value.begin(), value.end(), [](char c) { return isdigit(c); });
if (it != value.end()) value.erase(it, value.end());
}
auto queue = "/sys/block/" + value + "/queue";
diskpart = GetDiskPart(target);
rootdisk = GetRootDisk(diskpart);
struct stat sb;
if (stat(queue.c_str(), &sb) || !S_ISDIR(sb.st_mode)) value = "";
if (stat(entry.mount_point.c_str(), &sb) || !S_ISDIR(sb.st_mode)) value = "";
if (stat(entry.mount_point.c_str(), &sb) || !S_ISDIR(sb.st_mode)) rootdisk = "";
// Clear the noise associated with loopback and APEX.
if (android::base::StartsWith(value, "loop")) value = "";
if (android::base::StartsWith(entry.mount_point, "/apex/")) value = "";
if (android::base::StartsWith(target, "loop")) rootdisk = "";
if (android::base::StartsWith(entry.mount_point, "/apex/")) rootdisk = "";
}
auto mount_prop = entry.mount_point;
if (mount_prop == "/") mount_prop = "/root";
std::replace(mount_prop.begin(), mount_prop.end(), '/', '.');
auto blk_mount_prop = "dev.mnt.blk" + mount_prop;
auto dev_mount_prop = "dev.mnt.dev" + mount_prop;
// Set property even if its value does not change to trigger 'on property:'
auto rootdisk_mount_prop = "dev.mnt.rootdisk" + mount_prop;
// Set property even if its rootdisk does not change to trigger 'on property:'
// handling, except for clearing non-existent or already clear property.
// Goal is reduction of empty properties and associated triggers.
if (value.empty() && android::base::GetProperty(blk_mount_prop, "").empty()) return;
android::base::SetProperty(blk_mount_prop, value);
if (!value.empty()) {
android::base::SetProperty(dev_mount_prop, entry.blk_device.substr(strlen(devblock)));
} else {
if (rootdisk.empty() && android::base::GetProperty(blk_mount_prop, "").empty()) return;
if (rootdisk.empty()) {
android::base::SetProperty(blk_mount_prop, "");
android::base::SetProperty(dev_mount_prop, "");
android::base::SetProperty(rootdisk_mount_prop, "");
return;
}
// 1. dm-N
// dev.mnt.dev.data = dm-N
// dev.mnt.blk.data = sdaN or mmcblk0pN
// dev.mnt.rootdisk.data = sda or mmcblk0
//
// 2. sdaN or mmcblk0pN
// dev.mnt.dev.data = sdaN or mmcblk0pN
// dev.mnt.blk.data = sdaN or mmcblk0pN
// dev.mnt.rootdisk.data = sda or mmcblk0
android::base::SetProperty(dev_mount_prop, target);
android::base::SetProperty(blk_mount_prop, diskpart);
android::base::SetProperty(rootdisk_mount_prop, rootdisk);
}
} // namespace

View file

@ -1085,9 +1085,11 @@ on boot
mkdir /dev/sys/fs/by-name 0755 system system
symlink /sys/fs/f2fs/${dev.mnt.dev.data} /dev/sys/fs/by-name/userdata
# to access dm-<num> sysfs
# dev.mnt.dev.data=dm-N, dev.mnt.blk.data=sdaN/mmcblk0pN, dev.mnt.rootdisk.data=sda/mmcblk0, or
# dev.mnt.dev.data=sdaN/mmcblk0pN, dev.mnt.blk.data=sdaN/mmcblk0pN, dev.mnt.rootdisk.data=sda/mmcblk0
mkdir /dev/sys/block/by-name 0755 system system
symlink /sys/devices/virtual/block/${dev.mnt.dev.data} /dev/sys/block/by-name/userdata
symlink /sys/class/block/${dev.mnt.dev.data} /dev/sys/block/by-name/userdata
symlink /sys/class/block/${dev.mnt.rootdisk.data} /dev/sys/block/by-name/rootdisk
# F2FS tuning. Set cp_interval larger than dirty_expire_centisecs, 30 secs,
# to avoid power consumption when system becomes mostly idle. Be careful
@ -1099,8 +1101,9 @@ on boot
# limit discard size to 128MB in order to avoid long IO latency
# for filesystem tuning first (dm or sda)
# Note that, if dm-<num> is used, sda/mmcblk0 should be tuned in vendor/init.rc
# this requires enabling selinux entry for sda/mmcblk0 in vendor side
write /dev/sys/block/by-name/userdata/queue/discard_max_bytes 134217728
write /dev/sys/block/by-name/rootdisk/queue/discard_max_bytes 134217728
# Permissions for System Server and daemons.
chown system system /sys/power/autosleep