Z80-Retro / Z80-Retro-Manual

2 stars 3 forks source link

Some clarifications about losetup and the loop device #17

Open Ericounet opened 1 day ago

Ericounet commented 1 day ago

Create the SD Card Image

!!! WARNING !!! This process can damage your host file system if you get it wrong. Make sure you only format the loop device after setting it up.

This step is a bit more complicated and these steps are definitely ONLY going to work on a Linux host.

We'll use the «losetup» command to create and link a pseudo loop device with the SDcard.img file.

  1. First we create the image file named «SDcard.img», filled with 0.
dd if=/dev/zero of=SDcard.img bs=1024 count=512000
  1. Second, we link our image «SDcard.img» with a loop device. Doing so, you'll see a new loop device in the list; type "lsblk» at the prompt, and a loop device with a size of 500m will appear.

Look at the screenshot of my system. The new loop is loop5. It will be that loop you'll need to use in the further commands.

eric@aldebaran:~/Projets-Z80/z80-retro/2063-Z80-cpm/boot$ lsblk
NAME        MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINTS
loop0         7:0    0     4K  1 loop  /snap/bare/5
loop1         7:1    0  55,7M  1 loop  /snap/core18/2829
loop2         7:2    0 164,8M  1 loop  /snap/gnome-3-28-1804/198
loop3         7:3    0  91,7M  1 loop  /snap/gtk-common-themes/1535
loop4         7:4    0  38,8M  1 loop  /snap/snapd/21759
loop5         7:5    0   500M  0 loop  
sda           8:0    0   3,6T  0 disk  
└─sda1        8:1    0   3,6T  0 part  
  └─md0       9:0    0   3,6T  0 raid1 /mnt/EricRaid
sdb           8:16   0   3,6T  0 disk  
└─md0         9:0    0   3,6T  0 raid1 /mnt/EricRaid
sdc           8:32   1   1,8G  0 disk  
└─sdc1        8:33   1   128M  0 part  
nvme0n1     259:0    0 465,8G  0 disk  
├─nvme0n1p1 259:2    0   953M  0 part  /boot/efi
├─nvme0n1p2 259:3    0  14,9G  0 part  [SWAP]
└─nvme0n1p3 259:4    0 449,9G  0 part  
  └─md1       9:1    0 449,8G  0 raid1 /
nvme1n1     259:1    0 465,8G  0 disk  
├─nvme1n1p1 259:5    0   953M  0 part  
├─nvme1n1p2 259:6    0  14,9G  0 part  [SWAP]
└─nvme1n1p3 259:7    0 449,9G  0 part  
  └─md1       9:1    0 449,8G  0 raid1 /
$ (
echo n # Add a new partition
echo p # Primary partition
echo 1 # Partition number
echo
# First sector (Accept default)
echo +128M # Last sector (Accept default: varies)
echo n # Add a new partition
echo p # Primary partition
echo 2 # Partition number
echo
# First sector (Accept default)
echo
# Last sector (Accept default: varies)
echo t # Partition Type
echo 1 # First partition
echo db # Type = CP/M
echo t # Partition Type
echo 2 # Second partition
echo 06 # Fat 16
echo w # Write changes
) | sudo fdisk /dev/loop5 # VERY IMPORTANT : use the new loop you found above. For me it is loop5

Again, take care of the name of the loop in the 3 next commands.

$ sudo mkfs.msdos /dev/loop5p1
$ sudo mkfs.msdos /dev/loop5p2
$ sudo dd if=retro.img of=/dev/loop5p1 bs=512
  1. Finally unmount the loopback disk.
1 $ sudo losetup -d /dev/loop5

You should now have a 500MB SDcard.img file.

linuxplayground commented 1 day ago

I think the example I gave in the current version of the manual for finding the loop device is clearer because it returns the actual filename in the output.

sudo losetup --all | grep SDcard
/dev/loop12: [2050]:7215475 (/home/davelatham/dev/retro/2063-Z80-cpm/filesystem/SDcard.img)

I also try to not show too much extra information about my system which can be confusing.

Perhaps I'll link to a document on losetup... Rather than try to explain it myself.

https://man7.org/linux/man-pages/man8/losetup.8.html

Ericounet commented 16 hours ago

Ok for me :)

linuxplayground commented 14 hours ago

Okay. I'll just add the link to the losetup man page on the next update.