#!/usr/bin/perl
# [x] resize root filesystem
-# [ ] find boot partition
-# [ ] generate fstab
-# [ ] mount boot
-# [ ] extract wifi firmware
-# [ ] reloads the kernel modules
+# [x] find root fs uuid
+# [x] find boot partition
+# [x] generate fstab
+# [x] mount /boot/efi
# [ ] install grub
-# [ ] disables itself
+# [ ] extract wifi firmware
+# [ ] reboots if grub or wifi firmware has changed
sub
find_root_device
die("Could not find root device");
}
-my $root_block_device = find_root_device();
+sub
+find_fs_uuid_of_device
+{
+ my $dev = shift || die;
+ my $blkid_output = `blkid ${dev}`;
+ # /dev/nvme0n1p5: LABEL="/" UUID="fe9c5ac8-edf4-442e-a09e-e0451606898e" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="primary" PARTUUID="03378b79-346d-42f9-b404-44b22bc6798f"
+ if ($blkid_output =~ /UUID="([^"]+)"/) {
+ return $1;
+ }
+
+ die("Could not find fs uuid of $dev");
+}
+
+sub
+find_efi_parition
+{
+ my $uuid_in_grub_cfg = shift || die;
+ my @candidates;
+
+ my $efi_parition = undef;
+
+ for (`blkid`) {
+ if (/^([\S]+):.*TYPE="vfat"/) {
+ push(@candidates, $1);
+ }
+ }
-system("resize2fs $root_block_device");
+ for my $dev (@candidates) {
+ system("mount -o ro $dev /mnt");
+ if (-f '/mnt/EFI/boot/grub.cfg') {
+ open(GRUBCFG, '<', '/mnt/EFI/boot/grub.cfg') || die ("Can't open /mnt/EFI/boot/grub.cfg: $!");
+ my @lines = <GRUBCFG>;
+ for (@lines) {
+ if (/${uuid_in_grub_cfg}/) {
+ $efi_parition = $dev;
+ }
+ }
+ close(GRUBCFG);
+ }
+ system("umount /mnt");
+ last if defined $efi_parition;
+ }
+
+ die ("No efi parition found") unless defined $efi_parition;
+
+ return $efi_parition;
+}
+
+sub
+generate_fstab
+{
+ my $root_fs_uuid = shift || die;
+ my $efi_fs_uuid = shift || die;
+
+ open(FSTAB, '>', '/etc/fstab') || die ("Can not open fstab");
+ print FSTAB <<EOF;
+
+UUID="$root_fs_uuid" / ext4 defaults 0 0
+UUID="$efi_fs_uuid" /boot/efi vfat defaults 0 0
+EOF
+ close(FSTAB);
+}
+
+my $root_block_device = undef;
+my $root_fs_uuid = undef;
+my $efi_block_device = undef;
+my $efi_fs_uuid = undef;
+
+unless (-f '/etc/fstab') {
+ $root_block_device = find_root_device();
+ system("resize2fs $root_block_device");
+ $root_fs_uuid = find_fs_uuid_of_device($root_block_device);
+ $efi_block_device = find_efi_parition($root_fs_uuid);
+ $efi_fs_uuid = find_fs_uuid_of_device($efi_block_device);
+ generate_fstab($root_fs_uuid, $efi_fs_uuid);
+ system('mount /boot/efi');
+}