3

I'm trying to create software to restore backups that I already create with scripts. I haven't needed to restore any, fortunately, but now I want to move the contents of one of my backups to a SSD drive, and it turns out it's harder than I expected.

Right now I'm trying to recreate the partition table. I'd like to use parted because it seems the easiest of the available tools to use in a script. I see two things that are puzzling me.

First, a command like

parted /dev/sdh mklabel gpt mkpart primary fat32 2048 4096 print

makes a gpt partition table and a partition, but it shows a name "primary" and that is what blkid calls a "PARTLABEL". Reading the info and man pages seems to indicate it should be the filesystem type, not name. On experimentation, it appears no fs type is accepted any more, and that is the name instead.

The second problem is: I do not want a name on the partition at all, but I've tried "" and " " with no luck.

So: how do I create a partition without a PARTLABEL? I can certainly do it in gparted, but I cannot script that.

Eliah Kagan
  • 116,445
  • 54
  • 318
  • 493
ForDummies
  • 321
  • 2
  • 11

2 Answers2

3

I found the way, and boy is it odd. If I want parted to accept a blank string (at least for this purpose) I have to quote it three times. That's right, three.

It looks like this:

parted -s $destination mkpart "'\ '" ntfs  1874585600s  1876559871s

So it's a backslash-space inside single quotes inside double quotes. That works, and I've been unable to find anything simpler. For instance if I just leave it out, parted still accepts the command, but uses 'ntfs' both as a filesystem type and a partition name.

Grrrrr.

Eliah Kagan
  • 116,445
  • 54
  • 318
  • 493
ForDummies
  • 321
  • 2
  • 11
  • Where did you find this solution? I find it doesn't work. First it does not work at all with the "mkpart" command (on parted 3.3 it says `parted: invalid token:`), and if you instead use the `name` command, it sets the name to a space, which you can't see with parted without -m; see it with `parted -sm $dev print`. The only thing I found so far that works is to use `sgdisk $dev -c 1:""` – Peter Jan 12 '21 at 13:26
  • It works for me, even on parted 3.3, but you are correct that it sets the name to a single space. That is good enough for what I wanted, but thanks for the method that really sets it empty. I tested with `parted -sm /dev/sdj mklabel gpt mkpart "'\ '" ext4 1MiB 8MiB print` and got what I needed. – ForDummies Jan 18 '22 at 19:07
0

I tried various methods to get an actual blank name (also this) but no method worked for a non-interactive script. However using a pipe to automatically "type" in the commands worked:

printf "\nfat32\n1048576B\n537919487B\n" | parted /dev/sda mkpart
Output:
Partition name?  []?
File system type?  [ext2]? fat32
Start? 1048576B
End? 537919487B
Information: You may need to update /etc/fstab.

For some reason the File system was blank:

parted /dev/sda u b p
Number  Start     End         Size        File system  Name  Flags
 1      1048576B  537919487B  536870912B                     msftdata

Had to format it with this as final step:

mkfs -t fat -F 32 /dev/sda1