How to Compress or write zero's /dev/zero to a swap file? - linux

We have a few linux based (Centos) virtual machines which are to be used as distributable virtual appliances. We want to be able to compress them as much as possible for distribution ( via tar.gz, zip, etc).
We've removed all unnecessary files (.log's, /tmp/*, /var/log/, etc) and have written /dev/zero to the free space on the disk.
Is it possible to write zeros via /dev/zero to the swap partitions and files? I know I would need to swapoff -a first. I'm worried about corrupting any internal structures.
Our vm uses both partition swap and file swap.
Also, are there any other strategies for reducing the size of a VM for distribution?
We need to support all of the hypervisor technologies (Xen, VMW, etc), so although the vendors tools maybe useful, I'm looking for strategies that are cross platform.
--- Thanks

You may want to write zeroes and then use mkswap to create an empty swap partition.

$ dd -if=/dev/zero of=/path/to/file bs=512 count=1
adjust the size that you want your files to be.

sudo swapoff -v /dev/sda2 <== The swap partition
sudo dd if=/dev/zero of=/dev/sda2 bs=512 status=progress
sudo mkswap /dev/sda2 UUID=46c1a133-bfdd-4695-a484-08fcf8286896 <== The original UUID of the swap partition

Related

Linux create swap using dd: swapon failed: Invalid argument

I have a swap file called /dev/dm-1 with 1G size, try to increase the size of the swap file to 4G using the steps below:
Turn off swap: swapoff /dev/dm-1
Remove old swap file:
rm /dev/dm-1
Create swap file using dd comand: dd if=/dev/zero of=/dev/dm-1 count=4096 bs=1MiB status=progress
Restrict privelages: chmod 600 /dev/dm-1
Setting up swapspace: mkswap /dev/dm-1
Start swap: swapon /dev/dm-1
After starting up swap that show error swapon failed: Invalid argument
I using SMP Debian 4.19.181-1 (2021-03-19) and filesystem is ext4
Can someone help?
Thanks, KamilCuk I create a swap in /srv directory and all works now.
Is /dev/dm-1 really a file? Isn't it a device? It's very odd to
create a file in /dev. Do not do it, /dev should be mounted as
devtmpfs, you could be basically creating a swap file in memory and
calling it swap.... Do not create regular files in /dev. Create the
swap file somewhere else, like in /srv. What does stat /dev/dm-1
output? What does findmnt /dev output? – KamilCuk

gcloud instance disk space

I am trying to do some computing on cloud. For this I created a computing instance and then I attached an external storage with about 10TB. But it seemed that I did something wrong and I got only 200GB available for my datalab. Any comment will be helpful
To check this I used
df -h
and
sudo lsblk
Thanks.
As I can see from lsblk command, you have the right size of your datalab-pd disk.
But you can use only 196 Gb.
I think this may be because the file system does not occupy the entire disk space.
Need to extend the file system.
As an example if you have ext3 fs need to do:
- umount /dev/sdb # Unmount your disk
- e2fsck /dev/sdb # Check file system in your disk
- resize2fs /dev/sdb
resize2fs command without any parameters will extend filesystem to all free space on disk.
More info: https://access.redhat.com/articles/1196353

How to check a disk for partitions for use in a script in Linux?

I'm scripting something in Bash for Linux systems. How would I check a disk for partitions in a robust manner?
I could use grep, awk, or sed to parse the output from fdisk, sfdisk, etc., but this doesn't seem to be an exact science.
I could also check if there are partitions in /dev, but it is also possible that the partitions exist and haven't been probed yet (via partprobe, as an example).
What would you recommend?
I think I figured out a reliable way. I accidentally learned some more features of partprobe while reading the man page:
-d Don’t update the kernel.
-s Show a summary of devices and their partitions.
Used together, I can scan a disk for partitions without updating the kernel and get a reliable output to parse. It's still parsing text, but at least the output isn't as "human-oriented" as fdisk or sfdisk. This also is information as read from the disk and doesn't rely on the kernel being up-to-date on the partition status for this disk.
Take a look:
On a disk with no partition table:
# partprobe -d -s /dev/sdb
(no output)
On a disk with a partition table but no partitions:
# partprobe -d -s /dev/sdb
/dev/sdb: msdos partitions
On a disk with a partition table and one partition:
# partprobe -d -s /dev/sdb
/dev/sdb: msdos partitions 1
On a disk with a partition table and multiple partitions:
# partprobe -d -s /dev/sda
/dev/sda: msdos partitions 1 2 3 4 <5 6 7>
It is important to note that every exit status was 0 regardless of an existing partition table or partitions. In addition, I also noticed that the options cannot be grouped together (partprobe -d -s /dev/sdb works while partprobe -ds /dev/sdb does not).
Another option is to run:
lsblk
See https://unix.stackexchange.com/a/108951
you could also use:
parted /dev/sda print 1 &> /dev/null echo $?
if a partition (first partition) exist it return true and otherwise false

Hard drive clone using dd

I have two hard drives:
sda ST3500...blabla (doesn't matter) and sdb WD...blabla. I want to clone sda to sdb using dd.
I ran:
dd if=/dev/sda of=/dev/sdb bs=4096 conv=notrunc,noerror,sync.
The problem is that dd also clones the label of sda (ST3500...) onto sdb. While booting SUSE from sdb it searches for WD... and fails to boot. Is there a way to clone a whole drive with dd and maintaining the target drive label/model information?
The label is in the filesystem, not in the hard drive. It can be modified by filesystem tools such as tune2fs -L for ext2/3/4 filesystems. Simply modify the label after cloning.
you can change the label manually after cloning
I started a thread here for a debian system but I am sure it will also apply for SUSE
debian forum link

How to create loop partition from already existing partition

I believe /images/backups is using the space in /images ?
/dev/sdb1 820G 645G 135G 83% /images
/dev/loop0 296G 296G 0 100% /images/backups
I've a similar kind of partition in another machine /images which is 500G free, and I would want to take out 350G for /images/backups, how to do it ?
Is it right that, it is a simple loop mount which can give specified amount of space or we should create a NULL file of required size and mount ? If so, what are the mount options should be used to specify the size ?
You'll need to create the destination with a fixed size, but can use a "sparse file" which doesn't actually have any blocks written to it yet (and which thus doesn't actually consume space until you write to it).
For instance:
dd if=/dev/zero of=file.img bs=1 count=0 seek=20G
will create a sparse file preallocated to 20GB. That said, actually writing 20GB of zeros to disk up-front (making the file non-sparse) will be faster on writes and lead to less fragmentation.
This can be attached to a loopback device with the losetup command, have a filesystem created, and be mounted:
losetup /dev/loop1 file.img
mke2fs -j /dev/loop1
mount /dev/loop1 /mnt/somewhere
If you want to know if an existing file is sparse, the following will do the trick (on a system with GNU tools; some of the below is not supported in a pure POSIX environment):
{
read block_count block_size file_size
if (( block_count * block_size < file_size )) ; then
echo "Sparse"
else
echo "Non-Sparse"
fi
} < <(stat --format='%b %B %s'$'\n' /images/backups.img)

Resources