1

I'm using up to date Arch Linux 5.12.5.

SD cards from time to time become corrupted, and if not bricked have to be reset/ reformatted.

I do this as follows

# 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'

If I use the below line, ie miss out setting the UID:GID to me as above, then ownership of the file system is for root only and the SD card cannot be written to by anyone other than root

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

When I use the below line, which sets the UID:GID to my UID:GID, then ownership of the file system is for me only and the SD card cannot be written to by anyone other than me

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

How do I set the UID:GID so that the SD card file system can be written to by anyone?

Toby Speight
  • 8,460
  • 3
  • 26
  • 50
Kes
  • 737
  • 1
  • 8
  • 20
  • 3
    Does this answer your question? [How to make an ext4 formatted usb drive with full RW permissions for any linux machine?](https://unix.stackexchange.com/questions/422656/how-to-make-an-ext4-formatted-usb-drive-with-full-rw-permissions-for-any-linux-m) – telcoM May 22 '21 at 14:31
  • Thanks. The linked answer above uses a different approach. I wish to know whether the UID:GID can be set in such a way that will allow anyone to write to and read the SD card. – Kes May 22 '21 at 15:02
  • 1
    No, there is no "anyone" UID nor "any group" GID available. Every file must be owned by *some UID*. The only way to get an ext4 filesystem approximately "automatically writeable to everyone, on any system it's plugged into" as far as I know is to use the default ACL trick I described in my linked answer. – telcoM May 22 '21 at 15:05
  • I have read the linked answer and its use of `tunefs` and `setfacl` is superb. I also like the simplicity of the below answer which mounts the file system and sets the file permissions to `777` with `chown` is elegant. – Kes May 22 '21 at 15:15
  • 1
    The problem with just `chmod 777` is that while the root directory of the card will indeed be fully accessible to everyone on any system the card is plugged into (as long as it supports the ext4 filesystem type), any files and sub-directories created into it will follow the `umask` setting of the user creating them, resulting in them being most likely unmodifiable by other UIDs, and possibly completely un-accessible... unless the creator modifies their `umask` setting or `chmod`s the files & dirs to world-writeable afterwards. – telcoM May 22 '21 at 15:24
  • @teccoM good point – Kes May 22 '21 at 15:27
  • @telcoM Yes, the linked solution does work. You say that getting the ext4 filesystem writable in a usable way, on any system, invoves using the linked solution. I'm wandering, how do most SD Cards arrive formatted when new? partition table= msdos, file system = fat32? I use ext4 for my day to day needs so thought gpt / ext4 potentially more reliable than whatever was there before. Who would think SD card lack of reliability would lead to such a rabbit hole. – Kes May 22 '21 at 15:55
  • Most SD cards will probably come either totally unformatted or using classic MBR (msdos) partition table, and either fat32 or ExFAT filesystem according to card size. – telcoM May 22 '21 at 16:07

2 Answers2

4

What you're doing there is erasing the card, creating a GPT partition table, and then immediately overwriting the GPT partition table with a filesystem. Either don't bother creating a partition table at all, or create a partition in the partition table for the filesystem. The second is generally the recommended approach.

# 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

This will give you a filesystem that anyone can write to. (UNIX/Linux filesystems don't have owners. Files and directories have owners.) However, if you create a file or directory then it's yours and unless you allow someone to write to it, they won't be able to. This is standard UNIX/Linux behaviour, unless modified with an ACL as in a related answer

roaima
  • 107,089
  • 14
  • 139
  • 261
  • Thank you. I see you also create a `GPT` partition table and then overwrite it with a `mkfs.ext4` filesystem. Is there a difference? – Kes May 22 '21 at 15:18
  • No I don't, @Kes. Look at the device names more closely – roaima May 22 '21 at 17:40
0

Here is the script I will be using for formatting future SD Cards in future when they play up.
It's solidly based on this answer 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
Kes
  • 737
  • 1
  • 8
  • 20