Sparse file taking all fallocate()d space at once - sparse-file

I'm trying to create a sparse file (for a QEMU HDD image).
Both qemu-img and fallocate are proving confusing.
$ fallocate -l 100M disk.img
$ ls -lsh disk.img
101M -rw-r--r-- 1 i336 users 100M Jul 22 12:03 disk.img
Note the 101M. strace shows a successful syscall:
$ strace fallocate -l 100M disk.img
open("disk.img", O_RDWR|O_CREAT|O_LARGEFILE, 0666) = 3
fallocate(3, 0, 0, 104857600) = 0
$ ls -lsh disk.img
101M -rw-r--r-- 1 i336 users 100M Jul 22 12:03 disk.img
I'm not sure if stat is the right tool, but just in case..
$ stat disk.img
File: 'disk.img'
Size: 104857600 Blocks: 204808 IO Block: 4096 regular file
Device: 802h/2050d Inode: 549166 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1337/ i336) Gid: ( 100/ users)
A possible (very weird) clue: 104857600/204808 = 511.9800. (File size / block count)
qemu-img has similar output. (I found the preallocation option in the manual.)
$ qemu-img create -f raw -o preallocation=falloc disk.img 100M
Formatting 'disk.img', fmt=raw size=104857600 preallocation=falloc
$ ls -lsh disk.img
101M -rw-r--r-- 1 i336 users 100M Jul 22 12:06 disk.img
Here's the annoying bit: the image appears to be using real space on disk.
$ df -h /; fallocate -l 1G disk.img; df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/root 48G 43G 3.5G 93% /
Filesystem Size Used Avail Use% Mounted on
/dev/root 48G 44G 2.5G 95% /
And yet, just like a sparse file, it takes no time to create!
$ time fallocate -l 3.3G disk.img
0.00user 0.57system 0:00.91elapsed 63%CPU (0avgtext+0avgdata 5424maxresident)k
200inputs+0outputs (0major+68minor)pagefaults 0swaps
0.91 seconds, on a 5400RPM HDD. There is no way I'm not creating a sparse file.
And yet no matter what tool I use, it appears to be using 101MB of space right off the bat.
What could I be doing wrong or have misconfigured?
$ cat /etc/fstab
/dev/sda2 / ext4 rw,user_xattr 0 0

Related

Paramiko exec_command not working with mkfs?

