parted /dev/loop3 targets the loop

The command parted /dev/loop3 starts the parted utility and targets the loop device /dev/loop3. This means you’re about to partition the file associated with this loop device, not a physical hard drive.

1. Loop Devices

Loop devices are virtual block devices. They allow you to mount a regular file as if it were a block device (like a hard drive partition). Before using parted /dev/loop3, you must have already set up /dev/loop3 using losetup.

Example setup:

1
2
3
4
5
# Create a file to use as a disk image (e.g., 1GB)
truncate -s 1G mydisk.img

# Associate the file with /dev/loop3
sudo losetup /dev/loop3 mydisk.img

2. parted /dev/loop3

Once the loop device is set up, running sudo parted /dev/loop3 (you’ll likely need sudo) will launch the parted interactive command-line tool. You’ll see the (parted) prompt.

3. Common parted Commands

Inside the parted prompt, you can use the following commands:

  • mklabel <label-type>: Creates a new disk label (partition table). Common types are msdos (for MBR partitions) and gpt (for GPT partitions). gpt is recommended for modern systems. Example: mklabel gpt
  • mkpart <part-type> <fs-type> <start> <end>: Creates a new partition.
    • <part-type>: primary, extended, or logical (only for msdos labels). For gpt, it’s usually primary.
    • <fs-type>: This is informational; parted doesn’t format the partition. Common values are ext4, fat32, linux-swap.
    • <start> and <end>: The start and end positions of the partition. You can use percentages (e.g., 0%, 100%), megabytes (e.g., 0MB, 500MB), or gigabytes (e.g., 0GB, 1GB).
  • print: Displays the current partition table.
  • rm <partition-number>: Removes a partition.
  • resizepart <partition-number> <end>: Resizes a partition.
  • quit: Exits parted.

Example parted session:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
sudo parted /dev/loop3
(parted) mklabel gpt
(parted) mkpart primary ext4 0% 100%
(parted) print
Model: Loop device (loop)
Disk /dev/loop3: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 1049kB 1074MB 1073MB primary

(parted) quit

4. After Partitioning (Important!)

parted only creates the partition table and partitions. You must then format the partition with a filesystem:

1
sudo mkfs.ext4 /dev/loop3p1  # Format the first partition (adjust if different)

5. Mounting the Partition

You can then mount the partition:

1
2
sudo mkdir /mnt/loop3
sudo mount /dev/loop3p1 /mnt/loop3

6. Cleaning Up

When you’re finished, unmount the partition and detach the loop device:

1
2
sudo umount /mnt/loop3
sudo losetup -d /dev/loop3

Key Points

  • losetup is essential: You must use losetup to create the loop device before using parted.
  • Formatting is required: parted does not format the partitions. Use mkfs.* (e.g., mkfs.ext4, mkfs.vfat) after partitioning.
  • Permissions: You’ll likely need sudo for all these commands.
  • Persistence: The changes you make are written to the file you used with losetup (mydisk.img in the example).