Unattended-Linux

Did you ever have the need to have a bootable USB system which performs some activities after boot than shutdown the computer? I haven't found one so I built a simple one myself and Im sharing it now so there might be someone else interested.

Take it from my Git repo and build yourself: GitHub - rezo552/unattended-linux: Minimal Linux ISO to run scripts unattended

In the supportFiles you will find an autostart script, you can place your code directly into that file if you need to run it in an isolated environment. If the machine has internet access where you are planning to use the USB drive, you may consider to use my autostart script included in the repo. This way you can have a single USB drive but run different script on different machines. How does it work? In the autostart script you need to configure your own private repo where you store the machine specific startup scripts. During the start the autostart script will download it from your private repo and execute it. Note that in order to access a private repo you need to create a Github API token which can be a classic one or a fine-grained (recommended) one. Make sure you update the autostart script with your token and repo information before building it.

#!/usr/bin/bash
board_serial=$(dmidecode -t baseboard | grep 'Serial Number' | awk '{print $3}')
token="INSERT token here"
git_repo_folder="https://api.github.com/repos/<username>/<repo>/contents/"

curl -H "Authorization: token $token" \
  -H 'Accept: application/vnd.github.v3.raw' \
  -o /root/$board_serial \
  -L $git_repo_folder$board_serial

chmod +x /root/$board_serial
/root/$board_serial        

As you can see from the above script it is searching for a file on your private repo which is matching your mainboard serial number. If you do not know how to look it up, you should just simply boot up the machine and check it with the following command:

dmidecode -t baseboard | grep 'Serial Number'        

Create a file on your private repo with the same name and put your script you wish to run inside it. For example this script is taking a backup from /dev/sda then send a notification via Telegram and finally shutdown the machine:

#!/usr/bin/bash
sleep 10
echo "Starting autoscript" > /dev/console
telegram_bot_token="xxxxxxxxxxxxxxxxx"
chat_id="xxxxxxxxxxxx"

echo "Mounting samba share" > /dev/console
mount -t cifs //xxxxxxxxxxxxx/Media /mnt -o username=xxxxxx,password=xxxx

echo "Backing up /dev/sda" > /dev/console
current_date=$(date +"%Y-%m-%d")
dd if=/dev/sda bs=32M | gzip -c > "/mnt/Backup/backup_${current_date}.tar.gz" && backup="Backup Successful" || backup="Backup Failed"

curl -s -X POST "https://api.telegram.org/bot${telegram_bot_token}/sendMessage" -d "chat_id=${chat_id}&text=${backup}"

echo "Shutting down..." > /dev/console
sleep 5 
umount /mnt
shutdown now -h        

The overall Linux ISO is lightweight (cca. 300 Mb) and boots up quickly...



要查看或添加评论,请登录

社区洞察

其他会员也浏览了