Some issue executing the following bash with Paramiko:
def format_disk(self, device, size, dformat, mount, name):
stdin_, stdout_, stderr_ = self.client.exec_command(f"pvcreate {device};" \
f"vgcreate {name}-vg {device};" \
f"lvcreate -L {size} --name {name}-lv {name}-vg;" \
f"mkfs.{dformat} /dev/{name}-vg/{name}-lv;" \
f"mkdir {mount};" \
f"echo '/dev/{name}-vg/{name}-lv {mount} {dformat} defaults 0 0' >> /etc/fstab")
print(f"mkfs.{dformat} /dev/{name}-vg/{name}-lv;")
Print statement outputs: mkfs.ext4 /dev/first_try-vg/first_try-lv; If I copy and paste this exact command on the server there are no errors and it formats the disk as expected.
Troubleshooting steps
Server before running python script:
ls: cannot access /first_try: No such file or directory
[root#localhost ~]# vgs
[root#localhost ~]# lvs
[root#localhost ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Feb 25 07:32:51 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=38b7e96a-71e5-4089-a348-bd23828f9dc8 / xfs defaults 0 0
UUID=72fd2a6a-85db-4596-9fc2-6604d0d865a3 /boot xfs defaults 0 0
Server after running python script:
[root#localhost ~]# ls /first_try/
[root#localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
first_try-vg 1 1 0 wz--n- <20.00g <15.00g
[root#localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
first_try-lv first_try-vg -wi-a----- 5.00g
[root#localhost ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Feb 25 07:32:51 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=38b7e96a-71e5-4089-a348-bd23828f9dc8 / xfs defaults 0 0
UUID=72fd2a6a-85db-4596-9fc2-6604d0d865a3 /boot xfs defaults 0 0
/dev/first_try-vg/first_try-lv /first_try ext4 defaults 0 0
[root#localhost ~]# mount -a
mount: wrong fs type, bad option, bad superblock on /dev/mapper/first_try--vg-first_try--lv,
missing codepage or helper program, or other error
In some cases useful info is found in syslog - try
dmesg | tail or so.
The error from mount -a indicates that the disk is not formatted.
If I format the disk manually and run mount -a it works.
Example:
[root#localhost ~]# mkfs.ext4 /dev/first_try-vg/first_try-lv
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): mdone
Writing superblocks and filesystem accounting information: done
[root#localhost ~]# mount -a
[root#localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 4.7G 14G 27% /
devtmpfs 471M 0 471M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 8.4M 478M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 297M 147M 151M 50% /boot
tmpfs 98M 12K 98M 1% /run/user/42
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/first_try--vg-first_try--lv 4.8G 20M 4.6G 1% /first_try
Pariminko could not handle the output from mkfs. I changed the command to use the -q quiet flag and was able to get the script to run successfully.
New commmand mkfs -q -t {dformat} /dev/{name}-vg/{name}-lv

File bigger than the size of the partition it's in

I have a server in which there's a file with 30GB inside the partition /, although df -h lists this partition as using 11GB. Why does that happen?
[root#APPSERVER21-S1 ~]# ls -l /etc/vinter/logs/
total 2046032
-rw-r--r-- 1 root root 3920496 Sep 11 14:35 PlusoftCRMIntegration-APISILVERNODE-1.log
-rw-r--r-- 1 root root 30823671719 Sep 11 15:13 rsp.appserver21-s1.apigoldnode-1.api-oi-gold.log
-rw-r--r-- 1 root root 406061056 Sep 11 15:13 rsp.appserver21-s1.apisilvernode-1.api-oi-silver.log
[root#APPSERVER21-S1 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 3.6G 0 3.6G 0% /dev
tmpfs 3.6G 0 3.6G 0% /dev/shm
tmpfs 3.6G 137M 3.4G 4% /run
tmpfs 3.6G 0 3.6G 0% /sys/fs/cgroup
/dev/mapper/vg_main-lv_root 16G 5.0G 11G 32% /
/dev/mapper/vg_dados-lv_dados 20G 33M 20G 1% /datastorage
/dev/mapper/vg_dados-lv_docker 80G 128M 79G 1% /var/lib/docker
/dev/xvdb1 497M 161M 337M 33% /boot
tmpfs 722M 0 722M 0% /run/user/0
I have a server in which there's a file with 30GB inside the partition /, although df -h lists this partition as using 11GB.
Actually, it lists this filesystem as using 5 GB, not 11 GB.
Why does that happen?
This is impossible to answer without knowing what the contents of that file are, what the filesystem is, how the file was created, and so on.
There are filesystems that perform data-deduplication. If there are blocks with identical content, they will be replaced with links, so that they only take up the space of one block. So, if there is a lot of duplicate data on the filesystem, it is easily possible that the files take up much less space on-disk than their duplicated contents.
There are filesystems that perform compression. If there is a lot of redundancy on the filesystem, then the compression will reduce the data dramatically, and it is easily possible that the files take up much less space on-disk than their uncompressed contents.
Many filesystems support sparse files. Sparse files are files with "holes" in them. The holes will be read as a long string of binary zeroes, and are technically part of the content of the file, but they are not stored on-disk.

/media directory not working anymore

I can't automount USB sticks on my linux because I have several problems with /media directory.
Here is my ls -al result on / (I just kept the media and mnt directories for you) :
total 116
drwxr-xr-x 25 root root 4096 juin 13 09:39 .
drwxr-xr-x 25 root root 4096 juin 13 09:39 ..
drwx------ 8 acarbonaro acarbonaro 8192 janv. 1 1970 media
drwxr-xr-x 2 root root 4096 avril 11 2014 mnt
This already seems strange as for other users it is often owned by root.
When I try to sudo chown root:root media it says permission denied.
When I try to sudo chown 755 media it doesn't say anything but when I ls -l after nothing has changed.
The other problem : I don't know why but the media directory is empty I can't find the user directory that used to be in it.
When I plug a USB flash drive, it cannot auto mount. I have to mount it manually in another directory, which is not impossible but clearly not handy.
Thank you for your help.
EDIT:
Here is my df -T result :
Sys. de fichiers Type blocs de 1K Utilisé Disponible Uti% Monté sur
udev devtmpfs 4015584 8 4015576 1% /dev
tmpfs tmpfs 805680 1212 804468 1% /run
/dev/sda1 ext4 115214888 9815468 99523708 9% /
none tmpfs 4 0 4 0% /sys/fs/cgroup
none tmpfs 5120 0 5120 0% /run/lock
none tmpfs 4028392 522580 3505812 13% /run/shm
none tmpfs 102400 600 101800 1% /run/user
/dev/sda2 ext4 130654772 18532260 105462572 15% /home
/dev/sdb2 vfat 14938864 218480 14720384 2% /media
EDIT:
I don't know the answer to my problem, but rebooting reset the /media directory as it was before and it works agian.
I assume the problem was that you have yanked the USB stick out of port without unmounting. UNIX is not very keen to parts of its FS disappearing. Next time, umount it first, then remove.

Hudson server always stopped every morning day

I've got this regular problem every morning that my build server (Hudson) is always stopped every morning so I have to manually start it, is there any reason why or any location that I can started to look for the error message?
Here's the error diagnostic that I did:
ascari:~# ps -ef | grep -i hud
root 5959 5944 0 09:00 pts/0 00:00:00 grep -i hud
ascari:~# cd /etc/init.d
ascari:/etc/init.d# ./hudson start
ascari:/etc/init.d# ps -ef | grep -i hud
hudson 6004 1 0 09:00 ? 00:00:00 /usr/bin/daemon --name=hudson -- inherit --env=HUDSON_HOME=/var/lib/hudson --output=/var/log/hudson/hudson.log -- user=hudson --pidfile=/var/run/hudson/hudson.pid -- /usr/bin/java -Xms512m -Xmx1 024m -Dhttp.proxyHost=proxy.domain.com -Dhttp.proxyPort=3128 -Dhttp.nonProxyHo sts="localhost|ascari|*.domain.com" -jar /usr/share/hudson/hudson.war --webroo t=/var/run/hudson/war
hudson 6005 6004 48 09:00 ? 00:00:01 /usr/bin/java -Xms512m -Xmx1024m -Dhttp.proxyHost=proxy.domain.com -Dhttp.proxyPort=3128 -Dhttp.nonProxyHosts= "localhost|ascari|*.domain.com" -jar /usr/share/hudson/hudson.war --webroot=/v ar/run/hudson/war
root 6008 5944 14 09:01 pts/0 00:00:00 grep -i hud
ascari:/etc/init.d# df -k -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 327M 125M 185M 41% /
tmpfs 1.5G 0 1.5G 0% /lib/init/rw
udev 10M 96K 10M 1% /dev
tmpfs 1.5G 0 1.5G 0% /dev/shm
/dev/sda9 4.7G 295M 4.1G 7% /home
/dev/sda8 4.2G 155M 3.8G 4% /tmp
/dev/sda5 4.6G 3.0G 1.4G 69% /usr
/dev/sda6 65G 32G 30G 52% /var
ascari:/etc/init.d# uname -a
Linux ascari 2.6.26-2-686 #1 SMP Sun Jun 21 04:57:38 UTC 2009 i686 GNU/Linux
ascari:/etc/init.d#
Have you checked the logfile (referenced above) and set the --logfile argument (as documented here) ?
Rescheduling the project build solve the problem.
The Hudson process was killed by the Linux kernel due to the memory over consumption.

knowing a device special file major and minor numbers in linux

All files in /dev are special files... they represent devices of the computer.
They were created with the mknod syscall. My question is: How can I know the minor and
major numbers that were used to create this special file?
The list is called the LANANA Linux Device List, and it is administered by Alan Cox.
You can find the latest copy online (direct link), or in the Linux source. Its filename in the kernel tree is Documentation/devices.txt.
To see the major and minor numbers that created a node in /dev (or any device node for that matter), simply use ls with the -l option:
22:26 jsmith#undertow% ls -l /dev/xvd?
brw-rw---- 1 root disk 202, 0 Nov 1 20:31 /dev/xvda
brw-rw---- 1 root disk 202, 16 Nov 1 20:31 /dev/xvdb
brw-rw---- 1 root disk 202, 32 Nov 1 20:31 /dev/xvdc
In this example, 202 is the three devices' major number, and 0, 16, and 32 are minors. The b at left indicates that the node is a block device. The alternative is c, a character device:
crw-rw-rw- 1 root tty 5, 0 Nov 22 00:29 /dev/tty
$ ls -l /dev/fd0 /dev/null
brw-rw---- 1 root floppy 2, 0 Nov 22 19:48 /dev/fd0
crw-rw-rw- 1 root root 1, 3 Nov 22 19:48 /dev/null
$ stat -c '%n: %F, major %t minor %T' /dev/fd0 /dev/null
/dev/fd0: block special file, major 2 minor 0
/dev/null: character special file, major 1 minor 3
Most device numbers are fixed (i.e. /dev/null will always be character device 1:3) but on Linux, some are dynamically allocated.
$ cat /proc/devices
Character devices:
...
10 misc
...
Block devices:
...
253 mdp
254 device-mapper
$ cat /proc/misc
...
57 device-mapper
...
For example, on this system, it just so happens that /dev/mapper/control will be c:10:57 while the rest of /dev/mapper/* will be b:254:*, and this could differ from one boot cycle to another -- or even as modules are loaded/unloaded and devices are added/removed.
You can explore these device registrations further in /sys.
$ readlink /sys/dev/block/2:0
../../devices/platform/floppy.0/block/fd0
$ cat /sys/devices/platform/floppy.0/block/fd0/dev
2:0
$ readlink /sys/dev/char/1:3
../../devices/virtual/mem/null
$ cat /sys/devices/virtual/mem/null/dev
1:3
You can also use stat.
$ stat -c 'major: %t minor: %T' <file>
Especially for block devices:
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 90G 0 disk
├─sda1 8:1 0 4G 0 part [SWAP]
├─sda2 8:2 0 4G 0 part /
Alternative that doesn't depend on stat:
$ cat /sys/class/*/random/dev
1:8

Resources