sg3_utils
                                
                                
                                
                                    sg3_utils copied to clipboard
                            
                            
                            
                        interaction between sg_start and systemd-udevd may spin-up disk immediately after spin-down
I reported an issue to systemd, stating that udevd stop my HDD from spinning down. The root cause is that sg_start opens the block device in read-write mode by default. And systemd-udevd needs to rescan the partition table and calling external programs according to udev rules after sg_start closes the read-write fd, which unfortunately triggers spin-up immediately after spin-down.
I am aware that sg_start has an option -r, --readonly which opens the block device in read-only mode. But can we make sg_start read-only by default?
It's @doug-gilbert's decision, but I don't like this idea much.
Changing the default behavior of a tool that has existed for decades is dangerous. sg3_utils supports other OSes besides systemd-based Linux. There might be environments in which opening the device read-only would make the command fail (not sure about that).
Note that sg3_utils ships with its own set of SCSI udev rules, which replace systemd's native ones. These are used on openSUSE, for example, and they try to avoid doing I/O on the device. It would be interesting to figure out if the issue is also observed if the sg3_utils udev rule set is used.
In Linux, scsi_cmd_allowed allows START_STOP command on read-only fds. How about sg_start defaults to read-only on Linux, and defaults to read-write on other OSes?
I suggest you create a PR and let @doug-gilbert decide.
I thought there are many more utilities from sg3_utils could open device in read-only mode rather than read-write mode under Linux. I tried that on sg_sat_identify as well. But upon reading sg3_utils source code, I found something like this:
                pr2serr("If %s is a sg device, need read+write "
                        "permissions, even to read it!\n", inf);
and
static int
sg_in_open(const char * fnp, struct flags_t * flagp, int bs, int bpt)
{
    int flags = O_RDWR;
These code pieces make me feel not confident about changing the default open flag from O_RDWR to O_RDONLY. It should work on normal SCSI disks, but may or may not work on SCSI generic devices or other SCSI device types.
Closing this issue as my proposed modification may cause breaking changes.