2019-04-02 18:07:53 +02:00
|
|
|
|
#!/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
|
2019-04-03 23:57:28 +02:00
|
|
|
|
## Computer hostname
|
|
|
|
|
new_hostname="2g"
|
2019-04-02 18:07:53 +02:00
|
|
|
|
## Volume Group name to use for LVM
|
|
|
|
|
vgname="ovhsys"
|
2019-04-02 18:30:31 +02:00
|
|
|
|
## If the script should manage the partitions (delete, add,…)
|
|
|
|
|
manage_part=0
|
2019-04-03 15:56:24 +02:00
|
|
|
|
## 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'
|
|
|
|
|
## }}}
|
2019-04-02 18:07:53 +02:00
|
|
|
|
|
2019-04-03 09:46:09 +02:00
|
|
|
|
## Package to exclude from debootstrap install
|
2019-04-06 00:18:35 +02:00
|
|
|
|
dbs_pkg_exclude="vim-tiny"
|
2019-04-03 09:46:09 +02:00
|
|
|
|
## Package to include to debootstrap install
|
2019-06-30 12:43:33 +02:00
|
|
|
|
dbs_pkg_include="aptitude,btrfs-progs,btrfs-tools,bzip2,debconf-i18n,dialog,dmsetup,htop,isc-dhcp-client,isc-dhcp-common,locales,lvm2,openssh-server,pciutils,tmux,vim-nox,wget,zsh"
|
2019-04-03 09:46:09 +02:00
|
|
|
|
|
2019-04-05 22:56:29 +02:00
|
|
|
|
# Prepare host system {{{
|
|
|
|
|
apt update
|
2019-04-06 00:18:35 +02:00
|
|
|
|
apt install -y coreutils debootstrap e2fsprogs gawk ipcalc lvm2 parted util-linux wget
|
2019-04-05 22:56:29 +02:00
|
|
|
|
# }}}
|
2019-04-02 18:07:53 +02:00
|
|
|
|
# Partitionning {{{
|
2019-04-02 18:30:31 +02:00
|
|
|
|
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
|
2019-04-02 18:07:53 +02:00
|
|
|
|
|
2019-04-02 18:30:31 +02:00
|
|
|
|
## 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
|
2019-04-06 00:18:35 +02:00
|
|
|
|
parted "${hdd}" mkpart primary 512MB 100%
|
2019-04-02 18:30:31 +02:00
|
|
|
|
parted "${hdd}" set 2 lvm on
|
|
|
|
|
sudo pvcreate "${hdd}"2
|
2019-04-02 20:15:10 +02:00
|
|
|
|
sudo vgcreate "${vgname}" "${hdd}"2
|
2019-04-02 18:30:31 +02:00
|
|
|
|
fi
|
2019-04-02 18:07:53 +02:00
|
|
|
|
|
2019-04-02 21:20:34 +02:00
|
|
|
|
mkfs.ext3 -F -L boot -- "${hdd}"1
|
2019-04-02 20:15:10 +02:00
|
|
|
|
|
2019-04-02 18:37:01 +02:00
|
|
|
|
## }}}
|
|
|
|
|
## 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
|
2019-04-02 21:20:34 +02:00
|
|
|
|
cd -- /dev/"${vgname}" || exit 1
|
|
|
|
|
for lvname in *; do
|
|
|
|
|
mkfs.ext4 -F -L "${lvname}" -- "${lvname}"
|
2019-04-02 18:37:01 +02:00
|
|
|
|
done
|
2019-04-02 21:20:34 +02:00
|
|
|
|
cd -- - || exit 1
|
2019-04-02 18:37:01 +02:00
|
|
|
|
|
|
|
|
|
### And format the swap
|
2019-04-02 21:20:34 +02:00
|
|
|
|
mkswap -L sw01 -- /dev/mapper/"${vgname}"-swap
|
2019-04-02 18:07:53 +02:00
|
|
|
|
## }}}
|
|
|
|
|
# }}}
|
2019-04-02 20:53:34 +02:00
|
|
|
|
# Debootstrap {{{
|
|
|
|
|
## Create and mount the system {{{
|
|
|
|
|
### Root
|
|
|
|
|
mkdir -p -- /target
|
|
|
|
|
mountpoint -q /target || mount -- /dev/mapper/"${vgname}"-root /target
|
2019-04-06 10:15:50 +02:00
|
|
|
|
### Prepare an fstab file
|
|
|
|
|
printf '%b' "/dev/mapper/${vgname}-root / ext4 defaults 0 0" > /tmp/target.fstab
|
2019-04-02 20:53:34 +02:00
|
|
|
|
|
|
|
|
|
### boot - grub
|
|
|
|
|
mkdir -p -- /target/boot
|
|
|
|
|
mountpoint -q /target/boot || mount -- ${hdd}1 /target/boot
|
2019-04-06 10:15:50 +02:00
|
|
|
|
printf '%b' "${hdd}1 /boot ext3 defaults 0 0" >> /tmp/target.fstab
|
2019-04-02 20:53:34 +02:00
|
|
|
|
|
|
|
|
|
### home LV
|
|
|
|
|
mkdir -p -- /target/home
|
|
|
|
|
mountpoint -q /target/home || mount -- /dev/mapper/"${vgname}"-home /target/home
|
2019-04-06 10:15:50 +02:00
|
|
|
|
printf '%b' "/dev/mapper/${vgname}-home /home ext4 defaults 0 0" >> /tmp/target.fstab
|
2019-04-02 20:53:34 +02:00
|
|
|
|
### opt LV
|
|
|
|
|
mkdir -p -- /target/opt
|
|
|
|
|
mountpoint -q /target/opt || mount -- /dev/mapper/"${vgname}"-opt /target/opt
|
2019-04-06 10:15:50 +02:00
|
|
|
|
printf '%b' "/dev/mapper/${vgname}-opt /opt ext4 defaults 0 0" >> /tmp/target.fstab
|
2019-04-02 20:53:34 +02:00
|
|
|
|
### srv LV
|
|
|
|
|
mkdir -p -- /target/srv
|
|
|
|
|
mountpoint -q /target/srv || mount -- /dev/mapper/"${vgname}"-srv /target/srv
|
2019-04-06 10:15:50 +02:00
|
|
|
|
printf '%b' "/dev/mapper/${vgname}-srv /srv ext4 defaults 0 0" >> /tmp/target.fstab
|
2019-04-02 20:53:34 +02:00
|
|
|
|
#### bkp LV
|
|
|
|
|
mkdir -p -- /target/srv/backup
|
|
|
|
|
mountpoint -q /target/srv/backup || mount -- /dev/mapper/"${vgname}"-bkp /target/srv/backup
|
2019-04-06 10:15:50 +02:00
|
|
|
|
printf '%b' "/dev/mapper/${vgname}-bkp /srv/backup ext4 defaults 0 0" >> /tmp/target.fstab
|
2019-04-02 20:53:34 +02:00
|
|
|
|
### tmp LV
|
|
|
|
|
mkdir -p -- /target/tmp
|
2019-04-06 10:15:50 +02:00
|
|
|
|
chmod 0777 -- /target/tmp
|
2019-04-02 20:53:34 +02:00
|
|
|
|
mountpoint -q /target/tmp || mount -- /dev/mapper/"${vgname}"-tmp /target/tmp
|
2019-04-06 10:15:50 +02:00
|
|
|
|
printf '%b' "/dev/mapper/${vgname}-tmp /tmp ext4 defaults 0 0" >> /tmp/target.fstab
|
2019-04-02 20:53:34 +02:00
|
|
|
|
### usr LV
|
|
|
|
|
mkdir -p -- /target/usr
|
|
|
|
|
mountpoint -q /target/usr || mount -- /dev/mapper/"${vgname}"-usr /target/usr
|
2019-04-06 10:15:50 +02:00
|
|
|
|
printf '%b' "/dev/mapper/${vgname}-usr /usr ext4 defaults 0 0" >> /tmp/target.fstab
|
2019-04-02 20:53:34 +02:00
|
|
|
|
### var LV
|
|
|
|
|
mkdir -p -- /target/var
|
|
|
|
|
mountpoint -q /target/var || mount -- /dev/mapper/"${vgname}"-var /target/var
|
2019-04-06 10:15:50 +02:00
|
|
|
|
printf '%b' "/dev/mapper/${vgname}-var /var ext4 defaults 0 0" >> /tmp/target.fstab
|
2019-04-02 20:53:34 +02:00
|
|
|
|
#### vz LV
|
|
|
|
|
mkdir -p -- /target/var/lib/vz
|
|
|
|
|
mountpoint -q /target/var/lib/vz || mount -- /dev/mapper/"${vgname}"-vz /target/var/lib/vz
|
2019-04-06 10:15:50 +02:00
|
|
|
|
printf '%b' "/dev/mapper/${vgname}-vz /var/lib/vz ext4 defaults 0 0" >> /tmp/target.fstab
|
2019-04-02 20:53:34 +02:00
|
|
|
|
|
|
|
|
|
### Swap
|
|
|
|
|
swapon -- /dev/mapper/"${vgname}"-swap
|
|
|
|
|
## }}}
|
2019-04-03 09:46:09 +02:00
|
|
|
|
## Run debootstrap
|
|
|
|
|
debootstrap --arch amd64 --include="${dbs_pkg_include}" --exclude="${dbs_pkg_exclude}" stretch /target http://ftp.fr.debian.org/debian
|
2019-04-02 20:53:34 +02:00
|
|
|
|
|
|
|
|
|
# }}}
|
2019-04-03 10:41:51 +02:00
|
|
|
|
# Configure system {{{
|
2019-04-03 16:34:29 +02:00
|
|
|
|
## Fstab {{{
|
2019-04-06 10:15:50 +02:00
|
|
|
|
### Copy the temp fstab file to target
|
|
|
|
|
cp -- /tmp/target.fstab /target/etc/fstab
|
2019-04-03 18:38:37 +02:00
|
|
|
|
## }}}
|
2019-04-05 23:52:17 +02:00
|
|
|
|
## Ensure to (re)mount devices for chroot {{{
|
|
|
|
|
mkdir -p -- /target/dev
|
2019-04-05 17:25:38 +02:00
|
|
|
|
mountpoint -q /target/dev/ || mount -t devtmpfs -- none /target/dev
|
2019-04-05 23:52:17 +02:00
|
|
|
|
mkdir -p -- /target/dev/pts
|
2019-04-05 17:25:38 +02:00
|
|
|
|
mountpoint -q /target/dev/pts || mount -t devpts -- /dev/pts /target/dev/pts
|
2019-04-05 23:52:17 +02:00
|
|
|
|
mkdir -p -- /target/proc
|
2019-04-05 17:25:38 +02:00
|
|
|
|
mountpoint -q /target/proc || mount -t proc -- none /target/proc
|
2019-04-05 23:52:17 +02:00
|
|
|
|
mkdir -p -- /target/sys
|
2019-04-05 17:25:38 +02:00
|
|
|
|
mountpoint -q /target/sys || mount -t sysfs -- none /target/sys
|
|
|
|
|
## }}}
|
2019-04-03 18:38:37 +02:00
|
|
|
|
## Network {{{
|
2019-04-05 13:36:52 +02:00
|
|
|
|
### Get all informations from current network configuration in rescue mode
|
2019-04-03 18:38:37 +02:00
|
|
|
|
net_device=$(ip r | grep "^default" | head -1 | cut -d" " -f5)
|
2019-04-05 23:52:17 +02:00
|
|
|
|
#### TODO: Switch to ip a to get ip address
|
2019-04-03 18:38:37 +02:00
|
|
|
|
net_address=$(ip r | grep -vE "(^default|metric)" | grep "${net_device}.*src" | head -1 | awk -F" " '{print $NF}')
|
2019-04-05 13:36:52 +02:00
|
|
|
|
read -r net_mac_address </sys/class/net/"${net_device}"/address
|
2019-04-03 18:38:37 +02:00
|
|
|
|
net_netmask=$(ipcalc "${net_address}" | awk '/Netmask:/{print $2;}')
|
2019-04-05 13:36:52 +02:00
|
|
|
|
net_netmask_cidr=$(ipcalc "${net_address}" | awk '/Netmask:/{print $4;}')
|
2019-04-03 18:38:37 +02:00
|
|
|
|
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)
|
|
|
|
|
|
2019-04-05 13:36:52 +02:00
|
|
|
|
### Create a network unit for systemd-networkd
|
|
|
|
|
printf '%b' "[Match]
|
|
|
|
|
MACAddress=${net_mac_address}
|
|
|
|
|
|
|
|
|
|
[Network]
|
|
|
|
|
Description=network interface with default route without dhcp
|
|
|
|
|
DHCP=no
|
|
|
|
|
Address=${net_address}/${net_netmask_cidr}
|
|
|
|
|
Gateway=${net_gateway}
|
|
|
|
|
IPv6AcceptRA=no
|
|
|
|
|
DNS=80.67.169.12
|
|
|
|
|
" > /target/etc/systemd/network/50-default.network
|
|
|
|
|
|
|
|
|
|
### Ensure to enable systemd-networkd at startup
|
|
|
|
|
chroot /target systemctl enable systemd-networkd
|
2019-04-03 18:38:37 +02:00
|
|
|
|
|
2019-04-03 18:52:30 +02:00
|
|
|
|
## }}}
|
|
|
|
|
## 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
|
|
|
|
|
|
2019-04-03 23:13:29 +02:00
|
|
|
|
## }}}
|
|
|
|
|
## 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
|
|
|
|
|
|
2019-04-03 23:13:44 +02:00
|
|
|
|
## }}}
|
|
|
|
|
## Kernel and Grub {{{
|
|
|
|
|
chroot /target aptitude install --assume-yes --without-recommends -- linux-image-amd64 grub-pc
|
|
|
|
|
chroot /target grub-install "${hdd}"
|
|
|
|
|
chroot /target update-grub
|
|
|
|
|
|
2019-04-03 23:57:28 +02:00
|
|
|
|
## }}}
|
|
|
|
|
## Hostname {{{
|
|
|
|
|
printf '%b\n' "${new_hostname}" > /target/etc/hostname
|
2019-04-05 23:52:17 +02:00
|
|
|
|
#printf '%b\n' "127.0.0.1 ${new_hostname}" >> /target/etc/hosts
|
2019-04-03 23:57:28 +02:00
|
|
|
|
|
2019-04-03 15:43:22 +02:00
|
|
|
|
## }}}
|
2019-04-03 10:41:51 +02:00
|
|
|
|
# }}}
|
2019-04-03 15:56:24 +02:00
|
|
|
|
# Finish {{{
|
2019-04-05 13:37:22 +02:00
|
|
|
|
## Call a latecommand script {{{
|
|
|
|
|
wget -O /tmp/latecommand.tar.gz "https://git.ipr.univ-rennes1.fr/cellinfo/tftpboot/raw/master/scripts/latecommand.tar.gz" --no-check-certificate
|
|
|
|
|
tar xzf /tmp/latecommand.tar.gz -C /target/tmp/
|
2019-04-05 17:24:22 +02:00
|
|
|
|
chroot /target /bin/sh /tmp/latecommand/post.stretch.sh
|
2019-04-05 13:37:22 +02:00
|
|
|
|
|
|
|
|
|
## }}}
|
2019-04-03 17:03:48 +02:00
|
|
|
|
## 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
|
2019-04-05 17:43:42 +02:00
|
|
|
|
mkdir -p -- /target/root/.ssh
|
2019-04-03 23:44:53 +02:00
|
|
|
|
cp -- /root/.ssh/authorized_keys /target/root/.ssh/authorized_keys
|
2019-04-03 17:03:48 +02:00
|
|
|
|
else
|
2019-04-03 23:44:53 +02:00
|
|
|
|
printf '%b\n' "${REDB}You might want to define an authorized key for SSH/root in /target/etc/ssh/sshd_config${RESET}"
|
2019-04-03 17:03:48 +02:00
|
|
|
|
fi
|
|
|
|
|
## }}}
|
2019-04-03 23:44:53 +02:00
|
|
|
|
printf '%b\n' "${REDB}Please change the root's password :${RESET}"
|
2019-04-03 15:56:24 +02:00
|
|
|
|
chroot /target passwd
|
|
|
|
|
|
2019-04-05 22:56:29 +02:00
|
|
|
|
# Ensure to umount everything
|
|
|
|
|
#umount /target/var/lib/vz/ /target/var/ /target/usr/ /target/tmp/ /target/sys/ /target/srv/backup/ /target/srv/ /target/proc/ /target/opt/ /target/home/ /target/dev/pts/ /target/dev /target/boot/ /target/
|
|
|
|
|
|
2019-04-03 23:44:53 +02:00
|
|
|
|
printf '%b\n' "${GREEN}The system is still available on /target but you can now try to reboot the hardware.${RESET}"
|
2019-04-03 15:56:24 +02:00
|
|
|
|
|
|
|
|
|
exit 0
|
|
|
|
|
# }}}
|