Use Ansible to migrate all my configuration and have a new Linux OS running in less that 20 minutes.
The first steps are actually external to the automatic configuration, as some of them need to have the OS running so that they can be executed with Ansible.
Select the disk where the partitions are going to be created:
fdisk -l
fdisk /dev/sdX
Create partitions root
, swap
and boot
, and assign their types:
- New partition -> Primary -> Partition number -> First sector -> Last sector (offset)
- Change partition type -> Partition number -> Type of partition:
83
- Linux filesystem82
- Linux swapef
- UEFI FAT 16/32
n p 1 <enter> +300M # Hex code ef00
t 1 ef <enter>
n p 2 <enter> +250M # Hex code 8300
t 2 83 <enter>
n p 3 <enter> +500G (to be encrypted) Hex code 8300
t 3 83 <enter>
Format partitions:
mkfs.fat -F 32 /dev/sdX1
mkfs.ext4 /dev/sdX2
Setup the encryption of the system:
cryptsetup --cipher aes-xts-plain64 --verify-passphrase --use-random luksFormat /dev/sdX3
cryptsetup luksOpen /dev/sdX3 luks
Create encrypted partitions - this creates on partition for root, modify if need /home
or other partitions separate.
pvcreate /dev/mapper/luks
vgcreate vg0 /dev/mapper/luks
lvcreate --size 8G vg0 --name swap
lvcreate -l +100%FREE vg0 --name root
Create filesystems on encrypted partitions:
mkfs.ext4 /dev/mapper/vg0-root
mkswap /dev/mapper/vg0-swap
mount /dev/mapper/vg0-root /mnt # /mnt is the installed system
swapon /dev/mapper/vg0-swap # Not needed but a good thing to test
mkdir /mnt/boot
mount /dev/sdX2 /mnt/boot
mkdir /mnt/boot/efi
mount /dev/sdX1 /mnt/boot/efi
pacstrap /mnt base base-devel git vim lvm2 grub-efi-x86_64 efibootmgr os-prober linux linux-firmware
genfstab -pU /mnt >> /mnt/etc/fstab
tmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0
# Make /tmp a ramdisk (add the following line to /mnt/etc/fstab)
# Change relatime on all non-boot partitions to noatime (reduces wear if using an SSD)
arch-chroot /mnt /bin/bash
ln -s /usr/share/zoneinfo/Europe/Madrid /etc/localtime
hwclock --systohc --utc
echo HOSTNAME > /etc/hostname
echo LANG=en_US.UTF-8 >> /etc/locale.conf
echo LANGUAGE=en_US >> /etc/locale.conf
echo LC_ALL=C >> /etc/locale.conf
passwd
vim /etc/mkinitcpio.conf
# Add 'ext4' to MODULES
# Add 'encrypt' and 'lvm2' to HOOKS before filesystems
# Regenerate initrd image
mkinitcpio -p linux
grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-ud=GRUB
# In /etc/default/grub:
# - edit the line GRUB_CMDLINE_LINUX to GRUB_CMDLINE_LINUX="cryptdevice=/dev/sdX3:luks:allow-discards"
# - uncomment GRUB_DISABLE_OS_PROBER=false
# then run:
grub-mkconfig -o /boot/grub/grub.cfg
Finally, you need to copy this repository and run the playbook
git clone https://github.com/PabloAceG/20min-migration.git
ansible-playbook run.yml
# Exit the newly created and configured system
exit
Unmount all partitions and then reboot:
umount -R /mnt swapoff -a