Dd in priviliged container not working

HI! I’m trying to make raw lxc container for making bootable Armbian SD card.
I tested dd “zeroing” on SD in host system and it works as expected - SD card was zeroed just as planed.
Then I tried to create privileged lxc container and made some changes in container’s config file:
creating container:

# lxc-create -n ArmbianPriv -t download -- --dist debian --release bookworm --arch amd64

containers conf file looks like this:

# Template used to create this container: /usr/share/lxc/templates/lxc-download
# Parameters passed to the template: --dist debian --release bookworm --arch amd64
# Template script checksum (SHA-1): 78b012f582aaa2d12f0c70cc47e910e9ad9be619
# For additional config options, please look at lxc.container.conf(5)

# Uncomment the following line to support nesting containers:
#lxc.include = /usr/share/lxc/config/nesting.conf
# (Be aware this has security implications)


# Distribution configuration
lxc.include = /usr/share/lxc/config/common.conf
lxc.include = /usr/share/lxc/config/userns.conf
lxc.arch = linux64

# Container specific configuration
lxc.rootfs.path = dir:/var/lib/lxc/armbianDLNA/rootfs
lxc.uts.name = armbianDLNA

# Network configuration
lxc.net.0.type = veth
lxc.net.0.link = lxcbr0
lxc.net.0.flags = up
lxc.net.0.hwaddr = 00:16:3e:27:78:09

#Permissions to write to SD card  
lxc.cgroup.devices.allow = b 8:96 rwm #on host SD card is /dev/sdg 
lxc.mount.entry = /dev/sdg /dev/sdg none bind,optional,create=file

lxc container is starting and I cat attache to it succesfull, BUT when I make dd to SD card I recieve strange error “no space left on device”:

#from container: 
root@ArmbianPriv:/# dd if=/dev/zero of=/dev/sdg 
dd: writing to '/dev/sdg': No space left on device
977+0 records in
976+0 records out
499712 bytes (500 kB, 488 KiB) copied, 0.00362073 s, 138 MB/s

I also noticed that fdisk and parted is seeing my /dev/sdg (where SD card is) different:

#from container: 
root@ArmbianPriv:/# /usr/sbin/fdisk -l /dev/sdg
Disk /dev/sdg: 488 KiB, 499712 bytes, 976 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

from host parted /dev/sdg print:

Model: Generic STORAGE DEVICE (scsi)
Disk /dev/sdg: 31,9GB
Sector size: 512B/512B
Table: msdos
Flags: 

Номер  Начало  Конец   Размер  Тип      Файловая система  Флаги
 1     4194kB  1707MB  1703MB  primary  ext4

What am i doing wrong? Why SD card from container and from host looks different? What I have to do to fix that error?

So i figured out how to solve this problem myself.
I destroyed previously made container and made new one with the same name:

# lxc-create -n ArmbianPriv -f /etc/lxc/defaultPriv.conf -t download -- --dist debian --release bookworm --arch amd64
Using image from local cache
Unpacking the rootfs

---
You just created a Debian bookworm amd64 (20231221_05:24) container.

To enable SSH, run: apt install openssh-server
No default root or user password are set by LXC.

Then I used command lxc-device and added /dev/sdg to my privileged container:

# lxc-device -n ArmbianPriv add /dev/sdg /dev/sdg

after that dd in container worked as expeted:

# dd status=progress if=/dev/zero of=/dev/sdg 
115941888 bytes (116 MB, 111 MiB) copied, 41 s, 2.8 MB/s^C
226841+0 records in
226841+0 records out
116142592 bytes (116 MB, 111 MiB) copied, 49.0642 s, 2.4 MB/s

*I skiped zeroing after I have undetstood that it’s going fine to save time.
I didin’t made changes to container’s conf file.

My conclusion:
If you want to add block device to raw lxc container:

  • use privileged lxc container
  • use lxc-device -n <container name> /dev/<device> /dev/<deviceInContainer> command.

I mark problem solved.

1 Like