当前位置: 首页 > news >正文

archlinux安装脚本

以下是archlinux系统的安装脚本。请将两个脚本保存为可执行文件(如 arch_install_part1.sharch_install_part2.sh),并从 Arch Linux 安装 ISO 启动后以 root 身份依次运行。

⚠️ 警告: 这两个脚本将完全擦除并重新分区 /dev/nvme0n1 (SSD) 和 /dev/sda (HDD)。运行前请备份所有重要数据!


脚本 1:arch_install_part1.sh (基本系统安装,在 chroot 之前执行)

#!/bin/bash
# arch_install_part1.sh – Basic Arch Linux installation (pre-chroot)
# Run this script as root from the Arch Linux live ISO.set -e  # Exit on error# ============================== Helper Functions ==============================
error_exit() {echo -e "\033[1;31m[ERROR]\033[0m $1" >&2exit 1
}info() {echo -e "\033[1;32m[INFO]\033[0m $1"
}confirm() {read -p "$1 [y/N] " -n 1 -recho[[ $REPLY =~ ^[Yy]$ ]]
}# ============================== Pre-flight Checks ==============================
if [[ $EUID -ne 0 ]]; thenerror_exit "This script must be run as root."
fiinfo "Checking internet connectivity..."
ping -c 2 archlinux.org &>/dev/null || error_exit "No internet connection. Please connect to the network first."timedatectl set-ntp true
info "System clock synchronized."# ============================== Disk Partitioning ==============================
info "The following disks will be COMPLETELY ERASED:"
lsblk /dev/nvme0n1 /dev/sda
if ! confirm "Do you want to continue?"; thenerror_exit "Installation aborted by user."
fi# Wipe existing partition tables and create new GPT
info "Partitioning SSD (/dev/nvme0n1)..."
wipefs -a /dev/nvme0n1
parted /dev/nvme0n1 --script mklabel gpt \mkpart primary fat32 1MiB 2GiB \set 1 esp on \mkpart primary linux-swap 2GiB 18GiB \mkpart primary ext4 18GiB 100%# Partition HDD (/dev/sda)
info "Partitioning HDD (/dev/sda)..."
wipefs -a /dev/sda
parted /dev/sda --script mklabel gpt mkpart primary ext4 0% 100%# Wait for kernel to recognize partitions
sleep 2
partprobe# ============================== Formatting ==============================
info "Formatting partitions..."
mkfs.fat -F32 /dev/nvme0n1p1        # /boot (EFI)
mkswap /dev/nvme0n1p2               # swap
mkfs.ext4 -F /dev/nvme0n1p3         # /
mkfs.ext4 -F /dev/sda1              # /home# ============================== Mounting ==============================
info "Mounting filesystems..."
mount /dev/nvme0n1p3 /mnt
mkdir -p /mnt/boot
mount /dev/nvme0n1p1 /mnt/boot
mkdir -p /mnt/home
mount /dev/sda1 /mnt/home
swapon /dev/nvme0n1p2# ============================== Base System Installation ==============================
info "Setup the mirrorlist to aliyun.
echo "Server = https://mirrors.aliyun.com/archlinux/\$repo/os/\$arch" > /etc/pacman.d/mirrorlist
info "Installing base system (this may take a few minutes)..."
pacstrap /mnt base base-devel linux linux-firmware \vim sudo grub efibootmgr networkmanager \intel-ucode git curl wget man-db man-pages texinfo# Generate fstab
info "Generating fstab..."
genfstab -U /mnt >> /mnt/etc/fstab# ============================== Prepare for chroot ==============================
cp "$0" /mnt/root/arch_install_part1.sh   # (optional, for reference)
info "Base system installation complete."
info "Next steps:"
info "1. Copy the second script (arch_install_part2.sh) into the chroot:"
info "   cp arch_install_part2.sh /mnt/root/"
info "2. Chroot into the new system:"
info "   arch-chroot /mnt"
info "3. Run the second script inside chroot:"
info "   /root/arch_install_part2.sh"
info "4. After completion, exit chroot, reboot, and remove the installation media."

脚本 2:arch_install_part2.sh (在 chroot 内执行,完成桌面环境配置)

