260 lines
9.1 KiB
Bash
Executable File
260 lines
9.1 KiB
Bash
Executable File
#!/bin/sh
|
||
|
||
# Vars
|
||
## Define the hard drive to use
|
||
if [ -b '/dev/sda' ]; then
|
||
hdd="/dev/sda"
|
||
else
|
||
printf '%b\n' "Please check the hard drive to use"
|
||
exit 0
|
||
fi
|
||
## Volume Group name to use for LVM
|
||
vgname="ovhsys"
|
||
## If the script should manage the partitions (delete, add,…)
|
||
manage_part=0
|
||
## Colors definition {{{
|
||
BLACK='\033[49;30m'
|
||
BLACKB='\033[49;90m'
|
||
RED='\033[0;31m'
|
||
REDB='\033[1;31m'
|
||
GREEN='\033[0;32m'
|
||
YELLOW='\033[0;33m'
|
||
BLUE='\033[94;49m'
|
||
MAGENTA='\033[0;35m'
|
||
CYAN='\033[36;49m'
|
||
WHITE='\033[0;37m'
|
||
BOLD='\033[1m'
|
||
RESET='\033[0m'
|
||
## }}}
|
||
|
||
## Package to exclude from debootstrap install
|
||
dbs_pkg_exclude="vim"
|
||
## Package to include to debootstrap install
|
||
dbs_pkg_include="aptitude,bzip2,debconf-i18n,dialog,dmsetup,htop,isc-dhcp-client,isc-dhcp-common,locales,lvm2,openssh-server,pciutils,tmux,vim-nox,wget,zsh"
|
||
|
||
# Partitionning {{{
|
||
if [ "${manage_part}" -eq 0 ]; then
|
||
## Remove all old partitions
|
||
for part_number in 1 2 3 4 5 6 7 8; do
|
||
[ -b "${hdd}""${part_number}" ] && parted "${hdd}" rm "${part_number}"
|
||
done
|
||
|
||
## Recreate partition (/boot and LV) {{{
|
||
### Partition type
|
||
parted "${hdd}" mklabel msdos
|
||
### /boot
|
||
parted "${hdd}" mkpart primary 0% 512MB
|
||
parted "${hdd}" set 1 boot on
|
||
### LV
|
||
parted "${hdd}" mkpart primary 4194kB 100%
|
||
parted "${hdd}" set 2 lvm on
|
||
sudo pvcreate "${hdd}"2
|
||
sudo vgcreate "${vgname}" "${hdd}"2
|
||
fi
|
||
|
||
mkfs.ext3 -F -L boot -- "${hdd}"1
|
||
|
||
## }}}
|
||
## Create Logical Volumes {{{
|
||
[ ! -b /dev/mapper/"${vgname}"-home ] && lvcreate -n home -L 20g "${vgname}"
|
||
[ ! -b /dev/mapper/"${vgname}"-opt ] && lvcreate -n opt -L 2g "${vgname}"
|
||
[ ! -b /dev/mapper/"${vgname}"-root ] && lvcreate -n root -L 5g "${vgname}"
|
||
[ ! -b /dev/mapper/"${vgname}"-srv ] && lvcreate -n srv -L 2g "${vgname}"
|
||
[ ! -b /dev/mapper/"${vgname}"-tmp ] && lvcreate -n tmp -L 10g "${vgname}"
|
||
[ ! -b /dev/mapper/"${vgname}"-usr ] && lvcreate -n usr -L 15g "${vgname}"
|
||
[ ! -b /dev/mapper/"${vgname}"-var ] && lvcreate -n var -L 10g "${vgname}"
|
||
[ ! -b /dev/mapper/"${vgname}"-vz ] && lvcreate -n vz -L 150g "${vgname}"
|
||
[ ! -b /dev/mapper/"${vgname}"-bkp ] && lvcreate -n bkp -L 150g "${vgname}"
|
||
[ ! -b /dev/mapper/"${vgname}"-swap ] && lvcreate -n swap -L 2g "${vgname}"
|
||
|
||
### Format the LV in ext4
|
||
cd -- /dev/"${vgname}" || exit 1
|
||
for lvname in *; do
|
||
mkfs.ext4 -F -L "${lvname}" -- "${lvname}"
|
||
done
|
||
cd -- - || exit 1
|
||
|
||
### And format the swap
|
||
mkswap -L sw01 -- /dev/mapper/"${vgname}"-swap
|
||
## }}}
|
||
# }}}
|
||
# Debootstrap {{{
|
||
## Create and mount the system {{{
|
||
### Root
|
||
mkdir -p -- /target
|
||
mountpoint -q /target || mount -- /dev/mapper/"${vgname}"-root /target
|
||
|
||
### boot - grub
|
||
mkdir -p -- /target/boot
|
||
mountpoint -q /target/boot || mount -- ${hdd}1 /target/boot
|
||
|
||
### home LV
|
||
mkdir -p -- /target/home
|
||
mountpoint -q /target/home || mount -- /dev/mapper/"${vgname}"-home /target/home
|
||
### opt LV
|
||
mkdir -p -- /target/opt
|
||
mountpoint -q /target/opt || mount -- /dev/mapper/"${vgname}"-opt /target/opt
|
||
### srv LV
|
||
mkdir -p -- /target/srv
|
||
mountpoint -q /target/srv || mount -- /dev/mapper/"${vgname}"-srv /target/srv
|
||
#### bkp LV
|
||
mkdir -p -- /target/srv/backup
|
||
mountpoint -q /target/srv/backup || mount -- /dev/mapper/"${vgname}"-bkp /target/srv/backup
|
||
### tmp LV
|
||
mkdir -p -- /target/tmp
|
||
mountpoint -q /target/tmp || mount -- /dev/mapper/"${vgname}"-tmp /target/tmp
|
||
### usr LV
|
||
mkdir -p -- /target/usr
|
||
mountpoint -q /target/usr || mount -- /dev/mapper/"${vgname}"-usr /target/usr
|
||
### var LV
|
||
mkdir -p -- /target/var
|
||
mountpoint -q /target/var || mount -- /dev/mapper/"${vgname}"-var /target/var
|
||
#### vz LV
|
||
mkdir -p -- /target/var/lib/vz
|
||
mountpoint -q /target/var/lib/vz || mount -- /dev/mapper/"${vgname}"-vz /target/var/lib/vz
|
||
|
||
### Swap
|
||
swapon -- /dev/mapper/"${vgname}"-swap
|
||
|
||
### System
|
||
mkdir -p -- /target/dev/pts
|
||
mountpoint -q /target/dev/pts || mount -o bind -- /dev/pts /target/dev/pts
|
||
mkdir -p -- /target/proc
|
||
mountpoint -q /target/proc || mount -t proc -- proc /target/proc
|
||
mkdir -p -- /target/sys
|
||
mountpoint -q /target/sys || mount -o bind -- /sys /target/sys
|
||
## }}}
|
||
## Run debootstrap
|
||
debootstrap --arch amd64 --include="${dbs_pkg_include}" --exclude="${dbs_pkg_exclude}" stretch /target http://ftp.fr.debian.org/debian
|
||
|
||
# }}}
|
||
# Configure system {{{
|
||
## Create the disk/partitions (eg. /dev/sda, /dev/sda1,…) on the target system {{{
|
||
### Create the disk
|
||
[ -b /target"${hdd}" ] || mknod --mode=660 /target"${hdd}" b 8 0
|
||
|
||
### Count the number of partitions on the rescue system
|
||
nb_part=$(ls -l -- "${hdd}"? | wc -l)
|
||
part=1
|
||
### Start at 1 and less/equal $nb_part
|
||
while [ "${part}" -le "${nb_part}" ]; do
|
||
#### Create the partitions on the target system
|
||
[ -b /target"${hdd}${part}" ] || mknod --mode=660 /target"${hdd}${part}" b 8 "${part}"
|
||
(( part++ ))
|
||
done
|
||
|
||
### Fix group of disk/partitions
|
||
chgrp disk -- /target"${hdd}"*
|
||
## }}}
|
||
## Create LV inodes {{{
|
||
### Count the number of LV on the rescue system
|
||
nb_lv=$(ls -l -- /dev/dm-* | wc -l)
|
||
lv=0
|
||
### Start at 0 and less than $nb_lv cause it start at 0
|
||
while [ "${lv}" -lt "${nb_lv}" ]; do
|
||
#### Create the LV on the target system
|
||
[ -b /target/dev/dm-"${lv}" ] || mknod --mode=660 /target/dev/dm-"${lv}" b 252 "${lv}"
|
||
(( lv++ ))
|
||
done
|
||
|
||
### Fix group of LV
|
||
chgrp disk -- /target/dev/dm-*
|
||
## }}}
|
||
## Create symlinks to LV {{{
|
||
### Get the list of all LV
|
||
tmp_list_lv="/tmp/lv.rescue.list"
|
||
find /dev/"${vgname}" -maxdepth 1 -type l -printf "%f\\n" > "${tmp_list_lv}"
|
||
|
||
### Create Volume Group and mapper directories
|
||
mkdir -p -- /target/dev/"${vgname}"
|
||
mkdir -p -- /target/dev/mapper
|
||
|
||
### For each LV
|
||
while IFS= read -r LV_NAME
|
||
do
|
||
|
||
#### Create symlink in Volume Group directory on the target system (eg. /dev/mapper/ovhsys/home -> ../dm-0)
|
||
[ -L /target/dev/"${vgname}/${LV_NAME}" ] || ln -fs -- $(readlink /dev/"${vgname}/${LV_NAME}") /target/dev/"${vgname}/${LV_NAME}"
|
||
|
||
#### Create symlink in mapper directory on the target system (eg. /dev/mapper/mapper/ovhsys-home -> ../dm-0)
|
||
[ -L /target/dev/mapper/"${vgname}-${LV_NAME}" ] || ln -fs -- $(readlink /dev/"${vgname}/${LV_NAME}") /target/dev/mapper/"${vgname}-${LV_NAME}"
|
||
done < "${tmp_list_lv}"
|
||
rm -f -- "${tmp_list_lv}"
|
||
|
||
## }}}
|
||
## Fstab {{{
|
||
### Use the current mtab content as default fstab file for the target system
|
||
grep target /etc/mtab > /target/etc/fstab
|
||
|
||
### Ensure to enable swap
|
||
grep swap /target/etc/fstab || echo "/dev/mapper/${vgname}-swap swap swap sw,pri=0 0 0" >> /target/etc/fstab
|
||
|
||
### Add extra tmpfs mount point
|
||
grep devtmpfs /target/etc/fstab || echo "udev /dev devtmpfs rw,relatime,size=10240k,nr_inodes=2051120,mode=755 0 0" >> /target/etc/fstab
|
||
grep "/run " /target/etc/fstab || echo "tmpfs /run tmpfs rw,size=15M,mode=755 0 0" >> /target/etc/fstab
|
||
grep "/run/lock" /target/etc/fstab || echo "tmpfs /run/lock tmpfs rw,size=5120k 0 0" >> /target/etc/fstab
|
||
grep "/run/shm" /target/etc/fstab || echo "tmpfs /run/shm tmpfs rw,nodev,size=100M 0 0" >> /target/etc/fstab
|
||
grep "/var/tmp" /target/etc/fstab || echo "/tmp /var/tmp none rw,bind,size=1M,mode=1777 0 0" >> /target/etc/fstab
|
||
|
||
### Remove all occurrences to /target
|
||
sed -i 's;/target;;g' /target/etc/fstab
|
||
## }}}
|
||
## Network {{{
|
||
net_device=$(ip r | grep "^default" | head -1 | cut -d" " -f5)
|
||
net_address=$(ip r | grep -vE "(^default|metric)" | grep "${net_device}.*src" | head -1 | awk -F" " '{print $NF}')
|
||
net_netmask=$(ipcalc "${net_address}" | awk '/Netmask:/{print $2;}')
|
||
net_broadcast=$(ip a s dev "${net_device}" | awk '/inet.*brd/{print $4}')
|
||
net_network=$(ip r | grep -vE "(^default|metric)" | grep "src ${net_address}" | head -1 | cut -d"/" -f1)
|
||
net_gateway=$(ip r | grep "^default" | head -1 | cut -d" " -f3)
|
||
|
||
printf '%b' "auto lo
|
||
iface lo inet loopback
|
||
|
||
auto ${net_device}
|
||
iface ${net_device} inet static
|
||
address ${net_address}
|
||
netmask ${net_netmask}
|
||
network ${net_network}
|
||
broadcast ${net_broadcast}
|
||
gateway ${net_gateway}
|
||
# FDN DNS - https://www.fdn.fr/actions/dns/
|
||
dns-nameserver 80.67.169.12
|
||
dns-nameserver 80.67.169.40
|
||
|
||
source-directory /etc/network/interfaces.d" > /target/etc/network/interfaces
|
||
|
||
## }}}
|
||
## Locale {{{
|
||
### Enable locale(s)
|
||
sed -i 's/^# \(en_US.UTF-8 UTF-8\)/\1/' /target/etc/locale.gen
|
||
#sed -i 's/^# \(fr_FR.UTF-8 UTF-8\)/\1/' /target/etc/locale.gen
|
||
chroot /target locale-gen
|
||
|
||
## }}}
|
||
## Timezone {{{
|
||
### Set timezone
|
||
printf '%b\n' "Europe/Paris" > /target/etc/timezone
|
||
ln -fs /usr/share/zoneinfo/Europe/Paris /target/etc/localtime
|
||
chroot /target dpkg-reconfigure --frontend noninteractive tzdata
|
||
|
||
## }}}
|
||
# }}}
|
||
# Finish {{{
|
||
## SSH {{{
|
||
### Allow root connections - this should be fixed if it works
|
||
sed -i 's/\(^\|^\#\)\(PermitRootLogin\).*/\2 yes/g' /target/etc/ssh/sshd_config
|
||
### Add current authorized_keys from the rescue system if present
|
||
if [ -f /root/.ssh/authorized_keys ]; then
|
||
cp /root/.ssh/authorized_keys /target/root/.ssh/authorized_keys
|
||
else
|
||
printf '%b\\n' "${REDB}You might want to define an authorized key for SSH/root in /target/etc/ssh/sshd_config${RESET}"
|
||
fi
|
||
## }}}
|
||
printf '%b\\n' "${REDB}Please change the root's password :${RESET}"
|
||
chroot /target passwd
|
||
|
||
printf '%b\\n' "${GREEN}The system is still available on /target but you can now try to reboot the hardware."
|
||
|
||
exit 0
|
||
# }}}
|