Tools/Linux_reinstall/Ubuntu/autoRepackUbuntuCloudImages.sh

43 lines
1.8 KiB
Bash
Raw Normal View History

2023-06-08 01:45:07 +08:00
#!/bin/bash
#
# kpartx and qumu-utils are required
2023-09-25 00:20:54 +08:00
apt update -y
2023-09-26 21:45:14 +08:00
apt install axel cron kpartx mount qemu-utils xz-utils -y
2023-06-08 01:45:07 +08:00
# get valid loop device
loopDevice=$(echo $(losetup -f))
loopDeviceNum=$(echo $(losetup -f) | cut -d'/' -f 3)
2023-09-26 21:45:14 +08:00
linuxName="Ubuntu"
websiteDir="/www/wwwroot/cloud-images.a.disk.re/$linuxName"
[[ ! -d "$websiteDir" ]] && mkdir -p "$websiteDir"
2023-06-08 01:45:07 +08:00
2023-06-08 22:53:44 +08:00
for distName in "jammy" "focal"; do
2023-09-26 21:45:14 +08:00
for archType in "amd64" "arm64"; do
fileName="$distName-server-cloudimg-$archType"
axel -n 16 -k -q -o /root/$fileName.img "https://cloud-images.ubuntu.com/$distName/current/$fileName.img"
2023-06-08 01:45:07 +08:00
qemu-img convert -f qcow2 -O raw /root/$fileName.img /root/$fileName.raw
losetup $loopDevice /root/$fileName.raw
mapperDevice=$(kpartx -av $loopDevice | grep "$loopDeviceNum" | head -n 1 | awk '{print $3}')
mount /dev/mapper/$mapperDevice /mnt
sed -ri 's/GRUB_CMDLINE_LINUX=""/GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0"/g' /mnt/etc/default/grub
sed -ri 's/cloudimg-rootfs ro/cloudimg-rootfs ro net.ifnames=0 biosdevname=0/g' /mnt/boot/grub/grub.cfg
2023-09-18 19:46:29 +08:00
# sed -ri 's/GRUB_TIMEOUT=0/GRUB_TIMEOUT=3/g' /mnt/etc/default/grub
# sed -ri 's/set timeout=0/set timeout=3/g' /mnt/boot/grub/grub.cfg
2023-06-08 01:45:07 +08:00
umount /mnt
kpartx -dv $loopDevice
losetup -d $loopDevice
2023-09-23 16:00:29 +08:00
xz -z -1 -T 0 /root/$fileName.raw
mv /root/$fileName.raw.xz /root/$fileName.xz
rm -rf $websiteDir/$fileName.xz
mv /root/$fileName.xz $websiteDir/$fileName.xz
2023-06-08 01:45:07 +08:00
rm -rf $websiteDir/$fileName.raw
2023-09-23 16:00:29 +08:00
rm -rf /root/$fileName.raw
2023-06-08 01:45:07 +08:00
rm -rf /root/$fileName.img
done
done
# write crontab task
2023-07-20 06:43:21 +08:00
if [[ ! `grep -i "autorepackubuntucloudimages" /etc/crontab` ]]; then
2023-09-23 16:00:29 +08:00
sed -i '$i 30 4 10-16,24-30 * 5 root bash /root/autoRepackUbuntuCloudImages.sh' /etc/crontab
2023-06-08 01:45:07 +08:00
/etc/init.d/cron restart
fi