--- title: 树莓派增强稳定性 tags: - raspberrypi - 稳定 id: '133' categories: - - Raspberry Pi date: 2020-05-31 14:18:40 --- ``` sudo raspi-config "Advanced Options" -> "Memory Split" -> 16 "Boot Options" -> "Desktop / CLI" -> Console ``` ``` 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 ``` ``` sudo dpkg-reconfigure tzdata ->Asia->shanghai date sudo nano /etc/crontab 0 3 * * 2 root /sbin/shutdown -r now ``` ``` 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 连接了。 ``` ``` #!/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 ``` ``` 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 ```