Disable swap in systemd

Tested in ubuntu 18 / debian 9

Usually you can disable the swap by this command:

~ $ swapoff -a

And then comment entry swap line in /etc/fstab file for making reboot persistent. But sometimes in systems with systemd this is not enough, steps would be these:

Get the swap service name in use:

~ $ systemctl --type swap

UNIT                                       LOAD   ACTIVE SUB    DESCRIPTION                  
dev-mapper-ubuntu\x2d\x2dvg\x2dswap_1.swap loaded active active /dev/mapper/ubuntu--vg-swap_1

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

1 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

Stop service, note “‘” on start and end in service name:

~ $ systemctl stop 'dev-mapper-ubuntu\x2d\x2dvg\x2dswap_1.swap'

Mask service:

~ $ systemctl mask 'dev-mapper-ubuntu\x2d\x2dvg\x2dswap_1.swap'
Created symlink /etc/systemd/system/dev-mapper-ubuntu\x2d\x2dvg\x2dswap_1.swap → /dev/null.

Finally check service:

~ $ systemctl --type swap
0 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

If you want enable swap again:

~ $ systemctl unmask 'dev-mapper-ubuntu\x2d\x2dvg\x2dswap_1.swap'
~ $ systemctl start 'dev-mapper-ubuntu\x2d\x2dvg\x2dswap_1.swap'

Creating lost+found directory

Directory lost+found is a system directory placed on the root partition of the file system. e2fsck uses this special directory to deposit unlinked files after a file system repair.

Extracted from the man help: “mklost+found pre-allocates disk blocks to the lost+found directory so that when e2fsck is being run to recover a filesystem, it does not need to allocate blocks in the filesystem to store a large number of unlinked files. This ensures that e2fsck will not have to allocate data blocks in the filesystem during recovery.”

So it’s important this directory exists, if it was erased accidentally it’s necessary to create it:

~ $ cd /
~ $ mklost+found
~ $ cd /lost+found
~ $ ls -lha
total 20K
drwx------  2 root root  16K May  3  2016 .
drwxr-x--x 24 root root 4.0K Nov 1 23:23 ..

On the file system root partition.

Using MAKEDEV

You can use MAKEDEV command to create a structure of devices files under /dev using for example in chroot environment or for virtualization.

In Debian/Ubuntu install:

~ $ apt-get install makedev

Go to the /dev directory (or wherever you want to create dev files) and exec MAKEDEV:

~ $ cd /dev
~ $ MAKEDEV std

With the option “std” MAKEDEV create a standard devices group, mem, kmem, loop, null, port, core, full, ram, zero, random and tty:

~ $ ls -lha /dev

lrwxrwxrwx  1 root  root     11 abr  2 21:34 core -> /proc/kcore
crw-rw-rw-  1 root  root  1,  7 abr  2 21:34 full
crw-r-----  1 root  kmem  1,  2 abr  2 21:34 kmem
brw-rw----  1 root  disk  7,  0 abr  2 21:34 loop0
brw-rw----  1 root  disk  7,  1 abr  2 21:34 loop1
brw-rw----  1 root  disk  7,  2 abr  2 21:34 loop2
brw-rw----  1 root  disk  7,  3 abr  2 21:34 loop3
brw-rw----  1 root  disk  7,  4 abr  2 21:34 loop4
brw-rw----  1 root  disk  7,  5 abr  2 21:34 loop5
brw-rw----  1 root  disk  7,  6 abr  2 21:34 loop6
brw-rw----  1 root  disk  7,  7 abr  2 21:34 loop7
crw-r-----  1 root  kmem  1,  1 abr  2 21:34 mem
crw-rw-rw-  1 root  root  1,  3 abr  2 21:34 null
crw-r-----  1 root  kmem  1,  4 abr  2 21:34 port
lrwxrwxrwx  1 root  root      4 abr  2 21:34 ram -> ram1
brw-rw----  1 root  disk  1,  0 abr  2 21:34 ram0
brw-rw----  1 root  disk  1,  1 abr  2 21:34 ram1
brw-rw----  1 root  disk  1, 10 abr  2 21:34 ram10
brw-rw----  1 root  disk  1, 11 abr  2 21:34 ram11
brw-rw----  1 root  disk  1, 12 abr  2 21:34 ram12
brw-rw----  1 root  disk  1, 13 abr  2 21:34 ram13
brw-rw----  1 root  disk  1, 14 abr  2 21:34 ram14
brw-rw----  1 root  disk  1, 15 abr  2 21:34 ram15
brw-rw----  1 root  disk  1, 16 abr  2 21:34 ram16
brw-rw----  1 root  disk  1,  2 abr  2 21:34 ram2
brw-rw----  1 root  disk  1,  3 abr  2 21:34 ram3
brw-rw----  1 root  disk  1,  4 abr  2 21:34 ram4
brw-rw----  1 root  disk  1,  5 abr  2 21:34 ram5
brw-rw----  1 root  disk  1,  6 abr  2 21:34 ram6
brw-rw----  1 root  disk  1,  7 abr  2 21:34 ram7
brw-rw----  1 root  disk  1,  8 abr  2 21:34 ram8
brw-rw----  1 root  disk  1,  9 abr  2 21:34 ram9
crw-rw-rw-  1 root  root  1,  8 abr  2 21:34 random
crw-rw-rw-  1 root  tty   5,  0 abr  2 21:34 tty
crw-rw-rw-  1 root  root  1,  9 abr  2 21:34 urandom
crw-rw-rw-  1 root  root  1,  5 abr  2 21:34 zero

Clone disks

To clone a entire disk you can use the dd command:

~ $ dd if=/dev/sda of=/dev/sdb bs=10240k conv=noerror,sync

Or dd_rescue, more reliable than dd:

~ $ dd_rescue -v /dev/sda /dev/sdb

Also you can use pv to watch the progress:

~ $ dd if=/dev/sda bs=10240k conv=noerror,sync | pv -c -b -r -t > /dev/sdb

About a partition table you can copy it from a disk to another:

~ $ sfdisk -d /dev/sda | sfdisk /dev/sdb

Or save to a file:

~ $ sfdisk -d /dev/sda > partition-table.output 

And restore then:

~ $ sfdisk /dev/sda < partition-table.output 

LVM Encryption with Luks

First create the logical volume:

~ $ lvcreate -L100G -n lv01 vg0

Now create the encrypted volume, you will be asked for the master password, it’s very important you save it safely.

~ $ cryptsetup -y -v luksFormat /dev/vg/lv01

Open the encrypted volume, is mapping to /dev/mapper:

~ $ cryptsetup luksOpen /dev/vg/lv01 clv01

Show the status:

~ $ cryptsetup -v status /dev/mapper/clv01
/dev/mapper/clv01 is active.
  type:    LUKS1
  cipher:  aes-cbc-essiv:sha256
  keysize: 256 bits
  device:  /dev/mapper/vg-lv01
  offset:  4096 sectors
  size:    2147479552 sectors
  mode:    read/write
  Command successful.

To see the headers on the encrypted partition:

~ $ cryptsetup luksDump /dev/vg/lv01

It’s advisable writing zeros to the disk, be patient with a huge disk:

~ $ dd if=/dev/zero of=/dev/mapper/clv01

Format the file system with the desirable format:

~ $ mkfs.ext4 /dev/mapper/clv01

Mount device, for example in /mnt/DATA:

~ $ mount /dev/mapper/clv01 /mnt/DATA/

Unmount device safely:

~ $ umount /dev/mapper/clv01
~ $ cryptsetup luksClose /dev/mapper/clv01