ITCooky Recipes

Lets cooky it yammy things!

Home server on Raspberry Pi 4 with NVMe, RTC, UPS in a DIY case!

дата October 22, 2020

CAUTION: From my experience, it turns out that if you install the operating system on USB and boot Raspberry OS from it, an accidental shutdown very quickly kills the USB disk, on the second or third time!
that’s how died
1Gb noname mSATA on SupTronics X856 v1.0
256Gb KingSpec M.2 NVMe on Blueendless SSD M2 PCIe
x725fro no reaone shut down after 30-60 hours of operation power ans starts to discharge batterys.

What a home server needs is a fault tolerant power supply, a large and fast disk, good cooling, small size, mobility, well, to put it where there is at least power! This is what I will do: power supply through the X725 with 18650 batteries, the NVMe disk is excessively fast for USB3, the best cooling! And a sleek little case – there are no universal Raspberry pi cases, for all the stuffing, but you can take a standard one and tweak it!


PC Raspberry Pi 4
I’ll check what kind of model I have
cat /sys/firmware/devicetree/base/model

Raspberry Pi 4 Model B Rev 1.1

Let’s see how much memory
free -h

              total        used        free      shared  buff/cache   available
Mem:          3.8Gi        90Mi       3.5Gi       8.0Mi       242Mi       3.6Gi
Swap:          99Mi          0B        99Mi

4Gb!

Cooler for Raspberry Pi
I take a cooler Low Profile Ice Tower Cooler SKU: EP-0112, why thie one i described here in details Testing 52Pi ICE Tower coolers for Raspberry Pi 4!.

Clock for Raspberry Pi
I have a pretty old one – DS3231 RTC

The watch contains a LIR2032 rechargeable battery? it can recharge, but I don’t know how to check this

UPS for Raspberry Pi
I got that SupTronics X725 Power Management Board, do not buy it, there is already a newer model, and in this one of the declared functions only the power supply and shutdown work: it can be turned on with any touch. Here I tested it in more detail X725 Power Management Board bad shield of 18650 batteries for Raspberry Pi!

Power button for X725
Momentary Self-Reset Power Control Switch

Power supply
This one! It has a round connector, and connects to the x725

USB disc for Raspberry Pi
At first I had mSATA on shield SupTronics X856 and it worked perfectly in test assembly, but then all of a sudden the speed dropped down 100 times, and it started to shut down

R.I.P
SupTronics X856 or noname 1Tb mSATA

Urgent replacement Blueendless SSD M2 PCIe NVMe/NGFF Type C USB 3,1 and 256Gb KingSpec M.2 NVMe SSD NE Series 2280 PCIE Gen3x4 I also tested them in detail here Which USB drive choose for Raspberry Pi, to boot the operating system, on mSATA or NVMe

Hope that change is an advantage as Wi-Fi malfunctions because SupTronics X856 interferes with Wi-Fi on Raspberry Pi. 2.4Mgz does not fully work with it, it is not possible to connect to the AP, and 5Mhz can partially work.

DIY case for Raspberry Pi
1. Iron electrical distribution box metal box DIY amplifier 150 * 140 * 70mm
2. Extension cord Cat6 RJ45
3. USB 2,0 Male to female extension cable with set screw
4. Power Cord Charger Adapter 1.2m DC Jack Tip Connector Cable Cord Power Supply for Laptop 5,5X2,5
5. DC099 power connector 5,5 мм x 2,1 мм,

Case DIY – Drill It Yourself

I drill holes for RPI and shield in the metal part of the box. I regretted not puting them closer to the cover, these two millimeters would have helped me a lot on the back cover! It is convenient to drill holes with a drill cordless screwdriver, you just have to remember that it can rotate the drill in both directions … you can still drill holes, but if the drill rotates clockwise, then drilling is much easier and faster! Before drilling, I nailed the holes, this must be done with caution, otherwise the metal will immediately bend. Port locations are thought of with guts and cables in mind. USB cables can be inserted below x725, below RPI you could enter USB NVMe, but literally a couple of millimeters are missing, the USB connector is very long!

The plastic cover has markings on the inside, which helps make holes for the ports. I used a screwdriver and a dremel with a cutter.

The power connector is soldered to the power cord. In the center is red positive, on the side negative black; there is a hint about how to connect on the power supply. On USB cables I cut the band protection on the connectors, otherwise they would not fit

Connection diagram

