logo一言堂

How To Install Linux From Scratch

One can install Linux from the installer, but the process is not very customizable. Sometimes you want to do it yourself. The following is Debian specific but the knowledge is applicable elsewhere.

Build a Image

First you need to have a working box. You can clone the root partition to make an image, or you can use bootstrap a minimal Debian system using:

 cdebootstrap wheezy /mnt/vol2/ http://mirrors.163.com/debian/
 chroot /mnt/vol2/ /bin/bash --login
 mount -t proc proc /proc

/mnt/vol2 here is a ext4 fs, LVM is your friend. Now chroot into it and start install all the software you want with apt-get. Some software's installation scripts are not very friendly in a chroot environment:

  • kernels and grub. see: /etc/kernel/postinst.d/zz-update-grub

Things to Change

There are a few thing the installer do but now you need to do it your self:

root passwd

passwd

fstab

# <file system> <mount point>   <type>  <options>       <dump>  <pass>

timezone

ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
echo "Asia/Shanghai" > /etc/timezone

hostname

echo HOSTNAME > /etc/hostname
# add 127.0.1.1 HOSTNAME in /etc/hosts

network

network probably need to be installed at the new machine.

Write an Image

you can write an image file by:

partclone.extfs -c -s FROM_DEVICE -o my.img

The image you have is already good enough for virtualization. for Real physical machine, you need to do more.

Boot the new machine using system_rescue CD (netboot or USB). Once you are in, partition and LVM to your liking. then write the image:

curl http://xxx/my.img | partclone.extfs -r -o /dev/YYY
fsck.ext4 /dev/YYY
resize2fs /dev/YYY

I suggest to use a seperate partition for grub (this is needed for GPT partiton scheme) and another for /boot. The /boot need to be simple like ext2. It is ok to be empty.

Ok, now you can chroot into it:

chroot /mnt/vol2/ /bin/bash --login
 

tweak more, and one thing you need to do is to install grub.

grub-install /dev/sda

And you reboot.

First boot

Most likely grub does not work now. so you drop into a grub shell and do:

linux (hd0,gpt2)/vm...gz root=/dev/YYY
initrd (hd0,gpt2)/initrd...
boot

Again, boot may not complete for things like LVM. you will get a initramfs shell:

vgchange -ay YOUR_VG

Now, it boot! Once you are in, remember to redo the boot stuff:

update-initramfs -k all -u
grub-mkconfig -o /boot/grub/grub.cfg
grub-install /dev/sda

Then setup your network. You can either use network-manager or manual setting.