您好,我正在尝试检测主要针对根分区的 ro 文件系统
这是使用 USB 作为操作系统磁盘的便携式系统
状态为只读,我无法写入任何内容
touch /tmp
touch: setting times of '/tmp': Read-only file system
lsblk 输出
lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 1 28.7G 0 disk
├─sdb1 8:17 1 512M 0 part /boot/efi
└─sdb2 8:18 1 15.5G 0 part /
这是 json 输出
{
"blockdevices": [
{
"name": "sdb",
"kname": "sdb",
"path": "/dev/sdb",
"maj:min": "8:16",
"fsavail": null,
"fssize": null,
"fstype": null,
"fsused": null,
"fsuse%": null,
"fsver": null,
"mountpoint": null,
"label": null,
"uuid": null,
"ptuuid": "cc59543f-1263-436b-b654-441947b5d432",
"pttype": "gpt",
"parttype": null,
"parttypename": null,
"partlabel": null,
"partuuid": null,
"partflags": null,
"ra": 128,
"ro": false,
"rm": true,
"hotplug": true,
"model": "SanDisk_3.2Gen1",
"serial": "0401915ccab85f2287ceb131fd353892d7ae4b148e3eff697bb2af15fdab6e26684500000000000000000000899c7a1b00827b18915581076eab2054",
"size": "28.7G",
"state": "running",
"owner": "root",
"group": "disk",
"mode": "brw-rw----",
"alignment": 0,
"min-io": 512,
"opt-io": 0,
"phy-sec": 512,
"log-sec": 512,
"rota": true,
"sched": "mq-deadline",
"rq-size": 2,
"type": "disk",
"disc-aln": 0,
"disc-gran": "0B",
"disc-max": "0B",
"disc-zero": false,
"wsame": "0B",
"wwn": null,
"rand": true,
"pkname": null,
"hctl": "52:0:0:0",
"tran": "usb",
"subsystems": "block:scsi:usb:pci",
"rev": "1.00",
"vendor": " USB ",
"zoned": "none",
"dax": false,
"children": [
{
"name": "sdb1",
"kname": "sdb1",
"path": "/dev/sdb1",
"maj:min": "8:17",
"fsavail": "506M",
"fssize": "511M",
"fstype": "vfat",
"fsused": "5M",
"fsuse%": "1%",
"fsver": "FAT32",
"mountpoint": "/boot/efi",
"label": null,
"uuid": "6729-75FE",
"ptuuid": "cc59543f-1263-436b-b654-441947b5d432",
"pttype": "gpt",
"parttype": "c12a7328-f81f-11d2-ba4b-00a0c93ec93b",
"parttypename": "EFI System",
"partlabel": null,
"partuuid": "54ce8d01-e9bc-434b-9971-5d31ded28098",
"partflags": null,
"ra": 128,
"ro": false,
"rm": true,
"hotplug": true,
"model": null,
"serial": null,
"size": "512M",
"state": null,
"owner": "root",
"group": "disk",
"mode": "brw-rw----",
"alignment": 0,
"min-io": 512,
"opt-io": 0,
"phy-sec": 512,
"log-sec": 512,
"rota": true,
"sched": "mq-deadline",
"rq-size": 2,
"type": "part",
"disc-aln": 0,
"disc-gran": "0B",
"disc-max": "0B",
"disc-zero": false,
"wsame": "0B",
"wwn": null,
"rand": true,
"pkname": "sdb",
"hctl": null,
"tran": null,
"subsystems": "block:scsi:usb:pci",
"rev": null,
"vendor": null,
"zoned": "none",
"dax": false
},
{
"name": "sdb2",
"kname": "sdb2",
"path": "/dev/sdb2",
"maj:min": "8:18",
"fsavail": "8.1G",
"fssize": "15.2G",
"fstype": "ext4",
"fsused": "6.2G",
"fsuse%": "41%",
"fsver": "1.0",
"mountpoint": "/",
"label": null,
"uuid": "f769a7b6-9d09-4bc7-9249-85f50d4a1c2e",
"ptuuid": "cc59543f-1263-436b-b654-441947b5d432",
"pttype": "gpt",
"parttype": "0fc63daf-8483-4772-8e79-3d69d8477de4",
"parttypename": "Linux filesystem",
"partlabel": null,
"partuuid": "0cf31261-d5f9-4182-894c-283bd8fb62ad",
"partflags": null,
"ra": 128,
"ro": false,
"rm": true,
"hotplug": true,
"model": null,
"serial": null,
"size": "15.5G",
"state": null,
"owner": "root",
"group": "disk",
"mode": "brw-rw----",
"alignment": 0,
"min-io": 512,
"opt-io": 0,
"phy-sec": 512,
"log-sec": 512,
"rota": true,
"sched": "mq-deadline",
"rq-size": 2,
"type": "part",
"disc-aln": 0,
"disc-gran": "0B",
"disc-max": "0B",
"disc-zero": false,
"wsame": "0B",
"wwn": null,
"rand": true,
"pkname": "sdb",
"hctl": null,
"tran": null,
"subsystems": "block:scsi:usb:pci",
"rev": null,
"vendor": null,
"zoned": "none",
"dax": false
}
]
}
]
}
在 lsblk RO 列中,它
在 json 格式 readonly 中显示为 0 也标记为 false
但是当检查 /proc/mounts 时它显示为 ro ( /dev/sdb2 )
cat /proc/mounts
/dev/sdb2 / ext4 ro,relatime,errors=remount-ro 0 0
/dev/sdb1 /boot/efi vfat rw,relatime,fmask=0077,dmask=0077,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro 0 0
那么 lsblk 无法读取正确的状态?
从 util-linux 2.36.1 的debian 11 lsblk 运行
我需要通知的输出,最好使用 lsblk,因为它具有 json 格式。
我还发现/proc/mounts有时对非root有奇怪的记录
# correct fstab but here different disk in same mount
# 2 of them ro , 1 of them rw
/dev/sdk1 /mnt/disk-***0G ext4 ro,noatime 0 0
/dev/sdo1 /mnt/disk-***0G ext4 ro,noatime 0 0
/dev/sdq1 /mnt/disk-***0G ext4 rw,noatime 0 0
在 findmnt 上测试
├─/mnt/disk-***0G /dev/sdk1 ext4 ro,noatime
│ └─/mnt/disk-***0G /dev/sdo1 ext4 ro,noatime
│ └─/mnt/disk-***0G /dev/sdq1 ext4 rw,noatime
│ └─/mnt/disk-***0G /dev/sdl1 ext4 rw,noatime
使用此选项使用 UUID 进行非根磁盘挂载
如果操作系统出现错误,那么陈旧操作系统并不那么重要,但需要知道是否有错误。
defaults,nofail,noatime,x-systemd.device-timeout=10s,errors=continue 0 0
答案1
正如纳西尔在评论中所说,输出RO
中的列lsblk
是关于只读设备的,而不是文件系统。因此,如果它是只读的,因为文件系统是使用该ro
选项安装的lsblk
,则不会显示它,因为设备本身仍然是读写的。lsblk
只能显示设备“状态”:
$ lsblk -o+RO /dev/sde3
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS RO
sde3 8:67 0 10M 0 part 0
$ sudo blockdev --setro /dev/sde3
$ lsblk /dev/sde3
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sde3 8:67 0 10M 1 part
如果要检查文件系统是否以只读方式挂载,则需要检查ro
或rw
mount 选项(默认情况mount
下将使用该选项方便地挂载只读设备,ro
因此您不需要同时检查设备和文件系统)。你可以用来findmnt
这样做,例如类似的事情
$ findmnt -n -o options /dev/sde3
ro,relatime,seclabel
$ findmnt -n -o options /dev/sde3 | grep -q ^ro && echo "read only"
read only