Skip to content

Instantly share code, notes, and snippets.

@PerchunPak
Created April 4, 2024 16:06
Show Gist options
  • Save PerchunPak/94a54415887c3a0a89c00a875c4cd204 to your computer and use it in GitHub Desktop.
Save PerchunPak/94a54415887c3a0a89c00a875c4cd204 to your computer and use it in GitHub Desktop.
Use disko to partition your disk without deleting existing data
diff --git a/nix/store/cn2l58x1hh512ijrhqq0ygadbny55pc4-disko b/disko
index 11f1a1e..8f547af 100755
--- a/nix/store/cn2l58x1hh512ijrhqq0ygadbny55pc4-disko
+++ b/disko
@@ -3,9 +3,6 @@ export PATH=/nix/store/1kql9l6sh9qwkfac720fl9a2d4lf78r3-jq-1.7.1-bin/bin:/nix/st
umount -Rv "/mnt" || :
-# shellcheck disable=SC2043
-for dev in /dev/vda; do
- /nix/store/nxykihliwdnlmp6apf2ivi0vnfv9ddak-disk-deactivate/disk-deactivate "$dev"
-done
set -efux
@@ -24,46 +21,15 @@ mkdir -p "$disko_devices_dir"
efiGptPartitionFirst='1'
type='gpt'
- if ! blkid "/dev/vda" >/dev/null; then
if sgdisk \
- --info=1 \
- /dev/vda > /dev/null 2>&1
- then
- sgdisk \
- --align-end \
- --new=1:0:+1M \
- --change-name=1:disk-main-boot \
- --typecode=1:EF02 \
- /dev/vda
- # ensure /dev/disk/by-path/..-partN exists before continuing
- partprobe /dev/vda
- udevadm trigger --subsystem-match=block
- udevadm settle
- fi
- if sgdisk \
- --info=2 \
- /dev/vda > /dev/null 2>&1
- then
- sgdisk \
- --align-end \
- --new=2:0:+500M \
- --change-name=2:disk-main-ESP \
- --typecode=2:EF00 \
- /dev/vda
- # ensure /dev/disk/by-path/..-partN exists before continuing
- partprobe /dev/vda
- udevadm trigger --subsystem-match=block
- udevadm settle
- fi
- if sgdisk \
--info=3 \
/dev/vda > /dev/null 2>&1
then
sgdisk \
--align-end \
- --new=3:0:+0 \
- --change-name=3:disk-main-fedora_boot \
- --typecode=3:8300 \
+ --new=3:0:+1M \
+ --change-name=3:disk-main-boot \
+ --typecode=3:EF02 \
/dev/vda
# ensure /dev/disk/by-path/..-partN exists before continuing
partprobe /dev/vda
@@ -76,9 +42,9 @@ mkdir -p "$disko_devices_dir"
then
sgdisk \
--align-end \
- --new=4:0:+0 \
- --change-name=4:disk-main-fedora_root \
- --typecode=4:8300 \
+ --new=4:0:+500M \
+ --change-name=4:disk-main-ESP \
+ --typecode=4:EF00 \
/dev/vda
# ensure /dev/disk/by-path/..-partN exists before continuing
partprobe /dev/vda
@@ -119,7 +85,6 @@ mkdir -p "$disko_devices_dir"
- fi
( # filesystem /dev/disk/by-partlabel/disk-main-ESP vfat /boot #

this formats disk

-# shellcheck disable=SC2043
-for dev in /dev/vda; do
-  /nix/store/nxykihliwdnlmp6apf2ivi0vnfv9ddak-disk-deactivate/disk-deactivate "$dev"
-done

this check for some reason failed and totally skipped execution of creation of partitions (does it check whether there are any partitions on a disk?)

if ! blkid "/dev/vda" >/dev/null; then

I modified my disko.nix to include 2 (this equals to amount of already claimed partitions in lsblk) additional empty paritions (fedora_boot and fedora_root), so disko would use proper indexes for partitions

diff --git a/modules/nixos/disko.nix b/modules/nixos/disko.nix
index 04d7ded..e9d4342 100644
--- a/modules/nixos/disko.nix
+++ b/modules/nixos/disko.nix
@@ -6,6 +6,8 @@
       content = {
         type = "gpt";
         partitions = {
+         fedora_boot = {};
+         fedora_root = {};
           boot = {
             name = "boot";
             size = "1M";

And because disko placed ESP and nixos' boot partitions before my fedora, I manualy deleted fedora partitions from the script and changed indexes of ESP and boot partitions.

(hand made diff)

       if sgdisk \
-      --info=1 \
+      --info=3 \
       /dev/vda > /dev/null 2>&1
     then
       sgdisk \
         --align-end \
-        --new=1:0:+1M \
-        --change-name=1:disk-main-boot \
-        --typecode=1:EF02 \
+        --new=3:0:+1M \
+        --change-name=3:disk-main-boot \
+        --typecode=3:EF02 \
         /dev/vda
       # ensure /dev/disk/by-path/..-partN exists before continuing
       partprobe /dev/vda
       udevadm trigger --subsystem-match=block
       udevadm settle
     fi
     if sgdisk \
-      --info=2 \
+      --info=4 \
      /dev/vda > /dev/null 2>&1
    then
      sgdisk \
        --align-end \
-        --new=2:0:+500M \
-        --change-name=2:disk-main-ESP \
-        --typecode=2:EF00 \
+        --new=4:0:+500M \
+        --change-name=4:disk-main-ESP \
+        --typecode=4:EF00 \
         /dev/vda
       # ensure /dev/disk/by-path/..-partN exists before continuing
       partprobe /dev/vda
       udevadm trigger --subsystem-match=block
       udevadm settle
     fi
-    if sgdisk \
-      --info=3 \
-      /dev/vda > /dev/null 2>&1
-    then
-      sgdisk \
-        --align-end \
-        --new=3:0:+0 \
-        --change-name=3:disk-main-fedora_boot \
-        --typecode=3:8300 \
-        /dev/vda
-      # ensure /dev/disk/by-path/..-partN exists before continuing
-      partprobe /dev/vda
-      udevadm trigger --subsystem-match=block
-      udevadm settle
-    fi
-    if sgdisk \
-      --info=4 \
-      /dev/vda > /dev/null 2>&1
-    then
-      sgdisk \
-        --align-end \
-        --new=4:0:+0 \
-        --change-name=4:disk-main-fedora_root \
-        --typecode=4:8300 \
-        /dev/vda
-      # ensure /dev/disk/by-path/..-partN exists before continuing
-      partprobe /dev/vda
-      udevadm trigger --subsystem-match=block
-      udevadm settle
-    fi

if you (for whatever reason) want to reproduce disko file, I used:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment