0

I am writing a script (for learning purpose) which will help me to automate Arch Linux installation. In this script, I am stuck at partitioning. Although, I know that how to partition using fdisk, cfdisk, gparted, but all these tools require manual intervention. However, I want to create partition automatically. In this respect, I have to embed following steps in my script:

  1. Create GPT partition table on /dev/sda
  2. Create primary boot partition - efi (FAT32) - 500MB - /dev/sda1
  3. Create primary root partition - ext4 - 17GB - /dev/sda2
  4. Create primary swap partition - swap - 2GB - /dev/sda3

I know that filesystem can later be implemented via mkfs command. However, in case of EFI and SWAP partition, it is necessary to tell system (at the time of partitioning) that this is not regular "Linux" partition. Can you guide me that how can I achieve these steps automatically in my script?

2
0

You can use this structure. This is a script I use to create a 25MB partition in FAT format.

First run the command fdisk interatively, and annotate every response. Put that response in the rigth order using echo. An empty echo means the default option.

(
  echo o;
  echo n;
  echo ;
  echo ;
  echo ;
  echo +25M;
  echo a;
  echo n;
  echo ;
  echo ;
  echo ;
  echo ;
  echo w;
) | fdisk /dev/sdb

mkfs.vfat -n 'BOOT' /dev/sdb1
6
  • 1
    @Pedro I'm aware of this. "but all these tools require manual intervention" and "Can you guide me that how can I achieve these steps automatically". I'm hinting OP in a way that he can automate the procedure, not providing a full script (which should be tested, which is not something any answerer will do). – schrodigerscatcuriosity Apr 29 '20 at 15:47
  • 1
    this script worked fine and i replaced 'o' with 'g' for gpt. – Salman Ahmed Apr 29 '20 at 15:48
  • 1
    nothing wrong with that. @schrodigerscatcuriosity apologies if I have been unfair to you. I've gone through this process, looked at alternatives and worked out a way to automate partitioning. OP: In my own personal view, it's a bad solution to automate a task by feeding keystrokes to a tool when there's an actual supported way to do this with a tool as basic and commonly deployed as fdisk. It's a bit like emulating a human at a task that a machine would be 1000x better at. Again - my view, my experience, my concept of a bad solution. – Pedro Apr 29 '20 at 16:03
  • 1
    @Pedro no worries! That's the goal of the site IMO, providing different options, and if one is bad or not so good, you can always downvote or leave a comment like you do here, which is useful for the community. Cheers! – schrodigerscatcuriosity Apr 29 '20 at 16:08
  • 1
    If sfdisk is the proper way to do this, in future , I will definitely look in to it. Thanks – Salman Ahmed Apr 29 '20 at 16:09
0

You certainly can, I have found sfdisk best for this purpose, however please please be very careful when using these tools, it's easy to partition the wrong disk and end up in a situation where you can't find your data (although it would be mostly intact).

Here's an example, for a GPT partition table. This is just the partition table text file (<file>) that you need to pipe to sfdisk to apply to a device like so: cat <file> | sfdisk <device>

label: gpt
device: /dev/sdY
unit: sectors

1: size=2048,type=21686148-6449-6E6F-744E-656564454649,name=part-bios
2: size=512MiB,type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B,name=part-efi
3: size=512MiB,type=0FC63DAF-8483-4772-8E79-3D69D8477DE4,name=part-boot
4: size=852GiB,type=E6D6D379-F507-44C2-A23C-238F2A3DF928,name=part-lvm
5: type=0FC63DAF-8483-4772-8E79-3D69D8477DE4,name=part-extra

#
# full list of partition UUIDs: sfdisk -X gpt -T
#

Here's another example, this time in a script (simpler case, DOS type label, but you get the idea):

#!/bin/bash

DEV=/dev/sdXXXXXX # pick your disk device

wipefs -a ${DEV} # recommended if you want to swap partition table types

# partitioning for 20G disk
BOOT_SIZE=512
ROOT_SIZE_SEC=16384

echo "label: dos
device: ${DEV}
unit: sectors

${DEV}1 : size=${BOOT_SIZE_SEC}MiB, type=83
${DEV}2 : size=${ROOT_SIZE_SEC}MiB, type=83
${DEV}3 : type=82
" | sfdisk ${DEV}
1
  • 1
    I am not familiar with sfdisk. I am comfortable with fdisk and I think it will be easy for me to use fdisk. – Salman Ahmed Apr 29 '20 at 15:50
0

@schrodigerscatcuriosit

I have edited your script according to my needs. Here it is:

(
  echo g;
  echo n;
  echo ;
  echo ;
  echo +500M;
  echo n;
  echo ;
  echo ;
  echo +17G;
  echo n;
  echo ;
  echo ;
  echo ;
  echo t;
  echo 1;
  echo 1;
  echo t;
  echo ;
  echo 19;
  echo w;
) | fdisk /dev/sda

I have made few changes.

  1. There are two default options after first 'n', not three.
  2. I have added "echo t" in the end and select partition 1 and then option 1, which is EFI System.
  3. I have added "echo t" again and changed type of partition 3 to swap.

I think changing the type of /dev/sda1 to EFI and /dev/sda3 to SWAP is necessary. What do you think?

2
  • 1
    This needs, as always, a little testing. I would suggest that you try the script in a virtual machine, so you don't need to perform it in you HD in case something doesn't work as expected, or you wan to change some parameter after. – schrodigerscatcuriosity Apr 29 '20 at 15:58
  • working fine in VM, thanks. – Salman Ahmed Apr 29 '20 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.