#!/bin/bash
# arch_install_part2.sh – Complete KDE desktop & driver setup (inside chroot)
# Run this script as root inside the chroot environment (arch-chroot /mnt)set -e# ============================== Helper Functions ==============================
error_exit() {echo -e "\033[1;31m[ERROR]\033[0m $1" >&2exit 1
}info() {echo -e "\033[1;32m[INFO]\033[0m $1"
}# ============================== Pre-flight Checks ==============================
if [[ $EUID -ne 0 ]]; thenerror_exit "This script must be run as root inside the chroot."
fi# ============================== System Configuration ==============================
info "Setup the mirrorlist to aliyun.
echo "Server = https://mirrors.aliyun.com/archlinux/\$repo/os/\$arch" > /etc/pacman.d/mirrorlistinfo "Setting timezone (Asia/Shanghai)..."
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
hwclock --systohcinfo "Generating locales..."
sed -i 's/^#\(en_US.UTF-8\)/\1/' /etc/locale.gen
sed -i 's/^#\(zh_CN.UTF-8\)/\1/' /etc/locale.gen
locale-gen
echo "LANG=en_US.UTF-8" > /etc/locale.conf   # System language English, user can override# Hostname
read -p "Enter hostname: " HOSTNAME
if [[ -z "$HOSTNAME" ]]; thenerror_exit "Hostname cannot be empty."
fi
echo "$HOSTNAME" > /etc/hostname
cat > /etc/hosts << EOF
127.0.0.1   localhost
::1         localhost
127.0.1.1   $HOSTNAME.localdomain $HOSTNAME
EOF# ============================== User Creation ==============================
read -p "Enter new username: " USERNAME
if [[ -z "$USERNAME" ]]; thenerror_exit "Username cannot be empty."
fi
useradd -m -G wheel,audio,video,storage,optical -s /bin/bash "$USERNAME"
passwd "$USERNAME"
echo "Set root password:"
passwd# Allow wheel group to use sudo
echo "%wheel ALL=(ALL:ALL) ALL" >> /etc/sudoers# ============================== Initramfs & Bootloader ==============================
info "Configuring mkinitcpio..."
# No changes needed for most setups, but ensure intel microcode is loaded
mkinitcpio -Pinfo "Installing GRUB for EFI..."
grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=GRUB
grub-mkconfig -o /boot/grub/grub.cfg# ============================== Install KDE Plasma & Drivers ==============================
info "Updating package database..."
pacman -Sy --noconfirminfo "Installing KDE Plasma (meta package) and core applications..."
pacman -S --noconfirm \plasma-meta \sddm \konsole dolphin \ark gwenview okular \kdeconnect \spectacle \plasma-wayland-session# Enable SDDM display manager
systemctl enable sddm# ============================== Networking & Bluetooth ==============================
info "Installing and enabling network tools..."
pacman -S --noconfirm networkmanager nm-connection-editor
systemctl enable NetworkManager# Bluetooth
pacman -S --noconfirm bluez bluez-utils bluedevil
systemctl enable bluetooth# ============================== Sound & Audio ==============================
info "Installing PipeWire (modern audio server)..."
pacman -S --noconfirm pipewire pipewire-pulse wireplumber \pipewire-alsa pipewire-jack
systemctl --user enable pipewire pipewire-pulse wireplumber
# Also enable for the new user (system-wide user service)
mkdir -p /etc/systemd/user
ln -sf /usr/lib/systemd/user/pipewire.service /etc/systemd/user/
ln -sf /usr/lib/systemd/user/pipewire-pulse.service /etc/systemd/user/
ln -sf /usr/lib/systemd/user/wireplumber.service /etc/systemd/user/# ============================== Graphics Drivers (Intel UHD 630) ==============================
info "Installing Intel graphics drivers and Vulkan support..."
pacman -S --noconfirm \mesa vulkan-intel intel-media-driver \libva-intel-driver intel-gpu-tools \xf86-video-intel   # optional but stable for X11# ============================== Chinese Environment ==============================
info "Installing Chinese fonts and Fcitx5 input method..."
pacman -S --noconfirm \noto-fonts-cjk \adobe-source-han-sans-cn-fonts \wqy-microhei \fcitx5 fcitx5-chinese-addons fcitx5-configtool \fcitx5-gtk fcitx5-qt# Set up environment variables for Fcitx5 (for both root and new user)
cat > /etc/environment << EOF
GTK_IM_MODULE=fcitx
QT_IM_MODULE=fcitx
XMODIFIERS=@im=fcitx
SDL_IM_MODULE=fcitx
EOF# Auto-start fcitx5 for the new user (via profile script)
mkdir -p "/home/$USERNAME/.config/autostart"
cat > "/home/$USERNAME/.config/autostart/fcitx5.desktop" << EOF
[Desktop Entry]
Type=Application
Name=Fcitx5
Exec=fcitx5
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
EOF
chown -R "$USERNAME:$USERNAME" "/home/$USERNAME/.config"# ============================== Extra Drivers & Utilities ==============================
info "Installing additional drivers: WiFi, printer, etc."
pacman -S --noconfirm \sof-firmware         # audio firmware for recent Intel chipsiwd                  # optional wireless daemon (can be used alongside NM)cups                 # printing system (optional)bluez-tools# Enable CUPS if desired (uncomment to enable)
# systemctl enable cups# ============================== Final Cleanup & Hints ==============================
info "Enabling TRIM for SSD (nvme0n1)..."
systemctl enable fstrim.timerinfo "All done! You can now exit chroot, reboot, and log into your new KDE Plasma system."
info "Remember to:"
info "  1. Type 'exit' to leave chroot"
info "  2. Run 'umount -R /mnt' and 'swapoff /dev/nvme0n1p2'"
info "  3. Reboot"

使用方法

  1. 从 Arch ISO 启动,连接网络,确保可以访问互联网。
  2. 保存两个脚本(例如用 vim),并赋予执行权限:
    chmod +x arch_install_part1.sh arch_install_part2.sh
    
  3. 以 root 身份运行第一个脚本
    ./arch_install_part1.sh
    
  4. 按照提示确认分区操作,等待基础系统安装完成。
  5. 将第二个脚本复制到 /mnt/root/(脚本结束时会提示):
    cp arch_install_part2.sh /mnt/root/
    
  6. chroot 进入新系统
    arch-chroot /mnt
    
  7. 在 chroot 中运行第二个脚本
    /root/arch_install_part2.sh
    
  8. 根据交互提示输入主机名、用户名、密码等信息。
  9. 安装完成后,退出 chroot、卸载分区并重启:
    exit
    umount -R /mnt
    swapoff /dev/nvme0n1p2
    reboot
    

重启后您将进入 KDE Plasma 桌面环境,中文输入法(Fcitx5 五笔拼音)已预配置,蓝牙、声卡、网络等均自动启用。