树莓派增强稳定性

1
2
sudo raspi-config
"Advanced Options" -> "Memory Split" -> 16 "Boot Options" -> "Desktop / CLI" -> Console
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
sudo dd if=/dev/zero of=/mnt/swapfile bs=1M count=2048
sudo mkswap /mnt/swapfile
sudo swapon /mnt/swapfile
free
sudo nano /etc/fstab
追加:
/mnt/swapfile swap swap defaults 0 0
删除:
/mnt/swapfile swap swap defaults 0 0
sudo swapoff /mnt/swapfile
sudo rm /mnt/swapfile
sudo nano /etc/dphys-swapfile
修改 CONF_SWAPSIZE = 2048
sudo /etc/init.d/dphys-swapfile restart
free
1
2
3
4
5
sudo dpkg-reconfigure tzdata
->Asia->shanghai
date
sudo nano /etc/crontab
0 3 * * 2 root /sbin/shutdown -r now
1
2
3
4
5
6
7
8
9
10
sudo apt-get install chkconfig watchdog
sudo chkconfig watchdog on
sudo nano /etc/watchdog.conf
去掉 watchdog-device = /dev/watchdog 前的注释 #号,让他监控的设备指向 CPU 的硬件看门狗
去掉 max-load-1 = 24 前的注释 #号,当 1 分钟 load 进程超过 24 个的时候(一般 5 个就是超高负载了,更高可以认为是死机,这在遭遇 DDOS 攻击的时候很容易遇到)就触发重启)
还可以设置内存耗尽就重启,如 min-memory =1 前的注释 #号去掉
sudo /etc/init.d/watchdog start
sudo systemctl enable watchdog
可以运行一下 shell 炸弹 " :(){ ::&};: "测试一下能不能正常工作
如果看门狗奏效,树莓派会在 10 秒后重启~~ 然后又可以 ssh 连接了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/sh

#echo ===================== part 1, install tools ===============================
#sudo apt-get -y install rsync dosfstools parted kpartx exfat-fuse pv
#sudo fdisk -l
#mount -t ntfs-3g /dev/sda1 /mnt/u/
#echo ===================== part 2, choose USB ===============================
#mount USB device
usbmount=/mnt/u
#mkdir -p $usbmount
#if [ -z $1 ]; then
#echo "no argument, assume the mount device is /dev/sda1 ? Y/N"
#read key
#if [ "$key" = "y" -o "$key" = "Y" ]; then
#sudo mount -o uid=1000 /dev/sda1 $usbmount
#else
#echo "$0 [backup dest device name], e.g. $0 /dev/sda1"
#exit 0
#fi
#else
#sudo mount -o uid=1000 $1 $usbmount
#fi
#if [ -z "`grep $usbmount /etc/mtab`" ]; then
#echo "mount fail, exit now"
#exit 0
#fi

img=$usbmount/rpi-back-`date +%Y%m%d-%H%M`.img
echo "img:$img"

echo ===================== part 3, create a new blank img ===============================
# New img file
#sudo rm $img
bootsz=`df -P grep /boot awk '{print $2}'`
rootsz=`df -P grep /dev/root awk '{print $3}'`
totalsz=`echo $bootsz $rootsz awk '{print int(($1+$2)*1.3)}'`
echo "start create img,total size=${totalsz}K"
#sudo dd if=/dev/zero of=$img bs=1K count=$totalsz
pv -tpreb /dev/zero sudo dd of=$img bs=1K count=$totalsz

# format virtual disk
bootstart=`sudo fdisk -l /dev/mmcblk0 grep mmcblk0p1 awk '{print $2}'`
bootend=`sudo fdisk -l /dev/mmcblk0 grep mmcblk0p1 awk '{print $3}'`
rootstart=`sudo fdisk -l /dev/mmcblk0 grep mmcblk0p2 awk '{print $2}'`
echo "format virtual disk: boot: $bootstart >>> $bootend, root: $rootstart >>> end"
#rootend=`sudo fdisk -l /dev/mmcblk0 grep mmcblk0p2 awk '{print $3}'`
sudo parted $img --script -- mklabel msdos
sudo parted $img --script -- mkpart primary fat32 ${bootstart}s ${bootend}s
sudo parted $img --script -- mkpart primary ext4 ${rootstart}s -1
loopdevice=`sudo losetup -f --show $img`
device=/dev/mapper/`sudo kpartx -va $loopdevice sed -E 's/.*(loop[0-9])p.*/\1/g' head -1`
sleep 5
sudo mkfs.vfat ${device}p1 -n boot
sudo mkfs.ext4 ${device}p2
echo "format finish"

echo ===================== part 4, fill the data to img =========================
# mount partitions
mountb=$usbmount/backup_boot/
mountr=$usbmount/backup_root/
mkdir -p $mountb $mountr
# backup /boot
sudo mount -t vfat ${device}p1 $mountb
sudo cp -rfp /boot/* $mountb
sync
echo "...Boot partition done"
# backup /root
sudo mount -t ext4 ${device}p2 $mountr
if [ -f /etc/dphys-swapfile ]; then
SWAPFILE=`cat /etc/dphys-swapfile grep ^CONF_SWAPFILE cut -f 2 -d=`
if [ "$SWAPFILE" = "" ]; then
SWAPFILE=/var/swap
fi
EXCLUDE_SWAPFILE="--exclude $SWAPFILE"
fi
sudo rsync --force -rltWDEgop --delete --stats --progress \
$EXCLUDE_SWAPFILE \
--exclude '.gvfs' \
--exclude '/dev' \
--exclude '/media' \
--exclude '/mnt' \
--exclude '/proc' \
--exclude '/run' \
--exclude '/sys' \
--exclude '/tmp' \
--exclude 'lost\+found' \
--exclude '$usbmount' \
// $mountr
# special dirs
for i in dev media mnt proc run sys boot; do
if [ ! -d $mountr/$i ]; then
sudo mkdir $mountr/$i
fi
done
if [ ! -d $mountr/tmp ]; then
sudo mkdir $mountr/tmp
sudo chmod a+w $mountr/tmp
fi

#sudo rm -f $mountr/etc/udev/rules.d/70-persistent-net.rules

sync
ls -lia $mountr/home/pi/
echo "...Root partition done"
# if using the dump/restore
# tmp=$usbmount/root.ext4
# sudo chattr +d $img $mountb $mountr $tmp
# sudo mount -t ext4 ${device}p2 $mountr
# cd $mountr
# sudo dump -0uaf - / sudo restore -rf -
# cd


# replace PARTUUID
opartuuidb=`blkid -o export /dev/mmcblk0p1 grep PARTUUID`
opartuuidr=`blkid -o export /dev/mmcblk0p2 grep PARTUUID`
npartuuidb=`blkid -o export ${device}p1 grep PARTUUID`
npartuuidr=`blkid -o export ${device}p2 grep PARTUUID`
sudo sed -i "s/$opartuuidr/$npartuuidr/g" $mountb/cmdline.txt
sudo sed -i "s/$opartuuidb/$npartuuidb/g" $mountr/etc/fstab
sudo sed -i "s/$opartuuidr/$npartuuidr/g" $mountr/etc/fstab

sudo umount $mountb
sudo umount $mountr

1
2
3
4
5
sudo nano -K /etc/dhcpcd.conf
interface eth0
static ip_address=192.168.1.240/24
static routers=192.168.1.1
static domain_name_servers=114.114.114.114 8.8.8.8

树莓派增强稳定性
https://b.limour.top/133.html
Author
Limour
Posted on
May 31, 2020
Licensed under