Tuesday, June 11, 2013

Installing Debian Wheezy from a Debian rescue environment using debootstrap

Intro

You might find this guide useful if you need complete control over your Debian install process, but your housing provider can only provide you with rescue mode boot. For example: your housing provider has no Debian install images which do LVM by default. If they do offer some kind of rescue system (especially if it's Debian) then you are in luck!

This guide has been tested with Debian Wheezy 7.0.0 net-install image (Rescue)

Notice

I'm not responsible for any and all damage done by following this guide. A fair level of GNU/Linux familiarity is expected of the reader. Extra Debian knowledge can't hurt either. Practicing this guide on a VirtualBox guest (with bridged networking) before trying it live is strongly advised.

Prerequisites

You are somewhat experienced with GNU/Linux and familiar with Debian installation process. You read manpages before you Google, and have at least heard of debootstrap.
You know your networking configuration for your machine in question (Virtual or otherwise).
You know that if you mess up, you might end up being cut off from your remote machine.

Ok? Let's start.

Debian rescue environment

If you are trying this on VirtualBox/VMware, you need to boot your CD. When the menu pops up, choose: Advanced Options -> Rescue Mode

In case you have a hosting provider, you might just be given access to it straight away.

Debian installer options

Configure your networking via prompts
Locale, and other options are not relevant and won't be included in our newly installed system. We will configure almost everything by hand.
You might get a warning that there are "No partitions found". Either way, make sure you pick: "Execute a shell in the installer environment"

Partitioning

The following partitioning scheme will be used for the purpose of this guide:
/dev/sda1, type Linux, size 512MB
/dev/sda2, type LVM, size <everything else>

As I said in the introduction, I expect the reader to be somewhat GNU/Linux ready. This means that you know how to use fdisk to do this basic type of partitioning.

To get you started:
fdisk /dev/sda

Press "h" for help, read the manpages, and use Google as your last resource (for examples)

LVM

Just like partitioning, this LVM setup will be used for the remainder of this guide

vgcreate mainvg /dev/sda2
lvcreate mainvg -L 1G -n root
lvcreate mainvg -L 1G -n swap
lvcreate mainvg -L 1G -n usr
lvcreate mainvg -L 1G -n tmp
lvcreate mainvg -L 1G -n srv
lvcreate mainvg -L 1G -n var
lvcreate mainvg -L 1G -n home

You can adjust the sizes (1G), volume group name (mainvg) and logical volume names (root,swap...) as you see fit... example:

lvcreate reallycoolvg -L 512M -n myhome

File systems

mkswap /dev/mainvg/swap
mkfs.ext2 /dev/sda1
mkfs.ext4 /dev/mainvg/root
mkfs.ext4 /dev/mainvg/usr
mkfs.ext4 /dev/mainvg/tmp
mkfs.ext4 /dev/mainvg/srv
mkfs.ext4 /dev/mainvg/var
mkfs.ext4 /dev/mainvg/home

You can use whatever filesystem you like. Just be careful to use whatever partition names you chose before and to set it accordingly in fstab later!

We have to mount root first

mount /dev/mainvg/root /mnt  

Basic dir structure

mkdir /mnt/boot
mkdir /mnt/usr
mkdir /mnt/tmp
mkdir /mnt/srv
mkdir /mnt/var
mkdir /mnt/home

Activate swap

swapon -a

Mount our partitions

mount /dev/sda1 /mnt/boot
mount /dev/mainvg/usr /mnt/usr
mount /dev/mainvg/tmp /mnt/tmp
mount /dev/mainvg/srv /mnt/srv
mount /dev/mainvg/var /mnt/var
mount /dev/mainvg/home /mnt/home

Bootstrap

We want locales set up, unless you like seeing Perl errors everywhere. Those editors (vim, joe) are just my preference. You will have nano installed by default.
Use whatever working mirror you like. Don't go overboard with the initial packages. Better save your list of favorite packages for later, when you can copy and paste with ease.

debootstrap --include=openssh-server,vim,joe,locales wheezy /mnt http://ftp.de.debian.org/debian

Chroot

cd /mnt
mount -t proc proc proc/
mount -t sysfs sys sys/
mount -o bind /dev dev/
mount -t devpts pts dev/pts/
chroot /mnt /bin/bash

Basic stuff

Root password:

passwd

Create your non-root user:

useradd -c "This is my account" -m -s /bin/bash -d /home/userguy userguy

Don't forget to set a password

passwd userguy

Optional

vim /etc/ssh/sshd_config

Find the following directive and set it like this:

PermitRootLogin no

Now that we are somewhat safer, it's time to start sshd in our chroot environment, so we can copy and paste with ease.

service ssh start

Now it's time to SSH in. Use the "userguy" user you created earlier, and switch to root:

su -

Initial system configuration

Let's edit our sources.list. If you know what you are doing, edit this to your satisfaction:

vim /etc/apt/sources.list

This is the contents of our example content:

deb http://ftp.de.debian.org/debian/ wheezy main contrib non-free
deb-src http://ftp.de.debian.org/debian wheezy main contrib non-free
deb http://security.debian.org/ wheezy/updates main contrib non-free
deb-src http://security.debian.org/ wheezy/updates main contrib non-free

We need to configure our locales. If you don't know what to pick, then go with en_US.UTF-8. You will get a second question, and again select your desired locale (en_US.UTF-8)

dpkg-reconfigure locales

Configure your timezone:
dpkg-reconfigure tzdata

Set your hostname
vim /etc/hostname

We configured networking for our rescue environment, but those settings don't get picked up by debootstrap. You need to configure your network manually here.
Use your own IP address / subnet / gateway

vim /etc/network/interfaces

Example content:

# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
        address 192.168.1.10
        netmask 255.255.255.0
        gateway 192.168.1.1


Set your DNS servers here
vim /etc/resolv.conf

Content:

nameserver 8.8.8.8
nameserver 8.8.4.4

Edit your fstab
vim /etc/fstab

Content:

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
/dev/mapper/mainvg-root  /   ext4 defaults                 0       1
# /boot was on /dev/sda1 during installation
/dev/sda1                /boot  ext2         defaults      0       2
/dev/mapper/mainvg-home  /home  ext4         nosuid,nodev  0       2
/dev/mapper/mainvg-srv   /srv   ext4         nosuid,nodev  0       2
/dev/mapper/mainvg-tmp   /tmp   ext4         nosuid,nodev  0       2
/dev/mapper/mainvg-usr   /usr   ext4         defaults      0       2
/dev/mapper/mainvg-var   /var   ext4         nosuid,nodev  0       2
/dev/mapper/mainvg-swap  none   swap         nosuid,nodev  0       0
/dev/sr0        /media/cdrom0   udf,iso9660  user,noauto   0       0

You might consider using UUID= for your swap partition. Use blkid /dev/mainvg/swap

Let's update our system

apt-get update && apt-get upgrade

Install some basic stuff

apt-get install lvm2 grub firmware-linux-nonfree firmware-linux-free sudo ntp linux-image-amd64 less

Note:
"firmware-*" is optional, but some hardware components might need it. 
"sudo", "less" and "ntp" are also optional. You can get rid of them if you don't need them.
Use the appropriate kernel architecture for your arch.

GRUB

Note: I hope you tested this procedure on a VirtualBox/VMware box first. If you have other partitions on a live system, be VERY careful how you type the following commands:

mkdir /boot/grub

update-grub

grub-install --root-directory=/ /dev/sda


Log out from your SSH session

Log out from your chroot environment

Reboot from rescue shell

Don't boot from CD

Hopefully, you are now looking at your Wheezy loading up. 

Post 1st boot

Take notes of any errors. 

As soon as the system boots, ssh in and check system logs for errors:

less /var/log/syslog

Check if all mounts are good:

mount

Check if swap is there:

free

Some people like to see what else they can configure with dpkg-reconfigure:
Run this command if you'd like to know what else you can reconfigure with it:

ls /var/lib/dpkg/info/*.templates|xargs -n 1 basename|sed -e "s/.templates$//"

Get your firewall up and running, and do the stuff you usually do.

I'm happy to take any suggestions and further improve this guide on your input.

Thank you for reading :)