3

I'm looking for a way to change a partition type on a linux system without starting fdisk. (In this particular case from "linux" to "softwareRAID")

The ultimate goal here is a script that is going to take /dev/sda and automatically create a software RAID1 with /dev/sdb

I'm not looking for anyone here to write me a script, just trying to provide some info changing the partition type in a script. I can take care of the rest.

Thanks in advance.

jemmille
  • 304
  • 5
  • 17

4 Answers4

12

or you could use sfdisk, it's also designed to be scriptable.

e.g. to change partition 1 of /dev/sda to type 0xfd (linux raid):

sfdisk --id /dev/sda 1 fd

you can also dump out an existing partition table to a file (in a format that is intended to be re-imported back into sfdisk), then modify that file with vi/awk/sed/perl/ed/whatever and then feed it back in to sfdisk.

e.g.

sfdisk -d /dev/sda | sed -e 's/Id=83/Id=fd/' > /tmp/sda.txt
sfdisk /dev/sda </tmp/sda.txt

i often use that when building raid arrays of identical disks. manually create the partition table on one drive then use sfdisk to copy it to the other drives.

cas
  • 6,653
  • 31
  • 34
  • Note that: "sfdisk: --id is deprecated in favour of --part-type", which use is: --part-type device partition-number [type] – Krapow May 19 '21 at 15:53
5

I would look into parted, it is easily scriptable

Daniel
  • 1,703
  • 1
  • 12
  • 16
1

Use parted. You can script it, and it won't make you reboot :)

Chad Huneycutt
  • 2,096
  • 1
  • 16
  • 14
1

If you're looking to clone a partition table, dd bs=512 count=1 if=/dev/sda of=/dev/sdb; partprobe /dev/sdb is the easiest way.

womble
  • 95,029
  • 29
  • 173
  • 228