debootstrap部署打包系统debian 和 ubuntu
目前为止好像还不支持 ubuntu24
具体支持的版本查看/usr/share/debootstrap/scripts/目录
#!/bin/bash
# 设置变量
NAME="jammy"
DISK="/dev/vdb"
BOOT_PART="${DISK}1"
SWAP_PART="${DISK}2"
ROOT_PART="${DISK}3"
apt update -y
apt install -y debootstrap arch-install-scripts parted dosfstools
parted ${DISK} --script -- mklabel msdos
parted ${DISK} --script -- mkpart primary ext4 1MiB 2GiB
parted ${DISK} --script -- mkpart primary linux-swap 2GiB 3GiB
parted ${DISK} --script -- mkpart primary ext4 3GiB 100%
# 格式化分区
#mkfs.vfat -F32 $BOOT_PART
mkfs.ext4 -F $BOOT_PART
mkfs.ext4 -F $ROOT_PART
mkswap $SWAP_PART
swapon $SWAP_PART
mount $ROOT_PART /mnt/
mkdir -p /mnt/boot
mount $BOOT_PART /mnt/boot
# 使用 debootstrap 安装基本系统
debootstrap --arch amd64 $NAME /mnt/ http://archive.ubuntu.com/ubuntu/
mount --bind /proc /mnt/proc
mount --bind /dev /mnt/dev
mount --bind /sys /mnt/sys
mount -t devpts devpts /mnt/dev/pts
#更新设置系统内核和配置
chroot /mnt /bin/bash -c "echo 'root:your_password' | chpasswd"
chroot /mnt/ apt -y update
chroot /mnt/ apt -y upgrade
chroot /mnt/ locale-gen en_US.UTF-8
chroot /mnt/ update-locale LANG=en_US.UTF-8
for I in linux-image-generic linux-firmware initramfs-tools efibootmgr grub2-common grub-efi-amd64 grub-pc;
do chroot /mnt/ apt -y install --no-install-recommends $I ; done
chroot /mnt/ sed -i 's#GRUB_CMDLINE_LINUX=""#GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0"#g' /etc/default/grub
chroot /mnt/ echo GRUB_DISABLE_OS_PROBER=true >/etc/default/grub
chroot /mnt/ grub-install $DISK
chroot /mnt/ grub-mkconfig -o /boot/grub/grub.cfg
chroot /mnt/ update-grub
#chroot /mnt/ grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=ubuntu --recheck
chroot /mnt/ apt -y install --no-install-recommends openssh-server
chroot /mnt/ sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config
chroot /mnt/ systemctl enable systemd-networkd
chroot /mnt/ systemctl enable ssh
cat>/mnt/etc/apt/sources.list<<EOF
deb http://archive.ubuntu.com/ubuntu ${NAME} main
deb http://archive.ubuntu.com/ubuntu ${NAME} ${NAME}-security main
deb http://archive.ubuntu.com/ubuntu ${NAME} ${NAME}-updates main
EOF
cat>/mnt/etc/systemd/network/10-eth0.network<<EOF
[Match]
Name=eth0
[Network]
DHCP=yes
EOF
# 配置网络、主机名、fstab等(可根据需要添加)
cat>/mnt/etc/fstab<<EOF
/dev/vda1 /boot ext4 defaults 1 1
/dev/vda2 swap swap defaults 0 0
/dev/vda3 / ext4 defaults 1 2
EOF
echo "ubuntu" > /mnt/etc/hostname
cat <<EOL > /mnt/etc/network/interfaces
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
EOL
swapoff $SWAP_PART
for i in /dev/pts /dev /proc /sys /boot /; do umount /mnt$i; done ## 卸载挂载
echo "Ubuntu installation setup completed on $DISK"
none