Disabling SATA disk and scanning for it

An annoying kernel bug delays resume from suspend to RAM: https://bugzilla.opensuse.org/show_bug.cgi?id=1195025 As a workaround the HDD involved is deactivated on suspend.

Scripts:

The dirty hack works as long as the HDD is plugged into the third SATA header on the motherboard.

Deactivate disk:


**erlangen:~ #** cat /usr/local/bin/delete-backup-disk                                                                            
#!/bin/bash 
if  -f /sys/devices/pci0000:00/0000:00:17.0/ata4/host3/target3:0:0/3:0:0:0/delete ] ; then 
        echo 1 > /sys/devices/pci0000:00/0000:00:17.0/ata4/host3/target3:0:0/3:0:0:0/delete 
fi 
**erlangen:~ #**

Scan for disk:

**erlangen:~ #** cat /usr/local/bin/scan-for-backup-disk  
#!/bin/bash 
echo "- - -" > /sys/devices/pci0000:00/0000:00:17.0/ata4/host3/scsi_host/host3/scan 
**erlangen:~ #**

Services:

Disable HDD on boot and prior to suspend

**erlangen:~ #** systemctl cat HDD-disable.service  
**# /etc/systemd/system/HDD-disable.service**
[Unit] 
Description=Disable backup HDD 

[Service] 
ExecStart=/usr/local/bin/delete-backup-disk 

[Install] 
WantedBy=multi-user.target 
**erlangen:~ #**

Scan for disk:

**erlangen:~ #** systemctl cat HDD-scan.service  
**# /etc/systemd/system/HDD-scan.service**
[Unit] 
Description=Scan for backup HDD 

[Service] 
ExecStart=/usr/local/bin/scan-for-backup-disk 
**erlangen:~ #**

Modification to suspend:

**erlangen:~ #** systemctl cat systemd-suspend 
**# /etc/systemd/system/systemd-suspend.service**
#  SPDX-License-Identifier: LGPL-2.1-or-later 
# 
#  This file is part of systemd. 
# 
#  systemd is free software; you can redistribute it and/or modify it 
#  under the terms of the GNU Lesser General Public License as published by 
#  the Free Software Foundation; either version 2.1 of the License, or 
#  (at your option) any later version. 

[Unit] 
Description=System Suspend 
Documentation=man:systemd-suspend.service(8) 
DefaultDependencies=no 
Requires=sleep.target **HDD-disable.service** 
After=sleep.target **HDD-disable.service** 

[Service] 
Type=oneshot 
ExecStart=/usr/lib/systemd/systemd-sleep suspend 
**erlangen:~ #**

Changes to backup service:

[FONT=monospace]**erlangen:~ #** systemctl cat backup-home.service  
**# /etc/systemd/system/backup-home.service**
[Unit] 
Description=Backup /home 
Requires=HDD.mount **HDD-scan.service** 
After=HDD.mount **HDD-scan.service** 

[Service] 
ExecStart=/usr/bin/rsync -a --exclude=.cache --exclude=covid-19-data /home/ /HDD/backup/home/ 
ExecStopPost=/usr/bin/umount /HDD 
**ExecStopPost=/usr/local/bin/delete-backup-disk **
**erlangen:~ #**

 
[/FONT]

Resume from suspend is now as fast as it was prior to kernel bug:

**erlangen:~ #** journalctl -b -u HDD-disable.service -u systemd-suspend.service --since 11:53 -o short-monotonic              
 4001.282464] erlangen systemd[1]: Started Disable backup HDD. 
 4001.286366] erlangen systemd[1]: Starting System Suspend... 
 4001.289061] erlangen systemd[1]: HDD-disable.service: Deactivated successfully. 
 4001.294992] erlangen systemd-sleep[5105]: INFO: Skip running /usr/lib/systemd/system-sleep/grub2.sleep for suspend 
 **4001.296099**] erlangen systemd-sleep[5103]: Entering sleep state 'suspend'... 
 **4003.592988**] erlangen systemd-sleep[5103]: System returned from sleep state. 
 4003.599700] erlangen systemd-sleep[5119]: INFO: Skip running /usr/lib/systemd/system-sleep/grub2.sleep for suspend 
 4003.600892] erlangen systemd[1]: systemd-suspend.service: Deactivated successfully. 
 4003.601116] erlangen systemd[1]: Finished System Suspend. 
**erlangen:~ #**

Few questions;

  • What does mean “deactivated”? Could you fill the table bellow? Change, add or remove info.

                      |State/Mode name|Synonymy|Read/Write|Spinning|Mounted|Power On|Something else|
    

|—|—|—|—|—|—|—|
|Active|| Y| Y| Y| Y||
|Idle|| N| Y| Y| Y||
|Sleep|||||||
|Standby|| N| N| Y| Y||
|Passive|||||||
|Low power|||||||
|Power off|| N| N|| N||
|Deactivated|| N| N||||

  • Does your new files/settings avoid HDDs to spin-up (if it was spun down) in the process of computer shut down?

The disk is physically plugged into the power supply and into the SATA header on the main board. However the system doesn’t know about it. Scanning SATA attaches the drive:

erlangen:~ # ll /dev/sd* 
brw-rw---- 1 root disk 8, 0 Apr  5 20:41 /dev/sda
brw-rw---- 1 root disk 8, 1 Apr  5 20:41 /dev/sda1
erlangen:~ # scan-for-backup-disk  
erlangen:~ # ll /dev/sd*           
brw-rw---- 1 root disk 8,  0 Apr  5 20:41 /dev/sda
brw-rw---- 1 root disk 8,  1 Apr  5 20:41 /dev/sda1
brw-rw---- 1 root disk 8, 16 Apr  6 20:20 /dev/sdb
brw-rw---- 1 root disk 8, 17 Apr  6 20:20 /dev/sdb1
erlangen:~ # hdparm -C /dev/sdb                                                                  

/dev/sdb: 
 drive state is:  active/idle 
erlangen:~ # 

Order matters:

**erlangen:~ #** systemctl cat backup-home.service                                                                   
**# /etc/systemd/system/backup-home.service**
[Unit] 
Description=Backup /home 
**Requires=HDD-scan.service HDD.mount 
After=HDD-scan.service HDD.mount 
**
[Service] 
ExecStart=/usr/bin/rsync -a --exclude=.cache --exclude=covid-19-data /home/ /HDD/backup/home/ 
ExecStopPost=/usr/bin/umount /HDD 
ExecStopPost=/usr/local/bin/delete-backup-disk 
**erlangen:~ #**
[FONT=monospace]**erlangen:~ #** journalctl -b -u backup-home.service -u 'HDD*' --since 0:00                            
Apr 08 04:49:59 erlangen systemd[1]: Started Scan for backup HDD. 
Apr 08 04:50:10 erlangen systemd[1]: HDD-scan.service: Deactivated successfully. 
Apr 08 04:50:11 erlangen systemd[1]: Mounting /HDD... 
Apr 08 04:50:11 erlangen systemd[1]: Mounted /HDD. 
Apr 08 04:50:11 erlangen systemd[1]: Started Backup /home. 
Apr 08 04:51:40 erlangen systemd[1]: HDD.mount: Deactivated successfully. 
Apr 08 04:51:42 erlangen systemd[1]: backup-home.service: Deactivated successfully. 
Apr 08 04:51:42 erlangen systemd[1]: backup-home.service: Consumed 19.816s CPU time. 
**erlangen:~ #**[/FONT]

List activated disks (option N skips nvme):

**erlangen:~ #** lsscsi -sN           
[2:0:0:0]    disk    ATA      CT2000BX500SSD1  030   /dev/sda   2.00TB 
[3:0:0:0]    disk    ATA      WDC WD40EZRX-22S 0A80  /dev/sdb   4.00TB 
[4:0:0:0]    cd/dvd  PIONEER  DVD-RW  DVR-221  1.00  /dev/sr0        - 
**erlangen:~ #**

List device units:

**erlangen:~ #** systemctl list-units --all blockdev@* 
  UNIT                                                                                 LOAD   ACTIVE   SUB  DESCRIPTION                                                                        
  blockdev@dev-disk-by\x2duuid-0e58bbe5\x2deff7\x2d4884\x2dbb5d\x2da0aac3d8a344.target loaded inactive dead Block Device Preparation for /dev/disk/by-uuid/0e58bbe5-eff7-4884-bb5d-a0aac3d8a344 
  blockdev@dev-disk-by\x2duuid-19CF\x2d0B54.target                                     loaded inactive dead Block Device Preparation for /dev/disk/by-uuid/19CF-0B54 
  blockdev@dev-disk-by\x2duuid-5605f149\x2d34a7\x2d4301\x2d9bf3\x2df1f177e35ed6.target loaded inactive dead Block Device Preparation for /dev/disk/by-uuid/5605f149-34a7-4301-9bf3-f1f177e35ed6 
  **blockdev@dev-disk-by\x2duuid-f5177cae\x2d4082\x2d44ed\x2d9471\x2db99030f06866.target** loaded inactive dead Block Device Preparation for /dev/disk/by-uuid/f5177cae-4082-44ed-9471-b99030f06866 
  blockdev@dev-nvme0n1p1.target                                                        loaded inactive dead Block Device Preparation for /dev/nvme0n1p1 
  blockdev@dev-nvme0n1p2.target                                                        loaded inactive dead Block Device Preparation for /dev/nvme0n1p2 
  blockdev@dev-sda1.target                                                             loaded inactive dead Block Device Preparation for /dev/sda1 
  blockdev@dev-sdb1.target                                                             loaded inactive dead Block Device Preparation for /dev/sdb1 

LOAD   = Reflects whether the unit definition was properly loaded. 
ACTIVE = The high-level unit activation state, i.e. generalization of SUB. 
SUB    = The low-level unit activation state, values depend on unit type. 
**8 loaded units listed.**
To show all installed unit files use 'systemctl list-unit-files'. 
**erlangen:~ #** 

Change to backup unit:

**erlangen:~ #** systemctl cat backup-home.service               
**# /etc/systemd/system/backup-home.service**
[Unit] 
**erlangen:~ #** systemctl cat backup-home.service                
**# /etc/systemd/system/backup-home.service**
[Unit] 
Description=Backup /home 
Requires=HDD-scan.service HDD.mount 
After=HDD-scan.service **blockdev@dev-disk-by\x2duuid-f5177cae\x2d4082\x2d44ed\x2d9471\x2db99030f06866.target** HDD.mount 

[Service] 
ExecStart=/usr/bin/rsync -a --exclude=.cache --exclude=covid-19-data /home/ /HDD/backup/home/ 
ExecStopPost=/usr/bin/umount /HDD 
ExecStopPost=/usr/local/bin/delete-backup-disk 
**erlangen:~ #**

Dependency on blockdev will cause backup-home.service wait for drive being ready:

**erlangen:~ #** journalctl -b -g sdb                                    
Apr 12 04:58:26 erlangen kernel: **sd 3:0:0:0: ****sdb****] 7814037168 512-byte logical blocks: (4.00 TB/3.64 TiB)**
Apr 12 04:58:26 erlangen kernel: **sd 3:0:0:0: ****sdb****] 4096-byte physical blocks**
Apr 12 04:58:26 erlangen kernel: **sd 3:0:0:0: ****sdb****] Write Protect is off**
Apr 12 04:58:26 erlangen kernel: sd 3:0:0:0: **sdb**] Mode Sense: 00 3a 00 00
Apr 12 04:58:26 erlangen kernel: **sd 3:0:0:0: ****sdb****] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA**
Apr 12 04:58:26 erlangen kernel:  sdb: sdb1 
Apr 12 04:58:26 erlangen kernel: **sd 3:0:0:0: ****sdb****] Attached SCSI disk**
Apr 12 04:58:28 erlangen hd-idle[766]:   disk: sdb timeout: 300 
Apr 12 04:58:28 erlangen kernel: **sd 3:0:0:0: ****sdb****] Synchronizing SCSI cache**
Apr 12 04:58:28 erlangen kernel: **sd 3:0:0:0: ****sdb****] Stopping disk**

Apr 12 06:36:14 erlangen kernel: **sd 3:0:0:0: ****sdb****] 7814037168 512-byte logical blocks: (4.00 TB/3.64 TiB)**
Apr 12 06:36:14 erlangen kernel: **sd 3:0:0:0: ****sdb****] 4096-byte physical blocks**
Apr 12 06:36:14 erlangen kernel: **sd 3:0:0:0: ****sdb****] Write Protect is off**
Apr 12 06:36:14 erlangen kernel: sd 3:0:0:0: **sdb**] Mode Sense: 00 3a 00 00
Apr 12 06:36:14 erlangen kernel: **sd 3:0:0:0: ****sdb****] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA**
Apr 12 06:36:23 erlangen kernel:  sdb: sdb1 
Apr 12 06:36:23 erlangen kernel: **sd 3:0:0:0: ****sdb****] Attached SCSI disk**
Apr 12 06:36:23 erlangen kernel: EXT4-fs (sdb1): mounted filesystem with ordered data mode. Quota mode: none. 
Apr 12 06:38:04 erlangen kernel: **Buffer I/O error on dev ****sdb****1, logical block 1057, async page read**
Apr 12 06:38:04 erlangen kernel: **Buffer I/O error on dev ****sdb****1, logical block 1057, async page read**
Apr 12 06:38:04 erlangen kernel: **sd 3:0:0:0: ****sdb****] Synchronizing SCSI cache**
Apr 12 06:38:04 erlangen kernel: **sd 3:0:0:0: ****sdb****] Stopping disk**
**erlangen:~ #**
**erlangen:~ #** journalctl -b -u backup-home.service -u HDD* 
Apr 12 04:58:28 erlangen systemd[1]: Started Disable backup HDD. 
Apr 12 04:58:29 erlangen systemd[1]: HDD-disable.service: Deactivated successfully. 

