Solved FDISK, partition and format from regular files

Solved issue

banderas20

Active Member
Joined
Aug 1, 2018
Messages
115
Reaction score
48
Credits
917
Hi.

I'm trying to create regular files and set them up to be used as disks.
I'm using fdisk utility, and AFAIK, we must first create partitions and then format them.

Here are my steps:

Creation of the file


Code:
dd if=/dev/random of=file2GB bs=1MB count 2000

Create new partition and its partition table


Code:
fdisk file2GB
n (new partition with the entire size)
p (show partitions)
(the new partition is shown)
w (write changes)

Format the ¿partition?


Code:
mkfs.ext4 file2GB

With the above command, I'm formatting the entire disk. Not the partition. Which should give me an error, but it works. In addition to that, If I run mkfs.ext4 against the partition name, it fails. But if I run fdisk -l file2GB the partition shows there.

If I try to mount the disk (not the partition), it works.

I shouldn't be able to format an entire drive, and I should be able to format the partition. But it works the other way round.

¿How is that so? ¿Am I missing something?

Thanks!
 


I must be missing something here. Why are you wanting to make large files and use them like a disk? It sounds alot like what happens when you make a virtual machine. The drive for the VM is really just a file on the host computer. Not sure though why you are wanting to make files and turn them into disks. I can't see the advantage to it.

FWIK, you would have to partition the drive from within. Maybe more information will help.
 
Hi.

I'm trying to create regular files and set them up to be used as disks.
I'm using fdisk utility, and AFAIK, we must first create partitions and then format them.

Here are my steps:

Creation of the file

Code:
dd if=/dev/random of=file2GB bs=1MB count 2000

Create new partition and its partition table

Code:
fdisk file2GB
n (new partition with the entire size)
p (show partitions)
(the new partition is shown)
w (write changes)

Format the ¿partition?

Code:
mkfs.ext4 file2GB

With the above command, I'm formatting the entire disk. Not the partition. Which should give me an error, but it works. In addition to that, If I run mkfs.ext4 against the partition name, it fails. But if I run fdisk -l file2GB the partition shows there.

If I try to mount the disk (not the partition), it works.

I shouldn't be able to format an entire drive, and I should be able to format the partition. But it works the other way round.

¿How is that so? ¿Am I missing something?

Thanks!
It looks like you wish to make a block device, which is basically what disks are. If, for example you check a disk on your system such as the following:
Code:
$ ls -l /dev/nvme0n1
brw-rw---- 1 root disk 259, 0 Sep 12 06:39 /dev/nvme0n1
it is marked with a b for file type at the beginning .. b indicates it's a block device.

The dd command will create a file that can be used as a block device. There's no need for the extra functionality in the dd command of "/dev/random". The "/dev/zero" option will suffice with less processing. After creating the file, one needs to treat it as a block device in which case the following may help.

Once the block device is created, one can directly apply a filesystem to with something like:
Code:
mkfs.ext4 -m 0 file2GB
The -m option is useful if you don't want a percentage of the filesystem reserved for the super user. It avoids fragmentation. Check the man page.

Then you can adjust the number of mounts after which the filesystem can be checked with something like:
Code:
tune2fs -c 0 file2GB

If you want partitions, the fdisk program will partition the block device before you apply a filesystem, in which case, when you apply the filesystem to a partition, you have to name the partition to which the mkfs program will write.

After that, one can mount the newly created block device, file2GB as a loop device and do what you like with it.
 
Last edited:
Create new partition and its partition table

Code:
fdisk file2GB
n (new partition with the entire size)
p (show partitions)
(the new partition is shown)
w (write changes)
Is this a mistake? because you didn't create partition table.
 
I must be missing something here. Why are you wanting to make large files and use them like a disk? It sounds alot like what happens when you make a virtual machine. The drive for the VM is really just a file on the host computer. Not sure though why you are wanting to make files and turn them into disks. I can't see the advantage to it.

FWIK, you would have to partition the drive from within. Maybe more information will help.

Basically I'm using regular files as disks for testing purposes. Then I'll remove them.

It looks like you wish to make a block device, which is basically what disks are. If, for example you check a disk on your system such as the following:
Code:
$ ls -l /dev/nvme0n1
brw-rw---- 1 root disk 259, 0 Sep 12 06:39 /dev/nvme0n1
it is marked with a b for file type at the beginning .. b indicates it's a block device.
Yes. That's what I'd like to make. I know physical disks are block devices, but I don't know if I should convert the regular file into a block device. And if so, how to do it.
The dd command will create a file that can be used as a block device. There's no need for the extra functionality in the dd command of "/dev/random". The "/dev/zero" option will suffice with less processing. After creating the file, one needs to treat it as a block device in which case the following may help.
I'll bear in mind using /zero.
How can I treat it as a block device?
Once the block device is created, one can directly apply a filesystem to with something like:
Code:
mkfs.ext4 -m 0 file2GB
The -m option is useful if you don't want a percentage of the filesystem reserved for the super user. It avoids fragmentation. Check the man page.
If I run that, I'm formatting the whole disk. Not an specific partition...
Then you can adjust the number of mounts after which the filesystem can be checked with something like:
Code:
tune2fs -c 0 file2GB

If you want partitions, the fdisk program will partition the block device before you apply a filesystem, in which case, when you apply the filesystem to a partition, you have to name the partition to which the mkfs program will write.

After that, one can mount the newly created block device, file2GB as a loop device and do what you like with it.
I have been able to create a partition from the fdisk menu, but I cannot access it from mkfs with its name...
Is this a mistake? because you didn't create partition table.
If I press "n" for new partition and use the default options, when I press "p" it shows the partition. Then I write changes and exit.

Thanks!
 
If I press "n" for new partition and use the default options, when I press "p" it shows the partition. Then I write changes and exit.
I think prior to that you should create either GPT or MBR partition, with "g" or "o".
 
I think prior to that you should create either GPT or MBR partition, with "g" or "o".
Hi.

With either "g" or "o" I get this warning:

Code:
Created a new GPT/MBR disklabel (GUID: AAA12C19-D464-2C47-9415-192B219C9993).
The device contains 'dos' signature and it will be removed by a write command. See fdisk(8) man page and --wipe option for more details.

Then, If I press "p" again, the created partition doesn't show up anymore.
 


Latest posts

Top