2

I created my virtual disk (1024MB):

~$ dd if=/dev/zero of=MyDrive.img iflag=fullblock bs=1M count=1024 && sync

Case 1:

Now I'm going to configure it with inside parted to Partitioning / formatting MyDrive.img:

~$ parted MyDrive.img
(parted) mklabel msdos
(parted) mkpart primary NTFS 1 1024
(parted) align-check optimal 1

Result:

(parted) print
 Model:  (file)
 Disk /home/user/test/MyDrive.img: 1074MB
 Sector size (logical / physical): 512B / 512B
 Table of partitions: msdos
 Disk Flags: 

 Start Number  End     Size    Type    File system  Flags
  1   1049kB  1024MB  1023MB  primary     ntfs       lba

Verifying partition:

~$ sudo losetup loop1 MyDrive.img
~$ sudo -H gparted /dev/loop1

So far, so good. Now the problem...

Case 2:

If I want to launch the "parted" from command line (in terminal; outside parted), with the same commands, i do not get the same result:

  ~$ parted -s MyDrive.img mklabel msdos
  ~$ parted -s MyDrive.img mkpart primary NTFS 1 1024
  ~$ parted -s MyDrive.img align-check optimal 1
  ~$ parted -s MyDrive.img set 1 lba on

Out:

  ~$ parted -s MyDrive.img print
     Model:  (file)
     Disk /home/user/test/MyDrive.img: 1074MB
     Sector size (logical / physical): 512B / 512B
     Table of partitions: msdos
     Disk Flags: 

     Start Number  End     Size    Type    File system  Flags
       1   1049kB  1024MB  1023MB  primary         

Problem: Missing NTFS and LBA (The same thing happens with ext3, ext4, etc)

Cause:

   ~$ parted -s MyDrive.img mklabel msdos # Works
   ~$ parted -s MyDrive.img mkpart primary NTFS 1 1024 # Not Work
   ~$ parted -s MyDrive.img align-check optimal 1  # Works
   ~$ parted -s MyDrive.img set 1 lba on  # Not Work

Question:

How to correctly execute "parted" with option "-s" (--script) directly from command line in terminal (case 2) to get the same output (Case 1)?

Thanks

1 Answer 1

1

parted uses odd units by default, so it's better to specify it.

(parted) unit MiB

or

(parted) mkpart ... 1MiB 1024MiB

There is no need to do an alignment check if you know your partition starts at 1MiB.

The set 1 lba on command actually changes the partition type to Linux. That might come as a bit of a surprise, but it's normal for parted's set to change partition types (other options are raid, lvm, ...).

Other than that detail, the result of your operations are completely identical. The difference in output is merely an optical problem.

It might be a remnant of old parted that used to create the filesystems themselves, instead of partitions only.(*) If you want the ntfs to stick, you actually have to mkntfs one.


Your interactive method:

# dd if=/dev/zero of=MyDrive.img iflag=fullblock bs=1M count=1024 && sync
# parted MyDrive.img 
GNU Parted 3.2
Using /dev/shm/MyDrive.img
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel msdos                                                    

I create a copy of it at this point so we can check and compare the other method:

(parted) ^Z                                                               
[1]+  Stopped                 parted MyDrive.img
# cp MyDrive.img MyDrive-Copy.img
# fg

Onwards:

(parted) mkpart primary NTFS 1 1024
(parted) align-check optimal 1                                            
1 aligned
(parted) print                                                            
Model:  (file)
Disk /dev/shm/MyDrive.img: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  1024MB  1023MB  primary  ntfs         lba

Shows ntfs and lba but print it again and it's gone:

# parted MyDrive.img
GNU Parted 3.2
Using /dev/shm/MyDrive.img
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print                                                            
Model:  (file)
Disk /dev/shm/MyDrive.img: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  1024MB  1023MB  primary

Your 2nd method using terminal commands, performed on the copy:

# parted MyDrive-Copy.img mkpart primary NTFS 1 1024
# parted MyDrive-Copy.img align-check optimal 1
1 aligned
# parted MyDrive-Copy.img print
Model:  (file)
Disk /dev/shm/MyDrive-Copy.img: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  1024MB  1023MB  primary

Are there actually any differences?

# cmp -l MyDrive.img MyDrive-Copy.img && echo Identical || echo Different
Identical

(Without the copy method, they would have different disk identifiers as it's random every time you mkpart. With GPT partitions, each individual partition would also have a unique PARTUUID so the method does not apply to other partitioning schemes, or rather you have to check what the differing bytes represent.)


So, you do get the same result after all, unless the commands are different (there was no set in your first example).


(*)

To make the confusion perfect, after mkntfs on the partition, parted detects NTFS to be present and set 1 lba on does not set Linux as partition type anymore. This also means with random data on the disk you might get random results in your partition table.

So there may be a point to specifying NTFS when you mkpart after all. parted remembers this type for the current session and acts accordingly when picking partition types. Translated to the terminal, it might be best to do it all in one command.

# parted MyDrive.img \
    mklabel msdos \
    mkpart primary NTFS 1 1024 \
    set 1 lba on \
    align-check optimal 1 \
    print

Model:  (file)
Disk /dev/shm/MyDrive.img: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  1024MB  1023MB  primary  ntfs         lba
5
  • Very interesting the answer and contributes a lot to the understanding of the functioning of "parted", however, the question has not yet been answered. If you read carefully, the question is about how to correctly execute the "parted -s" command
    – acgbox
    Mar 22, 2018 at 23:45
  • I consider it answered. Added a print to the last example. Mar 23, 2018 at 0:11
  • Already execute your suggestion. I even mounted the partition (sudo losetup loop1 MyDisk.img) and verified it (sudo -H gparted /dev/loop1) and there is no NTFS partition (or ext4, etc)
    – acgbox
    Mar 23, 2018 at 0:15
  • We're not on the same page here. parted does not create filesystems (it used to have filesystem handling but not anymore), you have to follow it up with mkfs. It is a different story with gparted. With losetup you might need --partscan to see /dev/loopXpY partition devices and then actually run mkntfs /dev/loopXpY. I understood your question to be about the difference in parted print output so my answer is mostly referring to that. Mar 23, 2018 at 0:24
  • Look at this unix.stackexchange.com/questions/433449/…
    – acgbox
    Mar 25, 2018 at 17:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .