Permanently mounting loop device

6,819

You're missing steps about creating a filesystem.

dd if=/dev/urandom of=pseudoblock bs=1M count=100
losetup /dev/loop0 pseudoblock

Now you can create a filesystem on it.

mkfs.ext4 /dev/loop0

Once this is done, I can get an UUID (this identifies the filesystem, so if you haven't created one, you won't get an UUID), and then find it.

blkid
/dev/vda1: UUID="a76b548b-f4e4-4616-bea0-469775cb852d" TYPE="ext4" PARTUUID="8395c684-01"
/dev/loop0: UUID="bb784e83-8484-4822-8930-20e02f689450" TYPE="ext4"

Then I mount it, and I can write things in it.

mount /dev/loop0 /mnt/loop

And once that pseudo block device is initialized with a filesystem you can mount it directly with the fstab using a line like (no need to use losetup anymore, and there's no really need for an UUID, the file is not like /dev devices that could get probed in different order, it won't move, so it's no problem using its absolute path).

/root/pseudoblock  /mnt/loop    auto loop 0 0
Share:
6,819

Related videos on Youtube

pkaramol
Author by

pkaramol

Updated on September 18, 2022

Comments

  • pkaramol
    pkaramol over 1 year

    I created a file

    touch /datastore/virtual/pseudoblock
    

    created a loop device with it

    sudo losetup /dev/loop0 /datastore/virtual/pseudoblock
    

    (although I got a warning about its size)

    and then initialized it with random data

    dd if=/dev/urandom of=/dev/loop0 bs=1M count=100
    

    My questions are

    a) why is it not displayed yet in my block devices' list?

    NAME                     MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
    sdb                        8:16   0 489.1G  0 disk  
    ├─sdb2                     8:18   0   488M  0 part  /boot
    ├─sdb3                     8:19   0 488.1G  0 part  
    │ └─sdb3_crypt           253:0    0 488.1G  0 crypt 
    │   ├─ubuntu--vg-root    253:1    0 472.2G  0 lvm   /
    │   └─ubuntu--vg-swap_1  253:2    0  15.9G  0 lvm   [SWAP]
    └─sdb1                     8:17   0   512M  0 part  /boot/efi
    sda                        8:0    0 931.5G  0 disk  
    └─sda1                     8:1    0 931.5G  0 part  
      └─sda1_crypt           253:3    0 931.5G  0 crypt 
        └─store--vg-store1tb 253:4    0 931.5G  0 lvm   /datastore
    

    b) how can I permanently mount it via /etc/fstab since I cannot get its UUID?

    $ blkid
    /dev/mapper/sdb3_crypt: UUID="fSHjbj-51K2-QfhI-G0Wb-o3lN-4pE1-BB4wCP" TYPE="LVM2_member"
    /dev/mapper/ubuntu--vg-root: UUID="9e452886-4d8g-fj54-854c-80bc171826e8" TYPE="ext4"
    

    working on

    $ lsb_release -a
    No LSB modules are available.
    Distributor ID: Ubuntu
    Description:    Ubuntu 16.04.5 LTS
    Release:    16.04
    Codename:   xenial
    

    with

    $ uname -a
    Linux pkara-pc01 4.15.0-42-generic #45~16.04.1-Ubuntu SMP Mon Nov 19 13:02:27 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux