[ale] Non-interactive disk partitioning?

DJPfulio at jdpfu.com DJPfulio at jdpfu.com
Fri Jan 20 14:32:17 EST 2023


On 1/20/23 14:11, Leam Hall via Ale wrote:
> Everything I've looked at, parted, fdisk, sfdisk, seems to either want to be interactive, or doesn't explain how to turn the whole disk into a partition. Any suggestions?
> 
> Ideally this will go into an ansible script, but I'm trying to make it work by hand first.
> 
> Leam

You can pass a machine readable file into sfdisk to be used to create partition tables with partitioning as you like.  Typically, I use sfdisk to dump the partitioning scheme I want to a file. Then use that file as the input.

It is also possible to clone a partitioning setup with sfdisk.

sgdisk can also be used.  This is the tool I use during most installs, before the 'disk setup' part of the install to pre-setup partitions and LVM in the ways I prefer.  Then, in the installation tool, I only need to connect the partitions/LVs to the specific mount and file systems as desired.  Some distros have really, really, bad disk setup screens.

Some examples:
   sgdisk --clear $DEV   # create fresh GPT partition table on the device
   sgdisk --new=1:0:+1M        --typecode=0:ef02 --change-name=0:GRUB   $DEV
   sgdisk --new=2:0:+${SZ_EFI}  --typecode=0:ef00 --change-name=0:EFI-SP $DEV
   sgdisk --new=3:0:+${SZ_BOOT} --typecode=0:8300 --change-name=0:boot   $DEV
   sgdisk --largest-new=4 --typecode=0:8e00 --change-name=0:LVM    $DEV

   sgdisk --typecode=1:ef02 --typecode=2:ef00 --typecode=3:8300 \
          --typecode=4:8e00 $DEV
   sgdisk --change-name=1:GRUB --change-name=2:EFI-SP --change-name=3:boot \
          --change-name=4:LVM $DEV
   sgdisk --hybrid 1:2:3:4 $DEV
   sgdisk --print $DEV

   pvcreate ${DEV}4
   vgcreate ${VG_NAME} ${DEV}4
   lvcreate -L ${SZ_swap} -n swap ${VG_NAME}
   lvcreate -L ${SZ_root} -n root ${VG_NAME}
   lvcreate -L ${SZ_var}  -n var ${VG_NAME}
   lvcreate -L ${SZ_tmp}  -n tmp ${VG_NAME}
   lvcreate -L ${SZ_home} -n home ${VG_NAME}

   # create the mapper links
   lvscan
   
   # lay down some file systems
   mkfs -t ext2 ${DEV}1  # not sure what this is about.
   mkfs -t vfat ${DEV}2  # /boot/efi
   mkfs -t ext2 ${DEV}3  # /boot
   mkswap  /dev/${VG_NAME}/swap  # swap (this was activated by the installer)
   mkfs -t ext4 /dev/${VG_NAME}/root  # /
   e2label /dev/${VG_NAME}/root root

   mkfs -t ext4 /dev/${VG_NAME}/home  # /home
   e2label /dev/${VG_NAME}/home home

   mkfs -t ext4 /dev/${VG_NAME}/var  # /var
   e2label /dev/${VG_NAME}/var  var

   mkfs -t ext4 /dev/${VG_NAME}/tmp  # /tmp
   e2label /dev/${VG_NAME}/tmp  tmp


That should get you started.
Some distros have a nice, automated too for disk setups. Alas, the Ubuntu one has been changing for the worst the last 5 yrs, forcing my into a script like the above. Sigh.
I'm sure there's a better way, but ansible and partitioning sorta don't fit, since it needs a setup, running, system.  There are other pre-install tools, but since mastering sgdisk was 20 minutes, for me, the problem is solved.



More information about the Ale mailing list