x725 and RPI are connected with ordinary Arduino cables, the first 7 pins (two rows on the power side). A couple times I made a mistake and my cables melted … but RPI and x725 (worked so bad before) survived! I connect the DS3231 RTC clock to the x725, only on the pins joined with the RPI. Attach the front panel at the last moment, connect the button to the corresponding pins on x725

There is a terrible mix of wires inside, but everything fits! Above is USB NVMe. The CPU fan was put into operation to blow outside, there are holes in the case right next to it! At 3.3 it is quite weak, but it blows, but quiet!

Outside everything looks pretty decent and beautiful, I took a red case especially for the raspberry!

To turn on, press and release button. To turn it off, press and hold it for 3-4 seconds, when it starts to flash then the shutdown process has started If you keep it for a long time, it will forcibly shut down. Press it for 1-2 seconds and it will restart, this all works only if the scripts for x725 are installed.

Initial installation to SD card
You need to install Raspberry Pi OS (32-bit) Lite (formerly known as Raspbian) in this version there is no desktop environment.
I download the latest version here www.raspberrypi.org/downloads/raspbian/
And here I download the installer https://www.raspberrypi.org/downloads/

For ssh to work, in BOOT section SD card you need to create empty file called ssh and it will work after that. You can already insert the card into the raspberry, connect the ethernet cable, turn on and see which IP you got, I looked at the router, you can go to the raspberry with your hands and look with the command
ifconfig
name ssh: pi
password: raspberry

Boot from NVMe via USB
We are still in SD until now.
See what firmware is now
vcgencmd bootloader_version

Apr 16 2020 18:11:26
version a5e1b95f320810c69441557c5f5f0a7f2460dfb8 (release)
timestamp 1587057086

It is not the last, lets update
sudo apt update
sudo apt full-upgrade
sudo rpi-eeprom-update
sudo rpi-eeprom-update -a

You can do it manually, but only after updating everything. A new firmware should appear and you need to update it, for this we change the file
sudo vi /etc/default/rpi-eeprom-update
word critical to stable

see what firmware is in
ls /lib/firmware/raspberrypi/bootloader/stable/
we install the lastpieeprom-2020-09-03.bin
sudo rpi-eeprom-update -d -f /lib/firmware/raspberrypi/bootloader/stable/pieeprom-2020-09-03.bin

Restart
Look
vcgencmd bootloader_version

Sep  3 2020 13:11:43
version c305221a6d7e532693cc7ff57fddfc8649def167 (release)
timestamp 1599135103

Now is the last

NVMe is connected to USB 3 and is defined as sda
We somehow download and put the last image on the raspberry2020-08-20-raspios-buster-armhf-lite.zip there is no direct link on the site so somehow!
unzip 2020-08-20-raspios-buster-armhf-lite.zip

We write this image in NVMe
sudo dd bs=4M if=2020-08-20-raspios-buster-armhf-lite.img of=/dev/sda conv=fsync

Mount the first partition in a folder, for example /mnt/usb

sudo mkdir /mnt/usb
sudo mount /dev/sda1 /mnt/usb

here we create a file for ssh
sudo touch /mnt/usb/ssh

After that, turn off the raspberry, take out the SD card, and turn on!
Everything is miraculously working on USB drive

Lets make the second partition and encrypt it
If you don’t need the second paretition, don’t do it! I need it to store all the data there and suddenly not clog the whole disk with it. I will also encrypt it, because there will be personal information, backups, passwords, etc.

Boot from SD again. Linux OS is files, and we just need to copy all the files from the main partition to make a copy or transfer the whole OS to a new disk!

make the necessary folders
sudo mkdir /mnt/nvme
sudo mkdir /home/pi/backup/

I have the desired partition determined as sda2 mount it
sudo mount /dev/sda2 /mnt/nvme
and copy all files
sudo cp -rp /mnt/nvme/* /home/pi/backup/
sudo umount /dev/sda2

IMPORTANT: You must copy it with the preservation of all rights and attributes, therefore, in the command its p

to see discs we have
sudo fdisk -lu
NVMe Information

Disk /dev/sda: 238.5 GiB, 256060514304 bytes, 500118192 sectors
Disk model:                 
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0xcf443634

Device     Boot  Start       End   Sectors   Size Id Type
/dev/sda1         8192    532479    524288   256M  c W95 FAT32 (LBA)
/dev/sda2       532480 500118191 499585712 238.2G 83 Linux

We don’t touch the first section, but we remove sda2 and we will do two sections on it!

We use fdisk
sudo fdisk /dev/sda
Something strange, but it cannot be used by commands, only interactive mode
to delete the second section, press enter each time
d
2
w
enter

Again
sudo fdisk /dev/sda
To create a new second partition you need to know where the first one ends, this can be seen in sudo fdisk -lu normally fdisk has to resolve it, but not this time. Add one to this figure and 532480 will be the beginning of the second section, and 64013096 will be the end, which means that the size of this section will be about 30GB
n
p
2
532480
64013096

check

Disk /dev/sda: 238.5 GiB, 256060514304 bytes, 500118192 sectors
Disk model:                 
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0xcf443634

Device     Boot  Start      End  Sectors  Size Id Type
/dev/sda1         8192   532479   524288  256M  c W95 FAT32 (LBA)
/dev/sda2       532480 64013096 63480617 30.3G 83 Linux

Filesystem/RAID signature on partition 2 will be wiped.

And we do the third section, here also the numbers, we just don’t indicate the end, we use everything that is
n
p
3
64013097
enter

To apply the changes
w

Make a file system
sudo mkfs -t ext4 /dev/sda2
You also need to immediately create a filesystem on the third partition, otherwise the raspberry after loading will try to expand and take it too (as it seems to me)
sudo mkfs -t ext4 /dev/sda3

Mount
sudo mount /dev/sda2 /mnt/nvme
and copy all files
sudo cp -rp /home/pi/backup/* /mnt/nvme/

I turn it off, take out the SD card, turn it on and now we have two sections!

Secure data storage on Raspberry Pi
You can encrypt the entire disk, but you must always enter the password when booting. Drawback, so I will only encrypt one partition. This will protect the data it contains from direct reading; If there is no encryption at some point, you can connect the drive to another PC and read all the data. Here, too, you will need to enter a password, but at the time of mountind and not at boot.

Updating if not up to date
sudo apt update
sudo apt full-upgrade

Install what is needed for encryption
sudo apt-get install cryptsetup
sudo modprobe dm-crypt sha256 aes

I am creating an encryption section, here you will have to create a password
sudo cryptsetup --verify-passphrase luksFormat /dev/sda3 -c aes -s 256 -h sha256
initialize the encryption section, here you must enter the password
sudo cryptsetup luksOpen /dev/sda3 mYdata

And I create a file system, also creating file table. Otherwise, if you just create it, tables will be created when you mount it; in the case of an encrypted partition this creates a huge incomprehensible load on the system
sudo mkfs -t ext4 -E lazy_itable_init=0,lazy_journal_init=0 /dev/mapper/mYdata

Mount

sudo mkdir /mnt/nvme
sudo mount /dev/mapper/mYdata /mnt/nvme

Encryption, of course, loads the processor and the writing speed drops.

I measured the file creation speed.
sudo dd if=/dev/zero of=test.file count=10000 bs=1048576
Speeds were 200 to 70 mb per second, these results are not clear to me …

Uninterruptible Power Supply (UPS) for Raspberry Pi
I have X725 Power Management Board do not buy that plate, the manufacturer is already selling a new x728 model; it should be better, even if half the declared functions work on it, then it is definitely better than the x725.

On the Raspberry Pi, you need to enable I2C, everything is done through the menu, I’ll do it through the console.

You need to edit the file
sudo vi /boot/config.txt
uncomment lines

dtparam=i2c_arm=on
dtparam=i2s=on

And also to the file
sudo vi /etc/modules
add

i2c-dev
i2c-bcm2835

Install
sudo apt-get install -y python-smbus i2c-tools

Now according to the manufacturer’s instructionswww.suptronics.com/miniPCkits/x725-software.html i do

wget https://raw.githubusercontent.com/suptronics/x750_x725-script/master/x750.sh
sudo bash x750.sh
printf "%s\n" "alias x750off='sudo x750shutdown.sh'" >> ~/.bashrc
sudo reboot

Rubn script
sudo python /home/pi/x750ups.py
this shows the voltage and the percentage of load, the result is constantly displayed on the console

******************
Voltage: 4.22V
Battery:  104%
******************
******************
Voltage: 4.22V
Battery:  104%
******************

And this command suddenly turns off the raspberry and x725
x750off
from the console it works from scripts no, but you can turn it off like this
sudo /usr/local/bin/x750shutdown.sh

I only need to get the voltage value once so I remove everything unnecessary to create a script
sudo vi /home/pi/x725bv.py

code

#!/usr/bin/env python
import struct
import smbus
import sys
import time


def readVoltage(bus):

     address = 0x36
     read = bus.read_word_data(address, 2)
     swapped = struct.unpack("<H", struct.pack(">H", read))[0]
     voltage = swapped * 1.25 /1000/16
     return voltage



bus = smbus.SMBus(1) # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)

while True:
 print ("{0}".format(readVoltage(bus))) 
 exit()

And bash script, which shuts down everything when the battery voltage is lower than 3.5, this is about two hours of running the raspberry under load
vi /home/pi/batteryoff.sh
code

#!/bin/bash
a=`sudo python /home/pi/x725bv.py`
compare=`echo "$a>3.5" | bc`
if [ $compare -eq 0 ]
then
sudo /usr/local/bin/x750shutdown.sh
fi

This will be a hardware shutdown … not the best option … since the x725 button shuts down differently

To run this scrit once a minute, add
sudo crontab -e
line

* * * * * sh /home/pi/batteryoff.sh

Static IP
uncomment the lines in the file
sudo vi /etc/dhcpcd.conf
and write our own IP from your subnet (not in the DHCP range), and a router and DNS

interface eth0
static ip_address=192.168.1.11/24
static routers=192.168.1.1
static domain_name_servers=8.8.8.8

Lets connect to Wi-Fi
Unblock Wi-Fi
sudo rfkill unblock 0

Let’s see the networks
sudo iwlist wlan0 scan

Add to archive
sudo vi /etc/wpa_supplicant/wpa_supplicant.conf
this with your network name and password

network={
    ssid="ITCOOKY_CEO_B"
    psk="passwrod"
}

Restart Wi-Fi
wpa_cli -i wlan0 reconfigure
And after 20 seconds, we see the connection
ifconfig

Connect clock ds3231 to Raspberry Pi
add
sudo vi /boot/config.txt
line

dtoverlay=i2c-rtc,ds3231

Remove a fake clock
sudo apt-get purge fake-hwclock
Setting the time
sudo dpkg-reconfigure tzdata

Reboot

to see what time it is on the clock
sudo hwclock -r

This how system time can be recorded in the clock.
sudo hwclock -w

To make the system take time from the clock on boot, I add to
sudo vi /etc/rc.local
before exit 0 you have to add

/sbin/hwclock -s

this should write the time from the RTC to the system on boot

Lets check if it works, disable time synchronization
sudo systemctl disable systemd-timesyncd
I turn off the raspberry and turn it on, the ds3231 clock has worked!

I re-enable time synchronization, as I understand it only works over the Internet
sudo systemctl enable systemd-timesyncd

Well, that is it, very long and there is still pending a system to monitor the status of the server with warnings …

UPD: Programmatically shutting down the Raspberry Pi and X725 Power Management Board
There is a problem with X725 Power Management Board there is a script that turns it off only itself, but not to Raspberry Pi!

After installing the manufacturer script, it appears here/usr/local/bin/x750shutdown.sh it sends 1 to 18 pins and the X725 shuts down, cutting power to the Raspberry Pi, which is not good … which is very bad!

Second script /etc/x750pwr.sh it sends a signal 1 to pin 17 and listens for a button press on pin 4, so when the button is pressed long enough it initiates the Raspberry Pi shutdown, and the x725 shuts down when the signal at 17 disappears, which means the raspberry went off, this is a super correct shutdown! The problem is that you cannot send signal 1 to pin 4 from another pin on the raspberry to initiate this shutdown programmatically (the raspberry shuts down but the X725 doesn’t) … you have to push the button somehow, this can be done! do with a transistor!

I take the 2N3904 transistor and connect it according to the schematic

I am confused about transistors, so follow the picture I solder the red wire from the transistor to the red one from the button, on the button itself and not like in the diagram.
On the Raspberry the transistor is connected to pin 27 and to ground, in the diagram these pins are thrown to X725.

And you need to press the button for only 3-6 seconds, if pressing 8 it will forcibly turn off all. So we make a script
sudo vi bshut.sh
code

#!/bin/bash
shdown=27
echo "$shdown" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio$shdown/direction
echo "1" > /sys/class/gpio/gpio$shdown/value
shtdownstart=$(date +%s%N | cut -b1-13)
while true; do
if [ $(($(date +%s%N | cut -b1-13)-$shtdownstart)) -gt 600 ]; then
exit
fi
done

Run
sudo bash ./bshut.sh

And the normal shutdown works like from a button!


One Response to “Home server on Raspberry Pi 4 with NVMe, RTC, UPS in a DIY case!”

  1. […] Integration with my DIY case UPS HAT V2 insert in case from this item Home server on Raspberry Pi 4 with NVMe, RTC, UPS in a DIY case! […]

Leave a Reply

Your email address will not be published. Required fields are marked *