-2

I have two types of computers, on one hard disk type is NVME while other is generic hard disk.

I have a script to format the disk and then clean /wipe the disk with Scrub.

sudo mkfs -F /dev/nvme0n1

sudo scrub /dev/nvme0n1

On generic hard disk (non-NVME disks), I have to modify the code as:

sudo mkfs -F /dev/sda

sudo scrub /dev/sda

These laptops may or may not contain an operating system by default.

I am trying to find a way to avoid having two scripts (1) for NVME (2) for standard drives.

In other words, I want to skip the "sda" or "nvme0n1" so script is general, can run on either disk types if it's possible somehow?

Update: 1st Nov 2023.

lsblk -d -o name
NAME
loop0
loop1
loop2
loop3
loop4
loop5
loop6
loop7
loop8
loop9
loop10
loop11
loop12
loop13
loop14
sda
sdb
nvme0n1 enter image description here

enter image description here

enter image description here

13
  • 1
    What do you mean by "disk 0" or "primary hard disk"? The disk that the system boots from? The disk(s) that contains the partition(s) mounted on /? The former may not be the same as the latter, and indeed the latter can be multiple disks.
    – muru
    Commented Oct 6, 2023 at 9:31
  • Use lsblk to list your devices - and use the ID provided there. My NVME drive is named nvme0n1. Commented Oct 6, 2023 at 9:34
  • Correct, the first disk /physical disk (disk 0 in Microsoft Windows). Instead of referring to sda or nvme0n1 I am looking for a more general approach (if possible), so I can modify the script. I am not asking how to get the device ID, rather asking how to write a script /reference in a script that work on both disk types. Commented Oct 6, 2023 at 9:36
  • 1
    Please edit your question and add the extra information. But, again, you need to explain what disk you are targeting. There is no concept of "primary", really, so which disk is it you want to look at? How would you choose it? Is this a script that will be run on a computer with no OS? With an OS? Is "primary" the disk which has the partition / is mounted on? What if this is an LVM? What of they are mounted remote partitions on a diskless server? Please edit your question and explain what you are trying to achieve here
    – terdon
    Commented Oct 6, 2023 at 12:30
  • 2
    I'm sorry but this still doesn't make sense. Why would you create a filesystem on /dev/sda. Do you perhaps mean /dev/sda1? What is the mkfs doing? I don't see an -F option, what does that do? What file system type are you trying to create? How do you know which disk you want to use? How do you know if you want nvme0n1 or nvme0n2 for example? Are you just looking for the / partition? If you want to generalize something, you need to tell us what the general principle is. So, again: how do you, as a human, choose the disk?
    – terdon
    Commented Oct 6, 2023 at 13:38

1 Answer 1

1

I assume you want to securely wipe the internal SSD/HDD of multiple computers after booting from an USB that runs the full version of Ubuntu. The full version allows installing additional software, while the live installation USB does not.

Step 1: Install nvme-cli

This utility will allow you to securely erase NVME drives without damaging them. Open a terminal and enter:

sudo apt install nvme-cli

Step 2: Write a bash script

Create a plain text file called squeakyclean in the folder $HOME/bin with the following content:

#!/bin/bash
# Purpose: find if there are any sata or nvme drives and take different 
# action on the each such drive
while IFS= read -r line; do
case $line in
    NAME | loop*)
    ;;
    sd?)
        echo "Found disk /dev/$line:"
        lsblk /dev/"$line"
        while true; do
        read -p "Do you want to scrub $line? (y/n) " yn < /dev/tty
            case $yn in
                y)
                    echo sudo scrub /dev/"$line"
                    echo " "
                    break
                ;;
                n)
                    echo "keeping $line"
                    echo " "
                    break
                ;;
                *) echo "invalid response";;
            esac
        done    
    ;;
    nvme[0-9]n[1-9])
        echo "Found disk /dev/$line:"
        lsblk /dev/"$line"
        while true; do
        read -p "Do you want to format $line? (y/n) " yn < /dev/tty
            case $yn in
                y)
                    echo sudo nvme format -s1 /dev/"$line"
                    echo " "
                    break
                ;;
                n)
                    echo "keeping $line"
                    echo " "
                    break
                ;;
                *) echo "invalid response";;
            esac
        done    
    ;;
    *)
        echo "$line found doing nothing"
    ;;      
    esac
done < <( lsblk -d -o name )
 

Save the file and make it executable.

Note: this assumes all the SATA drives are HDDs. You should probably use a different utility like hdparm for scrubbing SATA SSDs.

The Logic

First, the script uses the lsblk command to list all the internal and external disks. This list may look like:

NAME
sda
sdb
sdc
nvme0n1
nvme0n2
nvme0n3

Second, the script asks if you want to scrub the first SATA disk, sda. You have to enter either y or n. Then it does the same thing for the second SATA disk sdb etc.

Third, the script asks if you want to securely format the first NVMe disk called nvme0n1 if it exists. You have to enter either y or n. Then it does the same thing for the second NVMe disk nvme0n1 etc.

Finally, for any other disks found, it lists them.

Test

Run the script as and see what it outputs. This will tell you which disks it has found and which will be scrubbed. If your USB drive is recognized as /dev/sda then you have to change the script so that it does not erase your USB drive.

Step 3: Execute!

When you are ready, remove the echo before sudo in the two lines of the script.

Warning: Executing this script will delete everything in your computer!

Boot the computer you want to clean using the USB with the full install of Ubuntu.

Open a terminal and type squeakyclean. You will be asked to enter your sudo password when needed.

Hope this helps

12
  • THANKS. Your script did the work. Now I can modify my script to to use NVME-CLI for NVM disks and hdparm or shred for HDDs. (In past I was maintaining different files for NVME and HDDs). Commented Oct 9, 2023 at 10:35
  • 1
    @user1737015 I have added added a Y/N confirmation so that it does not erase the drive you don't want to erase.
    – user68186
    Commented Oct 31, 2023 at 16:54
  • 1
    @user1737015 Sorry about that. Fixed it.
    – user68186
    Commented Oct 31, 2023 at 20:53
  • 1
    @user1737015 I edited the code so that loop devices are ignored. Now I know why the October 20 version didn't work. This version should be generic enough for any PC.
    – user68186
    Commented Nov 1, 2023 at 10:31
  • 1
    @user1737015 I don't know how to take into account for the card reader slot. It is difficult to write a script that will work with all possible combinations of USB, car reader, HDD, SSD, etc. I think the Y/N approach is better.
    – user68186
    Commented Nov 1, 2023 at 17:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .