Skip to content

Instantly share code, notes, and snippets.

@yitong-ovo
Forked from Phaeilo/archvm.sh
Last active February 5, 2020 10:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yitong-ovo/7a5a4750bfa661d8ea263d1887383084 to your computer and use it in GitHub Desktop.
Save yitong-ovo/7a5a4750bfa661d8ea263d1887383084 to your computer and use it in GitHub Desktop.
Archlinux VM automated installation script.
#!/bin/bash
# The MIT License (MIT)
#
# Copyright (c) 2015 Philip Huppert
# fork by Yitong
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
DISK="/dev/sda"
PARTITION="${DISK}1"
HOSTNAME="archvm"
PASSWORD="root"
TIMEZONE="Asia/Shanghai"
CONSOLE_KEYMAP="us"
PACSTRAP="base grub linux linux-firmware"
VBOX_GUEST_ADDITIONS=1
ETH0_MODE="DHCP" # DHCP/STATIC/OFF
function announce {
>&2 echo -n "$1"
}
function check_fail {
if [[ $1 -ne 0 ]]; then
>&2 echo "FAIL!"
exit 1
else
>&2 echo "OK!"
fi
}
announce "Checking internet connectivity... "
wget -q --tries=10 --timeout=20 --spider http://g.cn
check_fail $?
announce "Set Pacman Mirrors(CN)... "
cat <<EOF > /etc/pacman.d/mirrorlist
## China
Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/\$repo/os/\$arch
EOF
check_fail $?
announce "Creating partition table... "
parted -s "$DISK" mklabel msdos
check_fail $?
announce "Creating partition... "
parted -s -a optimal "$DISK" mkpart primary 0% 100%
check_fail $?
announce "Making partition bootable... "
parted -s "$DISK" set 1 boot on
check_fail $?
announce "Formatting partition with ext4... "
mkfs.ext4 -F "$PARTITION"
check_fail $?
announce "Mounting partition... "
mount "$PARTITION" /mnt
check_fail $?
announce "Installing base system... "
pacstrap /mnt $PACSTRAP
check_fail $?
announce "Configuring fstab... "
genfstab -p /mnt >> /mnt/etc/fstab
check_fail $?
announce "Setting root password... "
echo "root:$PASSWORD" | arch-chroot /mnt chpasswd
check_fail $?
announce "Setting hostname... "
echo "$HOSTNAME" > /mnt/etc/hostname
check_fail $?
announce "Setting timezone... "
ln -sf "/usr/share/zoneinfo/$TIMEZONE" /mnt/etc/localtime
check_fail $?
announce "Enabling locales... "
sed -i 's/^#en_US\.UTF/en_US\.UTF/' /mnt/etc/locale.gen && sed -i 's/^#zh_CN\.UTF/zh_CN\.UTF/' /mnt/etc/locale.gen && sed -i 's/^#zh_TW\.UTF/zh_TW\.UTF/' /mnt/etc/locale.gen
check_fail $?
announce "Configuring vconsole... "
echo -en "KEYMAP=$CONSOLE_KEYMAP" > /mnt/etc/vconsole.conf
check_fail $?
announce "Generating locales... "
arch-chroot /mnt locale-gen
check_fail $?
if [ "$ETH0_MODE" = "DHCP" ]; then
announce "Configuring eth0 for DHCP... "
cat <<EOF > /mnt/etc/systemd/network/ens33_DHCP.network
[Match]
Name=ens33
[Network]
DHCP=v4
EOF
check_fail $?
fi
announce "Enabling networking... "
arch-chroot /mnt systemctl enable systemd-networkd
check_fail $?
announce "Enabling DNS... "
arch-chroot /mnt systemctl enable systemd-resolved
check_fail $?
announce "Configuring NTP... "
sed -i 's/^#NTP=$/NTP=0.pool.ntp.org 1.pool.ntp.org 2.pool.ntp.org 3.pool.ntp.org/' /mnt/etc/systemd/timesyncd.conf
check_fail $?
if [[ $VBOX_GUEST_ADDITIONS -eq 1 ]]; then
announce "Installing virtualbox-guest-utils... "
arch-chroot /mnt pacman --noconfirm -S virtualbox-guest-utils mesa-libgl
check_fail $?
announce "Configuring virtualbox kernel modules... "
echo -en "vboxguest\nvboxsf\nvboxvideo\n" > /mnt/etc/modules-load.d/virtualbox.conf
check_fail $?
announce "Enabling vboxservice... "
arch-chroot /mnt systemctl enable vboxservice
check_fail $?
fi
announce "Writing MBR... "
arch-chroot /mnt grub-install --target=i386-pc --recheck /dev/sda
check_fail $?
announce "Enabling GRUB configuration... "
arch-chroot /mnt grub-mkconfig -o /boot/grub/grub.cfg
check_fail $?
announce "Generating first-boot script... "
cat <<EOF > /mnt/firstboot.sh
#!/bin/bash
timedatectl set-ntp true
ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
systemctl disable firstboot.service
rm -f /etc/systemd/system/firstboot.service
rm -f /firstboot.sh
EOF
check_fail $?
announce "Generating first-boot service... "
cat <<EOF > /mnt/etc/systemd/system/firstboot.service
[Unit]
Description=Configuration script for first boot of system
After=basic.target
[Service]
Type=oneshot
User=root
ExecStart=/bin/bash /firstboot.sh
[Install]
WantedBy=basic.target
EOF
check_fail $?
announce "Enabling first-boot service... "
arch-chroot /mnt systemctl enable firstboot.service
check_fail $?
announce "Set Pacman archlinuxcn... "
cat <<EOF >> /mnt/etc/pacman.conf
[archlinuxcn]
Server = https://cdn.repo.archlinuxcn.org/\$arch
EOF
arch-chroot /mnt pacman -Syy
arch-chroot /mnt pacman --noconfirm -S archlinuxcn-keyring
check_fail $?
announce "Install Packages... "
arch-chroot /mnt pacman --noconfirm -S oh-my-zsh-git wget curl htop sudo aria2 axel git python-pip bind-tools openssh nmap proxychains vim nano byobu
check_fail $?
announce "Add User:Yitong... "
arch-chroot /mnt useradd -m -g users -G wheel -s /bin/zsh yitong
cp /mnt/usr/share/oh-my-zsh/zshrc /mnt/home/yitong/.zshrc
sed -i 's/^ZSH_THEME="robbyrussell"/ZSH_THEME="ys"/' /mnt/home/yitong/.zshrc
check_fail $?
announce "edit and enable sshd.service... "
sed -i 's/^ClientAliveInterval\ 0/ClientAliveInterval\ 30/' /mnt/etc/ssh/sshd_config
arch-chroot /mnt systemctl enable sshd.service
check_fail $?
announce "Add and Enable sysctl conf... "
cat <<EOF > /mnt/etc/sysctl.d/forwarding.conf
# Enable packet forwarding for IPv4
net.ipv4.ip_forward = 1
# Enable packet forwarding for IPv6
net.ipv6.conf.all.forwarding = 1
# Accept IPv6 router advertisement
net.ipv6.conf.all.accept_ra = 2
EOF
cat <<EOF > /mnt/etc/sysctl.d/bbr.conf
# Enable bbr
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr
EOF
cat <<EOF > /mnt/etc/sysctl.d/net.conf
net.core.netdev_max_backlog = 100000
net.core.netdev_budget = 50000
net.core.netdev_budget_usecs = 5000
net.core.somaxconn = 1024
net.core.rmem_default = 1048576
net.core.rmem_max = 16777216
net.core.wmem_default = 1048576
net.core.wmem_max = 16777216
net.core.optmem_max = 65536
net.ipv4.tcp_rmem = 4096 1048576 2097152
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.udp_rmem_min = 8192
net.ipv4.udp_wmem_min = 8192
net.ipv4.tcp_fastopen = 3
net.ipv4.tcp_keepalive_time = 60
net.ipv4.tcp_keepalive_intvl = 10
net.ipv4.tcp_keepalive_probes = 6
net.ipv4.tcp_mtu_probing = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.rp_filter = 1
EOF
arch-chroot /mnt sysctl --system
check_fail $?
announce "input User:Yitong Password... "
arch-chroot /mnt passwd yitong
check_fail $?
announce "allow wheel sudoers... "
sed -i 's/^#\ %wheel\ ALL=(ALL)\ NOPASSWD:\ ALL/%wheel\ ALL=(ALL)\ NOPASSWD: ALL/' /mnt/etc/sudoers
check_fail $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment