In den meisten Situationen würden Sie einfach ein Loopback-Gerät mit „losetup“ erstellen und es mit den „-o loopback“-Optionen mounten. Wenn Sie jedoch eine Loopback-Datei erstellen, partitionieren und schließlich eine Unterpartition einhängen möchten, kann diese Option nicht verwendet werden. Sehen Sie sich an, wie Sie Partitionen innerhalb eines Loopback-Images erstellen können.
Loopback-Gerät erstellen
1. Erstellen Sie zunächst mit dem Befehl „dd“ eine Datei mit einer Größe von etwa 1 GB.
# dd if=/dev/zero of=loopbackfile.img bs=100M count=10 10+0 records in 10+0 records out 1048576000 bytes (1.0 GB) copied, 1.26748 s, 827 MB/s
2. Erstellen Sie ein Loopback-Gerät über der im obigen Schritt erstellten Datei.
# losetup -fP loopbackfile.img
3. Um das mit dem obigen Befehl generierte Loop-Gerät zu drucken, verwenden Sie „losetup -a“.
# losetup -a /dev/loop0: [64769]:4199216 (/root/loopbackfile.img)
Erstellen von Partitionen im Loopback-Image mit fdisk
1. Verwenden Sie den Befehl fdisk, um Partitionen auf dem Loopback-Gerät /dev/loop0 zu erstellen. Eine primäre Partition mit einer Größe von 500 MB wird wie unten gezeigt erstellt.
# fdisk /dev/loop0 Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0x4d455ea1. Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): 1 First sector (2048-2047999, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-2047999, default 2047999): +500M Partition 1 of type Linux and of size 500 MiB is set
2. Speichern Sie die Partitionstabelle und verlassen Sie das Dienstprogramm fdisk.
Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
Dateisystem erstellen und mounten
1. Erstellen Sie das ext4-Dateisystem auf der /dev/loop0p1-Partition, die im obigen Schritt erstellt wurde.
# mkfs.ext4 /dev/loop0p1 mke2fs 1.42.9 (28-Dec-2013) Discarding device blocks: done Filesystem label= OS type: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) Stride=0 blocks, Stripe width=0 blocks 128016 inodes, 512000 blocks 25600 blocks (5.00%) reserved for the super user First data block=1 Maximum filesystem blocks=34078720 63 block groups 8192 blocks per group, 8192 fragments per group 2032 inodes per group Superblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409 Allocating group tables: done Writing inode tables: done Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done
2. Hängen Sie das Dateisystem in das gewünschte Verzeichnis ein.
# mkdir /loopfs # mount -o loop /dev/loop0p1 /loopfs
3. Überprüfen Sie die Größe des Einhängepunkts und den Dateisystemtyp.
# df -hP /loopfs Filesystem Size Used Avail Use% Mounted on /dev/loop1 477M 2.3M 445M 1% /loopfs
# mount | grep loopfs /dev/loop0p1 on /loopfs type ext4 (rw,relatime,seclabel,data=ordered)So erstellen Sie ein virtuelles Blockgerät (Loop-Gerät/Dateisystem) unter Linux