pve/back/rebuild_qcow2.sh

151 lines
12 KiB
Bash
Raw Normal View History

#!/bin/bash
2023-05-03 17:14:44 +08:00
# from https://github.com/spiritLHLS/pve
if ! command -v virt-customize &> /dev/null
then
echo "virt-customize not found, installing libguestfs-tools"
sudo apt-get update
sudo apt-get install -y libguestfs-tools
2023-04-10 15:19:09 +08:00
sudo apt-get install -y libguestfs-tools --fix-missing
fi
if ! command -v rngd &> /dev/null
then
echo "rng-tools not found, installing rng-tools"
sudo apt-get update
sudo apt-get install -y rng-tools
2023-04-10 15:19:09 +08:00
sudo apt-get install -y rng-tools --fix-missing
fi
2023-04-09 11:46:57 +08:00
# export LIBGUESTFS_DEBUG=1 LIBGUESTFS_TRACE=1
qcow_file=$1
echo "转换文件$qcow_file中......"
2023-05-03 17:06:15 +08:00
# if [[ "$qcow_file" == *"alpine"* ]]; then
# virt-sysprep --enable alpine -a "$qcow_file"
# elif [[ "$qcow_file" == *"centos"* ]]; then
# virt-sysprep --enable centos -a "$qcow_file"
# elif [[ "$qcow_file" == *"almalinux"* ]]; then
# virt-sysprep --enable almalinux -a "$qcow_file"
# fi
2023-05-03 17:05:41 +08:00
if [[ "$qcow_file" == *"debian"* || "$qcow_file" == *"ubuntu"* || "$qcow_file" == *"arch"* ]]; then
virt-customize -a $qcow_file --run-command "sed -i 's/ssh_pwauth:[[:space:]]*0/ssh_pwauth: 1/g' /etc/cloud/cloud.cfg"
2023-05-03 13:33:29 +08:00
virt-customize -a $qcow_file --run-command "echo 'Modified from https://github.com/spiritLHLS/Images' >> /etc/motd"
virt-customize -a $qcow_file --run-command "echo 'Related repo https://github.com/spiritLHLS/pve' >> /etc/motd"
virt-customize -a $qcow_file --run-command "echo '--by https://t.me/spiritlhl' >> /etc/motd"
echo "启用SSH功能..."
virt-customize -a $qcow_file --run-command "systemctl enable ssh"
virt-customize -a $qcow_file --run-command "systemctl start ssh"
virt-customize -a $qcow_file --run-command "systemctl enable sshd"
virt-customize -a $qcow_file --run-command "systemctl start sshd"
echo "启用root登录..."
virt-customize -a $qcow_file --run-command "sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config"
2023-05-03 16:03:56 +08:00
virt-customize -a $qcow_file --run-command "sed -i 's/#PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config"
2023-05-03 13:33:29 +08:00
virt-customize -a $qcow_file --run-command "sed -i 's/#Port 22/Port 22/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "sed -i 's/#AddressFamily any/AddressFamily any/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "sed -i 's/#ListenAddress 0.0.0.0/ListenAddress 0.0.0.0/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "sed -i 's/#ListenAddress ::/ListenAddress ::/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "service ssh restart"
virt-customize -a $qcow_file --run-command "service sshd restart"
virt-customize -a $qcow_file --run-command "systemctl restart sshd"
virt-customize -a $qcow_file --run-command "systemctl restart ssh"
elif [[ "$qcow_file" == *"alpine"* ]]; then
2023-05-03 17:14:44 +08:00
virt-customize -a $qcow_file --run-command "sed -i 's/disable_root:[[:space:]]*1/disable_root: 0/g' /etc/cloud/cloud.cfg"
2023-05-03 17:05:41 +08:00
virt-customize -a $qcow_file --run-command "sed -i 's/ssh_pwauth:[[:space:]]*0/ssh_pwauth: 1/g' /etc/cloud/cloud.cfg"
2023-05-03 13:33:29 +08:00
virt-customize -a $qcow_file --run-command "echo 'Modified from https://github.com/spiritLHLS/Images' >> /etc/motd"
virt-customize -a $qcow_file --run-command "echo 'Related repo https://github.com/spiritLHLS/pve' >> /etc/motd"
virt-customize -a $qcow_file --run-command "echo '--by https://t.me/spiritlhl' >> /etc/motd"
echo "启用SSH功能..."
2023-05-03 17:05:41 +08:00
# virt-customize -a $qcow_file --run-command "apk update"
# virt-customize -a $qcow_file --run-command "apk add --no-cache wget curl openssh-server sshpass"
# virt-customize -a $qcow_file --run-command "cd /etc/ssh"
# virt-customize -a $qcow_file --run-command "ssh-keygen -A"
2023-05-03 13:33:29 +08:00
echo "启用root登录..."
2023-05-03 17:05:41 +08:00
# virt-customize -a $qcow_file --run-command "sed -i.bak '/^#PermitRootLogin/c PermitRootLogin yes' /etc/ssh/sshd_config"
# virt-customize -a $qcow_file --run-command "sed -i.bak '/^#PasswordAuthentication/c PasswordAuthentication yes' /etc/ssh/sshd_config"
# virt-customize -a $qcow_file --run-command "sed -i.bak '/^#ListenAddress/c ListenAddress 0.0.0.0' /etc/ssh/sshd_config"
# virt-customize -a $qcow_file --run-command "sed -i.bak '/^#AddressFamily/c AddressFamily any' /etc/ssh/sshd_config"
# virt-customize -a $qcow_file --run-command "sed -i.bak 's/^#\?Port.*/Port 22/' /etc/ssh/sshd_config"
# virt-customize -a $qcow_file --run-command "/usr/sbin/sshd"
elif [[ "$qcow_file" == *"almalinux9"* ]]; then
2023-05-03 17:14:44 +08:00
virt-customize -a $qcow_file --run-command "sed -i 's/ssh_pwauth:[[:space:]]*0/ssh_pwauth: 1/g' /etc/cloud/cloud.cfg"
2023-05-03 17:05:41 +08:00
virt-customize -a $qcow_file --run-command "echo 'Modified from https://github.com/spiritLHLS/Images' >> /etc/motd"
virt-customize -a $qcow_file --run-command "echo 'Related repo https://github.com/spiritLHLS/pve' >> /etc/motd"
virt-customize -a $qcow_file --run-command "echo '--by https://t.me/spiritlhl' >> /etc/motd"
echo "启用SSH功能..."
virt-customize -a $qcow_file --run-command "systemctl enable ssh"
virt-customize -a $qcow_file --run-command "systemctl start ssh"
virt-customize -a $qcow_file --run-command "systemctl enable sshd"
virt-customize -a $qcow_file --run-command "systemctl start sshd"
echo "启用root登录..."
virt-customize -a $qcow_file --run-command "sed -i 's/ssh_pwauth:[[:space:]]*0/ssh_pwauth: 1/g' /etc/cloud/cloud.cfg"
virt-customize -a $qcow_file --run-command "sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "sed -i 's/#PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication yes/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "sed -i 's/^ChallengeResponseAuthentication no/ChallengeResponseAuthentication yes/g' /etc/ssh/sshd_config.d/50-redhat.conf"
virt-customize -a $qcow_file --run-command "sed -i 's/#Port 22/Port 22/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "sed -i 's/#AddressFamily any/AddressFamily any/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "sed -i 's/#ListenAddress 0.0.0.0/ListenAddress 0.0.0.0/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "sed -i 's/#ListenAddress ::/ListenAddress ::/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "service ssh restart"
virt-customize -a $qcow_file --run-command "service sshd restart"
virt-customize -a $qcow_file --run-command "systemctl restart sshd"
virt-customize -a $qcow_file --run-command "systemctl restart ssh"
elif [[ "$qcow_file" == *"almalinux"* || "$qcow_file" == *"centos9-stream"* || "$qcow_file" == *"centos8-stream"* || "$qcow_file" == *"centos7"* ]]; then
virt-customize -a $qcow_file --run-command "sed -i 's/ssh_pwauth:[[:space:]]*0/ssh_pwauth: 1/g' /etc/cloud/cloud.cfg"
virt-customize -a $qcow_file --run-command "echo 'Modified from https://github.com/spiritLHLS/Images' >> /etc/motd"
virt-customize -a $qcow_file --run-command "echo 'Related repo https://github.com/spiritLHLS/pve' >> /etc/motd"
virt-customize -a $qcow_file --run-command "echo '--by https://t.me/spiritlhl' >> /etc/motd"
echo "启用SSH功能..."
virt-customize -a $qcow_file --run-command "systemctl enable ssh"
virt-customize -a $qcow_file --run-command "systemctl start ssh"
virt-customize -a $qcow_file --run-command "systemctl enable sshd"
virt-customize -a $qcow_file --run-command "systemctl start sshd"
echo "启用root登录..."
virt-customize -a $qcow_file --run-command "sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "sed -i 's/#PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication yes/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "sed -i 's/^ChallengeResponseAuthentication no/ChallengeResponseAuthentication yes/g' /etc/ssh/sshd_config.d/50-redhat.conf"
virt-customize -a $qcow_file --run-command "sed -i 's/#Port 22/Port 22/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "sed -i 's/#AddressFamily any/AddressFamily any/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "sed -i 's/#ListenAddress 0.0.0.0/ListenAddress 0.0.0.0/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "sed -i 's/#ListenAddress ::/ListenAddress ::/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "service ssh restart"
virt-customize -a $qcow_file --run-command "service sshd restart"
virt-customize -a $qcow_file --run-command "systemctl restart sshd"
virt-customize -a $qcow_file --run-command "systemctl restart ssh"
else
2023-05-03 17:14:44 +08:00
virt-customize -a $qcow_file --run-command "sed -i 's/disable_root:[[:space:]]*1/disable_root: 0/g' /etc/cloud/cloud.cfg"
2023-05-03 17:05:41 +08:00
virt-customize -a $qcow_file --run-command "sed -i 's/ssh_pwauth:[[:space:]]*0/ssh_pwauth: 1/g' /etc/cloud/cloud.cfg"
2023-05-03 17:08:05 +08:00
virt-customize -a $qcow_file --run-command "echo 'Modified from https://github.com/spiritLHLS/Images' >> /etc/motd"
virt-customize -a $qcow_file --run-command "echo 'Related repo https://github.com/spiritLHLS/pve' >> /etc/motd"
virt-customize -a $qcow_file --run-command "echo '--by https://t.me/spiritlhl' >> /etc/motd"
echo "启用SSH功能..."
virt-customize -a $qcow_file --run-command "systemctl enable ssh"
virt-customize -a $qcow_file --run-command "systemctl start ssh"
virt-customize -a $qcow_file --run-command "systemctl enable sshd"
virt-customize -a $qcow_file --run-command "systemctl start sshd"
echo "启用root登录..."
virt-customize -a $qcow_file --run-command "sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "sed -i 's/#PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "sed -i 's/#Port 22/Port 22/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "sed -i 's/#AddressFamily any/AddressFamily any/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "sed -i 's/#ListenAddress 0.0.0.0/ListenAddress 0.0.0.0/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "sed -i 's/#ListenAddress ::/ListenAddress ::/g' /etc/ssh/sshd_config"
virt-customize -a $qcow_file --run-command "service ssh restart"
virt-customize -a $qcow_file --run-command "service sshd restart"
virt-customize -a $qcow_file --run-command "systemctl restart sshd"
virt-customize -a $qcow_file --run-command "systemctl restart ssh"
2023-05-03 13:33:29 +08:00
fi
echo "创建备份..."
cp $qcow_file ${qcow_file}.bak
echo "复制新文件..."
cp $qcow_file ${qcow_file}.tmp
echo "覆盖原文件..."
mv ${qcow_file}.tmp $qcow_file
rm -rf *.bak
echo "$qcow_file修改完成"