Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
1
votes
1
answers
1090
views
How to modify (add files or directories) to the qemu-img created raw disk file?
How to add files or directories to the ```qemu-img``` created raw disk file. For example. I use ``` qemu-img create -f raw disk.raw 1G ``` to generate a ```disk.raw``` file. I want to copy some ```/bin```, ```/usr``` directories to the ```disk.raw``` file. I have tried the following commands. With t...
How to add files or directories to the
-img
created raw disk file. For example. I use qemu-img create -f raw disk.raw 1G
to generate a .raw
file. I want to copy some /bin
, /usr
directories to the .raw
file.
I have tried the following commands. With the reference to here
qemu-img create -f raw disk.raw 1G
mkdir /image
losetup /dev/loop0 disk.raw
fdisk /dev/loop0
> n
> p
> ENTER
> ENTER
> ENTER
> a
> w
kpartx -a /dev/loop0
mount /dev/mapper/loop0p1 /image
However, the last commands output the following error messages: : /image: wrong fs type, bad option, bad superblock on /dev/mapper/loop0p1, missing codepage or helper program, or other error.
Zhaoyang
(43 rep)
Nov 21, 2023, 09:31 AM
• Last activity: Nov 21, 2023, 10:42 AM
2
votes
1
answers
1229
views
LVM and device mapper: Logical Volume device sector size
This seems to be a problem regarding the sector size of the mapped device for a logical volume between different machines. More specifically, I'd like to know if and how the sector size of a mapped device corresponding to a logical volume can be configured. Here is a description of the problem, comp...
This seems to be a problem regarding the sector size of the mapped device for a logical volume between different machines.
More specifically, I'd like to know if and how the sector size of a mapped device corresponding to a logical volume can be configured.
Here is a description of the problem, comparing two machines.
## Machine 1
I have an entire disk image in a logical volume
mytestlv
in a volume group named MyVolumeGroup
.
This disk image has its own partition table (completely independent of the actual disk it's stored on).
For example, fdisk /dev/mapper/MyVolumeGroup-mytestlv
shows this:
Disk /dev/mapper/MyVolumeGroup-mytestlv: 30 GiB, 32212254720 bytes, 62914560 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: ...
Device Start End Sectors Size Type
/dev/mapper/MyVolumeGroup-mytestlv-part1 2048 4095 2048 1M BIOS boot
/dev/mapper/MyVolumeGroup-mytestlv-part2 4096 4198399 4194304 2G Linux swap
/dev/mapper/MyVolumeGroup-mytestlv-part3 4198400 62914526 58716127 28G Linux filesystem
If I need access to the data on a partition of that disk image, I can use kpartx
and mount the partition.
kpartx -a /dev/mapper/MyVolumeGroup-mytestlv
creates these devices files, which can be used to mount a partition within that disk image, for example:
/dev/mapper/MyVolumeGroup-mytestlvl
/dev/mapper/MyVolumeGroup-mytestlv2
/dev/mapper/MyVolumeGroup-mytestlv3
## Machine 2
This was now copied onto a different machine (content is exactly the same, checksums of both entire /dev/mapper/MyVolumeGroup-mytestlv
are identical).
Using the configured block size, fdisk /dev/mapper/MyVolumeGroup-mytestlv
shows this:
/dev/mapper/MyVolumeGroup-mytestlv: 30 GiB, 32212254720 bytes, 7864320 sectors
Units: sectors of 1 * 4096 = 4096 bytes
Sector size (logical/physical): 4096 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x00000000
Device Boot Start End Sectors Size Id Type
/dev/mapper/MyVolumeGroup-mytestlv-part1 1 7864319 7864319 30G ee GPT
## Differences
The logical sector size is 512 on Machine 1, but 4096 on Machine 2.
On Machine 1, blockdev --getss /dev/mapper/MyVolumeGroup-mytestlv
returns 512.
On Machine 2, blockdev --getss /dev/mapper/MyVolumeGroup-mytestlv
returns 4096.
## Workaround
On Machine 2, forcing the sector size to 512 with fdisk
helps it see the partition table correctly.
fdisk --sector-size 512 /dev/mapper/MyVolumeGroup-mytestlv
Disk /dev/mapper/MyVolumeGroup-mytestlv: 30 GiB, 32212254720 bytes, 62914560 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: ...
Device Start End Sectors Size Type
/dev/mapper/MyVolumeGroup-mytestlv-part1 2048 4095 2048 1M BIOS boot
/dev/mapper/MyVolumeGroup-mytestlv-part2 4096 4198399 4194304 2G Linux swap
/dev/mapper/MyVolumeGroup-mytestlv-part3 4198400 62914526 58716127 28G Linux filesystem
Unfortunately, other tools such as sfdisk
or kpartx
don't seem to have those options.
If I create a loopback device explicitly from that LV, then that loopback device has a sector size of 512, and everything works (in fact, versions of losetup
that are not too old have an explicit sector size option).
After losetup --show -f /dev/mapper/MyVolumeGroup-mytestlv
:
* blockdev --getss /dev/loop0
returns 512
* kpartx -a /dev/loop0
creates /dev/mapper/loop0p{1,2,3}
## Where is that sector size configured?
I'd like to be able to use fdisk
, sfdisk
, kpartx
without relying on an additional losetup
.
On Machine 2, cat /sys/block/dm-2/queue/hw_sector_size
returns 4096 (/dev/mapper/MyVolumeGroup-mytestlv
is actually a soft-link to /dev/dm-2
). I've tried to change this using echo 512 > /sys/block/dm-2/queue/hw_sector_size
, but this doesn't seem to be possible.
* Is there a way to tell LVM that I'd like the sector size for the device corresponding to /dev/mapper/MyVolumeGroup-mytestlv
to be 512?
* Is that something that affects all the logical volumes in the group or on the system?
I can't see any options with lvcreate
or lvdisplay
related to this.
Bruno
(1223 rep)
Jun 7, 2023, 12:13 PM
• Last activity: Jun 7, 2023, 04:15 PM
0
votes
1
answers
1052
views
How to mount a sparsed disk image
I have an image of the mSATA SSD disk of a PC. The disk contains the operating system and has a capacity of 512GB. I do not have that free storage, so I have cloned the disk into an image with `dd` compressing it with `gz`, and later according to the answer of [this post][1], I have copied the spars...
I have an image of the mSATA SSD disk of a PC. The disk contains the operating system and has a capacity of 512GB.
I do not have that free storage, so I have cloned the disk into an image with
dd
compressing it with gz
, and later according to the answer of this post , I have copied the sparsed content so that it occupied little.
This has worked correctly, resulting in a 512GB image occupying less than 5GB on disk.
As a summary of what has been done:
# dd bs=64K if=/dev/sdd conv=sync,noerror status=progress | gzip -c > /image.img.gz
# gunzip -c /image.img.gz | cp --sparse=always /dev/stdin mini.img
# ls -lhs
4,8G -rw------- 1 balon users 477G ene 13 10:54 mini.img
2,3G -rw-r--r-- 1 balon users 2,3G ene 11 08:32 minimal-industrial-pc.img.gz
So far, everything is correct. The problem comes when I intend to mount the image (since I want to cage myself in it and make some changes about the file system).
I have tried the following:
1. fdisk
# fdisk -l mini.img
The size mismatch of the primary master boot record (GPT PMBR) (1000215215!= 1000215295) will be corrected by writing.
The backup GPT table is not at the end of the device.
Disk mini.img: 476,94 GiB, 512110231552 bytes, 1000215296 sectors
Units: sectores de 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disc label type: gpt
Disk identifier: 74BC899D-E8BA-4C70-B82D-6F4E8F6343A3
Device Start End Sectors Size Type
mini.img1 2048 2203647 2201600 1G EFI System
mini.img2 2203648 6397951 4194304 2G Linux file system
mini.img3 6397952 1000212479 993814528 473.9G Linux file system
2. kpartx
# kpartx -a -v mini.img
GPT:Primary header thinks Alt. header is not at the end of the disk.
GPT:Alternate GPT header not at the end of the disk.
GPT: Use GNU Parted to correct GPT errors.
add map loop1p1 (254:4): 0 2201600 linear 7:1 2048
add map loop1p2 (254:5): 0 4194304 linear 7:1 2203648
add map loop1p3 (254:6): 0 993814528 linear 7:1 6397952
In this case there seems to be no problems mounting loop1p1
and loop1p2
, but with `loop1p3, which I understand corresponds to the Ubuntu root system, there is no way.
3. gdisk
# gdisk -l mini.img
GPT fdisk (gdisk) version 1.0.9.1
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Disk mini.img: 1000215296 sectors, 476.9 GiB
Sector size (logical): 512 bytes
Disk identifier (GUID): 74BC899D-E8BA-4C70-B82D-6F4E8F6343A3
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 1000215182
Partitions will be aligned on 2048-sector boundaries
Total free space is 4717 sectors (2.3 MiB)
Number Start (sector) End (sector) Size Code Name
1 2048 2203647 1.0 GiB EF00
2 2203648 6397951 2.0 GiB 8300
3 6397952 1000212479 473.9 GiB 8300
What am I doing wrong?
Francisco de Javier
(1311 rep)
Jan 13, 2023, 10:11 AM
• Last activity: Jan 13, 2023, 05:59 PM
6
votes
1
answers
1994
views
kpartx: cannot unmount disk after mappings have been deleted
I cannot unmount a partition on which I created and then deleted mappings with `kpartx`: # mount /dev/sdb1 usb/ # cd usb/ # kpartx -a -p sda -r -s -v sda.img add map loop0sda1 (254:5): 0 16777216 linear 7:0 2048 add map loop0sda2 (254:6): 0 16777216 linear 7:0 16779264 add map loop0sda3 (254:7): 0 4...
I cannot unmount a partition on which I created and then deleted mappings with
kpartx
:
# mount /dev/sdb1 usb/
# cd usb/
# kpartx -a -p sda -r -s -v sda.img
add map loop0sda1 (254:5): 0 16777216 linear 7:0 2048
add map loop0sda2 (254:6): 0 16777216 linear 7:0 16779264
add map loop0sda3 (254:7): 0 440401920 linear 7:0 33556480
add map loop0sda4 (254:8): 0 26159792 linear 7:0 473958400
# kpartx -dv /dev/loop0
del devmap : loop0sda4
del devmap : loop0sda3
del devmap : loop0sda2
del devmap : loop0sda1
# cd ..
#umount usb/
target is busy.
as far as i can tell, the command kpartx -dv /dev/loop0
properly deleted the mappings, as I can see they have disappeared from /dev/mapper/
. But still I cannot unmount the disk, because it reports: target is busy.
lsof
shows no files open from that disk:
# lsof +D
Why cannot i unmout the disk after all mappings have been freed up ?
Martin Vegter
(586 rep)
Aug 21, 2022, 03:39 PM
• Last activity: Aug 23, 2022, 05:15 PM
Showing page 1 of 4 total questions