Score:3

I need to automate the task of formatting and mounting an SSD when Ubuntu 20.04 LTS starts up

us flag

I am using a local SSD in Google Cloud Platform, and the characteristic of the local SSD provided by Google is that the user has to re-format and mount it every time the user restarts the OS : https://cloud.google.com/compute/docs/disks/add-local-ssd#formatmultiple

I wanted to automate this because it's annoying having to do this manually every time the system restarts. So I created a script in /etc/init.d/automountssd.sh

#!bin/bash
sudo mdadm --create /dev/md0 --level=0 --raid-devices=4 /dev/nvme0n1 /dev/nvme0n2 /dev/nvme0n3 /dev/nvme0n4
sudo mkfs.ext4 -F /dev/md0
sudo mount /dev/md0 /root/Downloads/

Then assign permissions : chmod 775 /etc/init.d/automountssd.sh

And I registered it as autorun : update-rc.d automountssd.sh defaults

And after rebooting, nothing happened. :(

Solved : I followed alfajorcito's answer and this works perfectly on GCP platform.

First I followed his advice and changed #!bin/bash to #!/bin/bash. And register it as a service by doing the following : nano /etc/systemd/system/automountssd.service

[Unit]
Description=automount for GCP local ssd

[Service]
Type=oneshot
ExecStart=/etc/init.d/automountssd.sh
TimeoutStartSec=0

[Install]
WantedBy=multi-user.target

And Start the service automatically when booting, and run the service : sudo systemctl enable automountssd && sudo systemctl start automountssd

Now whenever the OS is booted, the SSD will be automatically formatted, Raid configured and mounted :)

Score:3
aw flag

Your shebang is missing a leading slash (i.e. it should be #!/bin/bash not #!bin/bash) - but apart from that, something else is off (unsure what exactly though) - I tried reproducing this with a simple shell script containing a single echo command, but it still didn't work...

However, I did manage to have the script automatically executed after reboot using systemd units, so perhaps you could try doing something similar? I don't have a GCP account, so I can't guarantee this would solve your local SSD problem, but the following worked on Ubuntu 20 on OCI:

# Linux version check
ubuntu@e2-micro-2:~$ uname -a
Linux e2-micro-2 5.11.0-1028-oracle #31~20.04.1-Ubuntu SMP Wed Jan 26 14:17:34 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

# script contents
ubuntu@e2-micro-2:~$ cat /etc/init.d/check-if-this-works.sh
#!/bin/bash
echo 'check' >> /tmp/checkme

# script permissions
ubuntu@e2-micro-2:~$ ls -l /etc/init.d/check-if-this-works.sh
-rwxrwxr-x 1 ubuntu ubuntu 41 Feb 19 08:26 /etc/init.d/check-if-this-works.sh

# systemd unit contents
ubuntu@e2-micro-2:~$ cat  /etc/systemd/system/checkme.service
[Unit]
Description=Check me

[Service]
Type=oneshot
ExecStart=/etc/init.d/check-if-this-works.sh
TimeoutStartSec=0

[Install]
WantedBy=multi-user.target

# confirm that output file is missing before reboot
ubuntu@e2-micro-2:~$ ls -l /tmp/checkme
ls: cannot access '/tmp/checkme': No such file or directory

# reload systemd daemon and enable service
ubuntu@e2-micro-2:~$ sudo systemctl daemon-reload
ubuntu@e2-micro-2:~$ sudo systemctl enable checkme

# reboot and check if script was successfully executed on startup
ubuntu@e2-micro-2:~$ sudo reboot
(ssh back to server)
ubuntu@e2-micro-2:~$ cat /tmp/checkme
check
us flag
Fantastic! working! As you said, I registered automountssd.sh as a service, and now the SSD is automatically formatted, raided and mounted at boot time. Thank you very much! XD
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.