如何在 SD 卡上创建任何人都可以写入的 mkfs.ext4 文件系统?

如何在 SD 卡上创建任何人都可以写入的 mkfs.ext4 文件系统?

我正在使用最新的 Arch Linux 5.12.5。

SD 卡有时会损坏,如果没有变砖,则必须重置/重新格式化。

我这样做如下

# 1. unmount the card / make sure it's unmounted
umount /dev/mmcblk0
umount /dev/mmcblk0p1

# 2. wipe the card. After this the card cannot be mounted becasue 
#      there is no partition. There's nothing on it at all.
echo password | sudo -S dd bs=4M if=/dev/zero of=/dev/mmcblk0 oflag=sync

# 3. create a GPT partition table
#   the "-s" defaults the go ahead answer to "yes" so that 
#      no user input is necessary rather confusingly the
#         command is 'mklabel' for creating a partition table!
sudo parted -s /dev/mmcblk0 mklabel gpt

# 4. create a GPT file system
#   HAVING THE "-E root_owner=$UID:$GID" IS ESSENTIAL,
#      OTHERWISE THE PARTITION CAN ONLY BE WRITTEN TO AS ROOT
sudo mkfs.ext4 -F -O ^64bit -E root_owner=$UID:$GID -L 'SD_CARD' '/dev/mmcblk0'

如果我使用下面的行,即错过了如上所述为我设置 UID:GID,则文件系统的所有权仅属于 root 用户,并且除 root 之外的任何人都无法写入 SD 卡

sudo mkfs.ext4 -F -O ^64bit -L 'SD_CARD' '/dev/mmcblk0

当我使用以下行将 UID:GID 设置为我的 UID:GID 时,文件系统的所有权仅属于我,并且除我之外的任何人都无法写入 SD 卡

sudo mkfs.ext4 -F -O ^64bit -E root_owner=$UID:$GID -L 'SD_CARD' '/dev/mmcblk0'

如何设置UID:GID以便任何人都可以写入SD卡文件系统?

答案1

您所做的就是擦除卡,创建 GPT 分区表,然后立即用文件系统覆盖 GPT 分区表。要么根本不创建分区表,要么在分区表中为文件系统创建一个分区。第二种通常是推荐的方法。

# Create partition table with a single full-sized partition
parted -s /dev/mmcblk0 mklabel gpt
parted /dev/mmcblk0 unit MiB mkpart primary 1 100%

# Print it out (optional)
parted /dev/mmcblk0 unit MiB print

# Create filesystem on the partition we have just made
mkfs.ext4 -L SD_CARD /dev/mmcblk0p1

# Mount it
mkdir -p /mnt/dsk
mount /dev/mmcblk0p1 /mnt/dsk

# Allow anyone to write to it
chmod 777 /mnt/dsk

这将为您提供一个任何人都可以写入的文件系统。 (UNIX/Linux 文件系统没有所有者。文件和目录有所有者。)但是,如果您创建文件或目录,那么它就是您的,除非您允许某人写入,否则他们将无法写入。这是标准的 UNIX/Linux 行为,除非使用 ACL 进行修改(如在相关答案

答案2

这是我将来在播放 SD 卡时将使用的脚本。
它完全基于这个答案 https://unix.stackexchange.com/a/422687/46470

# creates GPT partition table
# creates ext4 file system
# creates file system writable by anyone
# variables

disk_z="mmcblk0"
part_z="p1"
user_z="$USER"
password_z="password"

# issue sudo password so that password is not needed again
echo password_z | sudo -S clear

# make sure the device is not mounted
sudo umount /dev/$disk_z
sudo umount /dev/$disk_z$part_z

# Create partition table with a single full-sized partition
sudo parted -s /dev/$disk_z mklabel gpt
sudo parted /dev/$disk_z unit MiB mkpart primary 1 100%

# Create (1) "ext4" file system partition with (2) "64bit" filesystem
#     and (3) name "p1" and (4) disk label "SD_CARD"  
sudo mkfs.ext4 -F -O ^64bit -L 'SD_CARD' "/dev/$disk_z$part_z"

# tune2fs adjusts tunable filesystem parameters
#   -o    calls mount options
#    acl   enables Posix Access Control Lists to be stored 
#                                         in the filesystems metadata
# -o acl will enable the ACL to be set by "setfacl" once 
#                             the partition is mounted and this will be 
#                                       stored in the filesystems metadata
sudo tune2fs -o acl "/dev/$disk_z$part_z"

# mount the file system
sudo mount "/dev/$disk_z$part_z" /mnt

# change ownership of the mounted file system
sudo chown "$user_z": /mnt

# chmod it to have rwx permissions for everyone
sudo chmod 777 /mnt

# SET File ACL (access list) permissions for users, groups and owner to rwx in each case
#    and store this in the files systems metadata
sudo setfacl -m d:u::rwx,d:g::rwx,d:o::rwx /mnt

# unmount the disk
sudo umount /mnt

相关内容