When we are creating the primary partitions in the loop device and we used parted command/options to saymkpart primary ext4 rangestart rangeend (step 3). Why do we then need to go and put another filesystem on the partition in step 6?
mkpart primary ext4 rangestart rangeend
That's a good question! Not sure what distro you are, but I did a test case on CentOS 8. As the way that parted works, it needs to be instructed to make filesystems on the partitions after they are created in step 3. If you do 'fdisk -l' just after step 3, you will see they marked as 'linux' partitions, but you won't be able to mount them, because the OS will say there isn't a filesystem in each of these partitions. So you will need to make them as in step 6.
The man parted(8) page says the following:
mkpart part-type [fs-type] start end
Make a part-type partition for filesystem fs-type (if specified), beginning at
start and ending at end (by default in megabytes). part-type should be one of
"primary", "logical", or "extended".
So, in the command in step 3, we are specifying the fs-type (we could not and it should work anyway). But for some reason we need to format they anyway after that, or they won't mount.
Hmmmm Okay. Seems like the overkill and that whomever maintains or would work on how that works would maybe cut out the redundancy if it needs to have mkfs later.
Good point, I'll check if we can avoid specifying the fs-type parameter in the first command.
It is not a redundancy. If you don't specify the fs type in the early step, I'm not sure what will show up as the type in fdisk -l. In the case of ext4 it might show up as "Linux" anyway, but what if you wanted vfat, or some other non-linux type. At any rate it is harmless and causes no extra pain.
Linux actually usually ignores the fs type from fdisk -l anyway. For instance you can use a partition for lvm even if you never mark it as such.
I just did a test case on CentOS 8, and I didn't specify the fs-type in step 3, as the man parted(8) says this parameter is optional. Then I gave filesystem to the partitions as shown in step 6, and the partitions showed up correctly in the fdisk -l output. So, it looks it's right to say that specifying the fs-type in step 3 is unnecessary.