Apr 12 06:36:14 erlangen systemd[1]: Started Scan for backup HDD. 
Apr 12 06:36:14 erlangen systemd[1]: HDD-scan.service: Deactivated successfully. 
Apr 12 06:36:23 erlangen systemd[1]: Mounting /HDD... 
Apr 12 06:36:23 erlangen systemd[1]: Mounted /HDD. 
Apr 12 06:36:23 erlangen systemd[1]: Started Backup /home. 
Apr 12 06:38:03 erlangen systemd[1]: HDD.mount: Deactivated successfully. 
Apr 12 06:38:05 erlangen systemd[1]: backup-home.service: Deactivated successfully. 
Apr 12 06:38:05 erlangen systemd[1]: backup-home.service: Consumed 27.369s CPU time. 
**erlangen:~ #** 

Deactivation will occur only after successfully unmounting the drive and all data being written. Deactivation of the drive by daily invocation of service backup-home since 31-Mar-2022 left the file system intact:

**erlangen:~ #** fsck.ext4 -f /dev/sdb1 
e2fsck 1.46.5 (30-Dec-2021) 
Pass 1: Checking inodes, blocks, and sizes 
Pass 2: Checking directory structure 
Pass 3: Checking directory connectivity 
Pass 4: Checking reference counts 
Pass 5: Checking group summary information 
HDD: 1557985/244195328 files (0.7% non-contiguous), 576762356/976753920 blocks 
**erlangen:~ # **

Waiting for [noparse]blockdev@dev-disk-by\x2duuid-f5177cae\x2d4082\x2d44ed\x2d9471\x2db99030f06866.target[/noparse] doesn’t ensure the disk is ready. Looping until file “delete” shows up in sysfs fixes the issue:

**erlangen:~ #** cat /usr/local/bin/scan-for-backup-disk                
#!/bin/bash  
delete=/sys/devices/pci0000:00/0000:00:01.2/0000:01:00.1/ata5/host4/target4:0:0/4:0:0:0/delete 
scan=/sys/devices/pci0000:00/0000:00:01.2/0000:01:00.1/ata5/host4/scsi_host/host4/scan 
while true ; do 
        echo scanning for backup disk  
         -f $delete ] && exit 
        echo '- - -' > $scan 
done 
**erlangen:~ #**

**Program starter for KDE Plasma:
**

karl@erlangen:~> cat Schreibtisch/Scan\ disk.desktop  
[Desktop Entry] 
Comment[de_DE]= 
Comment= 
Exec=/usr/bin/systemctl start HDD-scan.service 
GenericName[de_DE]= 
GenericName= 
Icon=drive-harddisk-symbolic 
MimeType= 
Name[de_DE]=Scan disk 
Name=Scan disk 
Path= 
StartupNotify=true 
Terminal=false 
TerminalOptions= 
Type=Application 
X-DBUS-ServiceName= 
X-DBUS-StartupType= 
X-KDE-SubstituteUID=false 
X-KDE-Username= 
karl@erlangen:~> 

Granting permission:

**erlangen:~ #** cat /etc/polkit-1/rules.d/00-manage-HDD.rules  
// Allow karl to  manage HDD-scan.service; 
// fall back to implicit authorization otherwise. 
polkit.addRule(function(action, subject) { 
    if (action.id == "org.freedesktop.systemd1.manage-units" && 
        action.lookup("unit") == "HDD-scan.service" && 
        subject.user == "karl") { 
        return polkit.Result.YES; 
    } 
}); 
**erlangen:~ #**
**erlangen:~ #** journalctl -b _KERNEL_DEVICE=+scsi:4:0:0:0 --since 08:18 
Booting:
Apr 18 08:18:09 erlangen kernel: **scsi 4:0:0:0: Direct-Access     ATA      ST2000DM001-1CH1 CC29 PQ: 0 ANSI: 5**
Apr 18 08:18:09 erlangen kernel: **sd 4:0:0:0: Attached scsi generic sg2 type 0**
Apr 18 08:18:16 erlangen kernel: **sd 4:0:0:0: [sdc] 3907029168 512-byte logical blocks: (2.00 TB/1.82 TiB)**
Apr 18 08:18:16 erlangen kernel: **sd 4:0:0:0: [sdc] 4096-byte physical blocks**
Apr 18 08:18:16 erlangen kernel: **sd 4:0:0:0: [sdc] Write Protect is off**
Apr 18 08:18:16 erlangen kernel: sd 4:0:0:0: [sdc] Mode Sense: 00 3a 00 00
Apr 18 08:18:16 erlangen kernel: **sd 4:0:0:0: [sdc] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA**
Apr 18 08:18:16 erlangen kernel: **sd 4:0:0:0: [sdc] Attached SCSI disk**
Apr 18 08:18:49 erlangen kernel: **sd 4:0:0:0: [sdc] Synchronizing SCSI cache**
Apr 18 08:18:49 erlangen kernel: **sd 4:0:0:0: [sdc] Stopping disk**
Scanning:
Apr 18 09:00:59 erlangen kernel: **scsi 4:0:0:0: Direct-Access     ATA      ST2000DM001-1CH1 CC29 PQ: 0 ANSI: 5**
Apr 18 09:01:05 erlangen kernel: **sd 4:0:0:0: Attached scsi generic sg2 type 0**
Apr 18 09:01:06 erlangen kernel: **sd 4:0:0:0: [sdc] 3907029168 512-byte logical blocks: (2.00 TB/1.82 TiB)**
Apr 18 09:01:06 erlangen kernel: **sd 4:0:0:0: [sdc] 4096-byte physical blocks**
Apr 18 09:01:06 erlangen kernel: **sd 4:0:0:0: [sdc] Write Protect is off**
Apr 18 09:01:06 erlangen kernel: sd 4:0:0:0: [sdc] Mode Sense: 00 3a 00 00
Apr 18 09:01:06 erlangen kernel: **sd 4:0:0:0: [sdc] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA**
Apr 18 09:01:06 erlangen kernel: **sd 4:0:0:0: [sdc] Attached SCSI disk**
Disabling:
Apr 18 09:01:07 erlangen kernel: **sd 4:0:0:0: [sdc] Synchronizing SCSI cache**
Apr 18 09:01:07 erlangen kernel: **sd 4:0:0:0: [sdc] Stopping disk**
**erlangen:~ #** 

Further testing revealed that dealing with systemd ordering is cumbersome and spurious failures occur. Revised scripts have mounting and unmounting the drive included:

**erlangen:~ #** cat /usr/local/bin/scan-for-backup-disk                                           
#!/bin/bash  
echo "- - -" > /sys/devices/pci0000:00/0000:00:01.2/0000:04:00.1/ata1/host0/scsi_host/host0/scan 
until  -d /HDD/backup ] ; do echo Scanning backup disk ; sleep 1; done 
**erlangen:~ #** cat /usr/local/bin/disable-backup-disk                                           
#!/bin/bash 
delete=/sys/devices/pci0000:00/0000:00:01.2/0000:04:00.1/ata1/host0/target0:0:0/0:0:0:0/delete 
 -d /HDD/backup ] && umount /HDD || true 
 -f $delete ] && echo 1 > $delete || true 
**erlangen:~ #**

In the scripts you test the sub-directory /HDD/backup. In the case you change the name of this sub-directory the scripts will fail.
As my HDD is ext4 formatted, is it possible to use the sub-directory /lost+found instead?

This will work presumably. But it may create lots of confusion. Don’t do that. Create a mount point of your own liking.

Revised script relied on mounting as a side effect, which no longer occurs with kernel 5.18:

New version explicitly mounts the device:

**erlangen:~ #** cat /usr/local/bin/scan-for-backup-disk             
#!/bin/bash  
echo "- - -" > /sys/devices/pci0000:00/0000:00:01.2/0000:04:00.1/ata1/host0/scsi_host/host0/scan 
until  -L /dev/disk/by-uuid/f5177cae-4082-44ed-9471-b99030f06866 ] ; do echo Scanning backup disk ; sleep 1; done 
 -d /HDD/backup ] || mount /HDD || true 
**erlangen:~ #**

Hide partitions from udisks:

**erlangen:~ #** cat /etc/udev/rules.d/10-local.rules                       
SUBSYSTEM=="block", ENV{ID_FS_UUID}=="2260f160-cc05-47cc-9893-cc32c050177d", ENV{UDISKS_IGNORE}="1" 
SUBSYSTEM=="block", ENV{ID_FS_UUID}=="f5177cae-4082-44ed-9471-b99030f06866", ENV{UDISKS_IGNORE}="1" 
**erlangen:~ #**

https://wiki.archlinux.org/title/Udisks#Hide_selected_partitions