本文根據Github Arch Linux安裝腳本寫的,有需要的可以根據原作者代碼進行更改,但是作者好久都沒有更新了,注意必要的修改
提取出主要的兩個文件,根據我自己的情況修改了一些,粗略的解釋一下,新手可以多翻翻
arch-linux-installer.sh
#!/bin/bash #字體顏色 color(){ case $1 in red) echo -e "\033[31m$2\033[0m" ;; green) echo -e "\033[32m$2\033[0m" ;; esac } #分區分配 partition(){ if (echo $1 | grep '/' > /dev/null 2>&1);then other=$1 else other=/$1 fi fdisk -l color green "Input the partition (/dev/sdaX" read OTHER color green "Format it ? y)yes ENTER)no" read tmp if [ "$other" == "/boot" ];then boot=$OTHER fi if [ "$tmp" == y ];then umount $OTHER > /dev/null 2>&1 color green "Input the filesystem's num to format it" select type in 'ext2' "ext3" "ext4" "btrfs" "xfs" "jfs" "fat" "swap";do case $type in "ext2") mkfs.ext2 $OTHER break ;; "ext3") mkfs.ext3 $OTHER break ;; "ext4") mkfs.ext4 $OTHER break ;; "btrfs") mkfs.btrfs $OTHER -f break ;; "xfs") mkfs.xfs $OTHER -f break ;; "jfs") mkfs.jfs $OTHER break ;; "fat") mkfs.fat -F32 $OTHER break ;; "swap") swapoff $OTHER > /dev/null 2>&1 mkswap $OTHER -f break ;; *) color red "Error ! Please input the num again" ;; esac done fi if [ "$other" == "/swap" ];then swapon $OTHER else umount $OTHER > /dev/null 2>&1 mkdir /mnt$other mount $OTHER /mnt$other fi } #分區准備 #建議手動分區,在后邊刪除調用這個程序的命令 prepare(){ fdisk -l color green "Do you want to adjust the partition ? y)yes ENTER)no" read tmp if [ "$tmp" == y ];then color green "Input the disk (/dev/sdX" read TMP cfdisk $TMP fi color green "Input the ROOT(/) mount point:" read ROOT color green "Format it ? y)yes ENTER)no" read tmp if [ "$tmp" == y ];then umount $ROOT > /dev/null 2>&1 color green "Input the filesystem's num to format it" select type in "ext4" "btrfs" "xfs" "jfs";do umount $ROOT > /dev/null 2>&1 if [ "$type" == "btrfs" ];then mkfs.$type $ROOT -f elif [ "$type" == "xfs" ];then mkfs.$type $ROOT -f else mkfs.$type $ROOT fi break done fi mount $ROOT /mnt color green "Do you have another mount point ? if so please input it, such as : /boot /home and swap or just ENTER to skip" read other while [ "$other" != '' ];do partition $other color green "Still have another mount point ? input it or just ENTER" read other done } install(){ #修改源 echo "Server = https://mirrors.ustc.edu.cn/archlinux/\$repo/os/\$arch" > /etc/pacman.d/mirrorlist #配置基本系統 #注意選擇的包,最小的只需要base,linux,linux-firmware即可,原作者更新於2019年九月以前,那時候linux包還在base包組里 pacstrap /mnt base base-devel linux-lts linux-firmware genfstab -U -p /mnt > /mnt/etc/fstab } config(){ #復制配置腳本到新系統里,並且chroot進去 cp config.sh /mnt/root/config.sh chmod +x /mnt/root/config.sh arch-chroot /mnt /root/config.sh $ROOT $boot } if [ "$1" != '' ];then case $1 in "--prepare") prepare ;; "--install") install ;; "--chroot") config ;; "--help") color red "--prepare : prepare disk and partition\n--install : install the base system\n--chroot : chroot into the system to install other software" ;; *) color red "Error !\n--prepare : prepare disk and partition\n--install : install the base system\n--chroot : chroot into the system to install other software" ;; esac else
#程序調用處 prepare install config fi
config.sh
#!/bin/bash root=$1 boot=$2 set -e #字體顏色 color(){ case $1 in red) echo -e "\033[31m$2\033[0m" ;; yellow) echo -e "\033[33m$2\033[0m" ;; esac } config_base(){
#設置電腦名稱 color yellow "Input your hostname" read TMP echo $TMP > /etc/hostname #設置root密碼
color yellow "Change your root passwd" passwd } config_locale(){ #選擇時區,tzselect有同樣的作用
color yellow "Please choose your locale time" select TIME in `ls /usr/share/zoneinfo`;do if [ -d "/usr/share/zoneinfo/$TIME" ];then select time in `ls /usr/share/zoneinfo/$TIME`;do ln -sf /usr/share/zoneinfo/$TIME/$time /etc/localtime break done else ln -sf /usr/share/zoneinfo/$TIME /etc/localtime break fi break done
#同步硬件時間 hwclock --systohc --utc #設置語言,建議Lang選擇en,locale.gen修改成
#en_US.UTF-8 UTF-8
#zh_CN.UTF-8 UTF-8
color yellow "Choose your language" select LNAG in "en_US.UTF-8" "zh_CN.UTF-8";do echo "$LNAG UTF-8" > /etc/locale.gen locale-gen echo LANG=$LANG > /etc/locale.conf break done } #無特殊需求首選grub,也可以選用其他引導工具,但是其他引導工具需要根據Wiki修改,此處不適用,如果雙系統需要添加os-prober包,修改成pacman -S --noconfirm grub efibootmgr os-prober -y即可 install_grub(){ if (mount | grep efivarfs > /dev/null 2>&1);then pacman -S --noconfirm grub efibootmgr -y rm -f /sys/firmware/efi/efivars/dump-* grub-install --target=`uname -m`-efi --efi-directory=/boot --bootloader-id=Arch #配置grub,可以根據需求修改grub.cfg文件
grub-mkconfig -o /boot/grub/grub.cfg else pacman -S --noconfirm grub fdisk -l color yellow "Input the disk you want to install grub (/dev/sdX" read TMP grub-install --target=i386-pc $TMP grub-mkconfig -o /boot/grub/grub.cfg fi } install_bootctl(){ if (mount | grep efivarfs > /dev/null 2>&1);then bootctl --path=/boot install cp /usr/share/systemd/bootctl/loader.conf /boot/loader/ echo "timeout 4" >> /boot/loader/loader.conf echo -e "title Arch Linux\nlinux ../vmlinuz-linux\ninitrd ../initramfs-linux.img" > /boot/loader/entries/arch.conf else color yellow "Looks like your PC doesn't suppot UEFI or not in UEFI mode ENTER to use grub. Input q to quit" read TMP if [ "$TMP" == "" ];then install_grub else exit fi fi } install_efistub(){ UUID=`blkid -s UUID -o value $root` efi=`echo $boot | grep -o "[0-9]*"` if (mount | grep efivarfs > /dev/null 2>&1);then pacman -S --noconfirm efibootmgr rm -f /sys/firmware/efi/efivars/dump-* efibootmgr --disk $boot --part $efi --create --label "Arch Linux" --loader /vmlinuz-linux --unicode "root=UUID=$UUID rw initrd=\initramfs-linux.img" else color yellow "Looks like your PC doesn't suppot UEFI or not in UEFI mode ENTER to use grub. Input q to quit" read TMP if [ "$TMP" == "" ];then install_grub else exit fi fi } #添加用戶 add_user(){ color yellow "Input the user name you want to use (must be lower case)" read USER useradd -m -g wheel $USER color yellow "Set the passwd" passwd $USER pacman -S --noconfirm sudo
#使添加的用戶組擁有sudo權限 sed -i 's/\# \%wheel ALL=(ALL) ALL/\%wheel ALL=(ALL) ALL/g' /etc/sudoers sed -i 's/\# \%wheel ALL=(ALL) NOPASSWD: ALL/\%wheel ALL=(ALL) NOPASSWD: ALL/g' /etc/sudoers } #安裝顯卡驅動,我的上一個電腦i5-7200U+MX150可以用此安裝顯卡驅動
#但是現在的電腦i7-9750H+1660ti不適用,不建議使用bumblebee管理顯卡
#可以根據wiki,從Nvidia官網下載Linux顯卡驅動安裝,使用optimus-manager管理
#點此處查看Wiki關於optimus-manager的說明
install_graphic(){ color yellow "What is your video graphic card?" select GPU in "Intel" "Nvidia" "Intel and Nvidia" "AMD";do case $GPU in "Intel") pacman -S --noconfirm xf86-video-intel -y break ;; "Nvidia") color yellow "Version of nvidia-driver to install" select NVIDIA in "GeForce-8 and newer" "GeForce-6/7" "Older";do case $NVIDIA in "GeForce-8 and newer") pacman -S --noconfirm nvidia -y break ;; "GeForce-6/7") pacman -S --noconfirm nvidia-304xx -y break ;; "Older") pacman -S --noconfirm nvidia-340xx -y break ;; *) color red "Error ! Please input the correct num" ;; esac done break ;; "Intel and Nvidia") pacman -S --noconfirm bumblebee -y systemctl enable bumblebeed color yellow "Version of nvidia-driver to install" select NVIDIA in "GeForce-8 and newer" "GeForce-6/7" "Older";do case $NVIDIA in "GeForce-8 and newer") pacman -S --noconfirm nvidia -y break ;; "GeForce-6/7") pacman -S --noconfirm nvidia-304xx -y break ;; "Older") pacman -S --noconfirm nvidia-340xx -y break ;; *) color red "Error ! Please input the correct num" ;; esac done break ;; "AMD") pacman -S --noconfirm xf86-video-ati -y break ;; *) color red "Error ! Please input the correct num" ;; esac done } #藍牙驅動 install_bluetooth(){ pacman -S --noconfirm bluez systemctl enable bluetooth color yellow "Install blueman? y)YES ENTER)NO" read TMP if [ "$TMP" == "y" ];then pacman -S --noconfirm blueman fi } #安裝一些軟件 install_app(){ color yellow "Install yay(aur helper) from archlinuxcn or use git ? (just for China users) y)YES ENTER)NO" read TMP if [ "$TMP" == "y" ];then sed -i '/archlinuxcn/d' /etc/pacman.conf sed -i '/archlinux-cn/d' /etc/pacman.conf
#添加archlinuxcn源 select MIRROR in "USTC" "TUNA" "163";do case $MIRROR in "USTC") echo -e "[archlinuxcn]\nServer = https://mirrors.ustc.edu.cn/archlinuxcn/\$arch" >> /etc/pacman.conf break ;; "TUNA") echo -e "[archlinuxcn]\nServer = https://mirrors.tuna.tsinghua.edu.cn/archlinuxcn/\$arch" >> /etc/pacman.conf break ;; "163") echo -e "[archlinuxcn]\nServer = http://mirrors.163.com/archlinux-cn/\$arch" >> /etc/pacman.conf break ;; *) color red "Error ! Please input the correct num" ;; esac done pacman -Sy pacman -S --noconfirm archlinuxcn-keyring else pacman -S --noconfirm git su - $USER -c "cd ~ git clone https://aur.archlinux.org/package-query.git cd package-query && makepkg -si cd .. git clone https://aur.archlinux.org/yay.git cd yay && makepkg -si cd .. rm -rf package-query yay" fi
#安裝一些軟件,可手動修改,建議使用networkmanager而不是wicd等網絡管理軟件 pacman -S --noconfirm networkmanager xorg-server wqy-zenhei wget systemctl enable NetworkManager
#由於我是手動安裝顯卡驅動故注釋 # if [ "$GPU" == "Intel and Nvidia" ];then # gpasswd -a $USER bumblebee # fi } #安裝桌面環境,可根據wiki說明選擇自己需要的包,這里給的不是最小化安裝 install_desktop(){ color yellow "Choose the desktop you want to use" select DESKTOP in "KDE" "Gnome" "Lxde" "Lxqt" "Mate" "Xfce" "Deepin" "Budgie" "Cinnamon";do case $DESKTOP in "KDE") pacman -S plasma kdebase kdeutils kdegraphics sddm systemctl enable sddm break ;; "Gnome") pacman -S gnome gnome-terminal systemctl enable gdm break ;; "Lxde") pacman -S lxde lightdm lightdm-gtk-greeter systemctl enable lightdm break ;; "Lxqt") pacman -S lxqt lightdm lightdm-gtk-greeter systemctl enable lightdm break ;; "Mate") pacman -S mate mate-extra mate-terminal lightdm lightdm-gtk-greeter systemctl enable lightdm break ;; "Xfce") pacman -S xfce4 xfce4-terminal lightdm lightdm-gtk-greeter # xfce4-goodies systemctl enable lightdm break ;; "Deepin") pacman -S deepin deepin-extra deepin-terminal lightdm lightdm-gtk-greeter systemctl enable lightdm sed -i '108s/#greeter-session=example-gtk-gnome/greeter-session=lightdm-deepin-greeter/' /etc/lightdm/lightdm.conf break ;; "Budgie") pacman -S budgie-desktop gnome-terminal lightdm lightdm-gtk-greeter systemctl enable lightdm break ;; "Cinnamon") pacman -S cinnamon gnome-terminal lightdm lightdm-gtk-greeter systemctl enable lightdm break ;; *) color red "Error ! Please input the correct num" ;; esac done } clean(){ sed -i 's/\%wheel ALL=(ALL) NOPASSWD: ALL/\# \%wheel ALL=(ALL) NOPASSWD: ALL/g' /etc/sudoers } #主程序,根據需要修改,由於該腳本沒有糾錯功能,輸入錯誤就會崩潰,運行到哪一步崩潰了把前面的刪了再運行即可 main(){ config_base config_locale color yellow "Use Bootctl EFISUB or GRUB ? b)Bootctl e)EFISTUB ENTER)GRUB" read TMP if [ "$TMP" == "b" ];then install_bootctl elif [ "$TMP" == "e" ];then install_efistub else install_grub fi add_user # install_graphic color yellow "Do you have bluetooth ? y)YES ENTER)NO" read TMP if [ "$TMP" == "y" ];then install_bluetooth fi install_app install_desktop clean color yellow "Done , Thanks for using" } main