Configurando montagem automática de partições no linux com systemd .

Nesse artigo  eu vou mostrar como você pode configurar a montagem de discos na inicialização do linux  utilizando o systemd , uma alternativa ao fstab.

Esse artigo é válido para todas as distribuições linux que utilizam systemd.

Vou procurar ser bem didático para facilitar o entendimento.

No meu caso eu inseri um novo disco em meu SO , e vou particiona-lo.

Vou verificar o nome do meu disco no SO.

sudo fdisk -l
Disk /dev/sdb: 100 GiB, 107374182400 bytes, 209715200 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
Disklabel type: gpt
Disk identifier: 1611C92B-587F-4852-93B6-40BDCEBA3A86

Vamos usar o parted.

Vamos criar a tabela de partição em gpt.

parted -s /dev/sdb mklabel gpt

Criar a primeira partição fat32  de 10GB.

sudo parted -s /dev/sdb  mkpart primary fat32 1MiB 10GiB

Criar a partição ntfs de 10GB.

sudo parted -s /dev/sdb mkpart primary ntfs 10GiB 20.5GiB

Partição swap de 4GB.

sudo parted -s /dev/sdb mkpart primary linux-swap 20.5GiB 24.5GiB

Partição ext4 de 25GB.

sudo parted -s /dev/sdb mkpart primary ext4 24.5GiB 50GiB

Formatar as partições .

Fat32

sudo mkfs.vfat -v -n PARTICAOFAT /dev/sdb1

NTFS.

sudo mkfs.ntfs -v -f -L PARTICAONTFS /dev/sdb2

Swap.

sudo mkswap  -L PARTICAOSWAP /dev/sdb3

Ext4

sudo mkfs.ext4  -L PARTICAOEXT4 /dev/sdb4

Verificar a configuração das partições .

julio@srv01:~$ sudo blkid 
/dev/sdb1: LABEL="PARTICAOFAT" UUID="F73B-481A" TYPE="vfat" PARTLABEL="primary" PARTUUID="ec54f186-7bf8-44f4-a7d5-7790bf0e0de5"
/dev/sdb2: LABEL="PARTICAONTFS" UUID="60FAA38264F431B5" TYPE="ntfs" PTTYPE="dos" PARTLABEL="primary" PARTUUID="f53d2f01-1995-43ec-88ad-b9ae3a33c4f9"
/dev/sdb3: LABEL="PARTICAOSWAP" UUID="9430805e-b3d0-419e-9bef-cbc3f9b2d9fe" TYPE="swap" PARTLABEL="primary" PARTUUID="94ca10cd-885a-4176-8fbe-117b930f9b0b"
/dev/sdb4: LABEL="PARTICAOEXT4" UUID="06400e57-9b8d-467c-b5a0-cbcb6d8eca95" TYPE="ext4" PARTLABEL="primary" PARTUUID="8484c192-8b13-4065-bedc-fe26cbf5991a"

Criar os pontos de montagem.

sudo mkdir /media/PARTICAOFAT
sudo mkdir /media/PARTICAONTFS
sudo mkdir /media/PARTICAOEXT4
sudo mkdir /media/PARTICAOSWAP

Verificar o caminho para as partições .

Tipos disponíveis  .

julio@srv01:~$ ls -latr /dev/disk/
total 0
drwxr-xr-x  2 root root   60 mai 15 10:45 by-id
drwxr-xr-x 17 root root 4020 mai 15 10:52 ..
drwxr-xr-x  2 root root  220 mai 15 10:52 by-path
drwxr-xr-x  2 root root  160 mai 15 10:52 by-partuuid
drwxr-xr-x  8 root root  160 mai 15 10:55 .
drwxr-xr-x  2 root root  120 mai 15 11:07 by-label
drwxr-xr-x  2 root root  140 mai 15 11:07 by-uuid
drwxr-xr-x  2 root root   60 mai 15 15:45 by-partlabel

Você  pode utilizar  por  exemplo:  by-label , by-uuid  ou  /dev/sdb1  e etc.

julio@srv01:~$ ls -latr /dev/disk/by-label/
total 0
drwxr-xr-x 8 root root 160 mai 15 10:55 ..
lrwxrwxrwx 1 root root  10 mai 15 10:56 PARTICAOFAT -> ../../sdb1
lrwxrwxrwx 1 root root  10 mai 15 11:07 PARTICAOEXT4 -> ../../sdb4
drwxr-xr-x 2 root root 120 mai 15 11:07 .
lrwxrwxrwx 1 root root  10 mai 15 11:24 PARTICAONTFS -> ../../sdb2
lrwxrwxrwx 1 root root  10 mai 15 11:33 PARTICAOSWAP -> ../../sdb3
ls -latr /dev/disk/by-partuuid/
lrwxrwxrwx 1 root root  10 mai 15 10:45 fb026b45-b355-40e1-a72b-c9384ef33d96 -> ../../sda2
lrwxrwxrwx 1 root root  10 mai 15 10:45 57c8160e-bf66-425f-9d30-049a30861f5c -> ../../sda1
drwxr-xr-x 2 root root 160 mai 15 10:52 .
drwxr-xr-x 8 root root 160 mai 15 10:55 ..
lrwxrwxrwx 1 root root  10 mai 15 10:56 ec54f186-7bf8-44f4-a7d5-7790bf0e0de5 -> ../../sdb1
lrwxrwxrwx 1 root root  10 mai 15 11:07 8484c192-8b13-4065-bedc-fe26cbf5991a -> ../../sdb4
lrwxrwxrwx 1 root root  10 mai 15 11:24 f53d2f01-1995-43ec-88ad-b9ae3a33c4f9 -> ../../sdb2
lrwxrwxrwx 1 root root  10 mai 15 11:33 94ca10cd-885a-4176-8fbe-117b930f9b0b -> ../../sdb3

Conceito das units .mount do systemd :

No caso da partição /media/PARTICAOFAT  o nome da unit será media-PARTICAOFAT.mount .

Descrição dos conceitos da Unit.

Unit]
Description=media-PARTICAOFAT.mount

[Mount]
What=/dev/disk/by-label/PARTICAOFAT
#What=/dev/disk/by-partuuid/ec54f186-7bf8-44f4-a7d5-7790bf0e0de5
Where=/media/PARTICAOFAT
Type=vfat
Options=defaults

[Install]
WantedBy=graphical.target

 

Descrição . Local onde você pode colocar o texto descrevendo sua unit.

Description=media-PARTICAOFAT.mount

Caminho do disco:

What=/dev/disk/by-label/PARTICAOFAT

Ponto de montagem. Local onde o a partição deve ser montado

Where=/media/PARTICAOFAT

Sistema de arquivos.

Type=vfat

Opções de montagem;

Options=defaults

Aguarde até essa unit seja iniciada com sucesso.

WantedBy=graphical.target

Inciando.

Vamos criar as units .mount e .swap .

Fat32.

sudo  vi /etc/systemd/system/media-PARTICAOFAT.mount
[Unit]
Description=media-PARTICAOFAT.mount

[Mount]
What=/dev/disk/by-label/PARTICAOFAT
#What=/dev/disk/by-partuuid/ec54f186-7bf8-44f4-a7d5-7790bf0e0de5
Where=/media/PARTICAOFAT
Type=vfat
Options=defaults

[Install]
WantedBy=graphical.target

Ext4.

sudo vi /etc/systemd/system/media-PARTICAOEXT4.mount
[Unit]
Description=media-PARTICAOEXT4
          
[Mount]
What=/dev/disk/by-label/PARTICAOEXT4
#What=/dev/disk/by-partuuid/8484c192-8b13-4065-bedc-fe26cbf5991a
Where=/media/PARTICAOEXT4
Type=ext4
Options=defaults

[Install]
WantedBy=graphical.target

NTFS.

sudo  vi /etc/systemd/system/media-PARTICAONTFS.mount
[Unit]
Description=media-PARTICAONTFS

[Mount]
What=/dev/disk/by-label/PARTICAONTFS
#What=/dev/disk/by-partuuid/f53d2f01-1995-43ec-88ad-b9ae3a33c4f9
Where=/media/PARTICAONTFS
Type=ntfs
Options=defaults

[Install]
WantedBy=graphical.target

Swap.

sudo vi  /etc/systemd/system/dev-sdb3.swap
[Unit]
Description=dev-sdb3.swap

[Swap]
What=/dev/sdb3

[Install]
WantedBy=multi-user.target

Vamos inciar as units.

sudo systemctl start media-PARTICAONTFS.mount
sudo systemctl start media-PARTICAOEXT4.mount
sudo systemctl start media-PARTICAOFAT.mount
sudo systemctl start dev-sdb3.swap

Ativar na inicialização.

sudo systemctl enable media-PARTICAONTFS.mount
sudo systemctl enable media-PARTICAOEXT4.mount
sudo systemctl enable media-PARTICAOFAT.mount
sudo systemctl enable dev-sdb3.swap
julio@srv01:~$ sudo systemctl enable media-PARTICAONTFS.mount
Created symlink /etc/systemd/system/graphical.target.wants/media-PARTICAONTFS.mount → /etc/systemd/system/media-PARTICAONTFS.mount.
julio@srv01:~$ sudo systemctl enable media-PARTICAOEXT4.mount
Created symlink /etc/systemd/system/graphical.target.wants/media-PARTICAOEXT4.mount → /etc/systemd/system/media-PARTICAOEXT4.mount.
julio@srv01:~$ sudo systemctl enable media-PARTICAOFAT.mount
Created symlink /etc/systemd/system/graphical.target.wants/media-PARTICAOFAT.mount → /etc/systemd/system/media-PARTICAOFAT.mount.
julio@srv01:~$ sudo systemctl enable dev-sdb3.swap
Created symlink /etc/systemd/system/multi-user.target.wants/dev-sdb3.swap → /etc/systemd/system/dev-sdb3.swap.

Ver o status.

sudo systemctl status media-PARTICAONTFS.mount
sudo systemctl status media-PARTICAOEXT4.mount
sudo systemctl status media-PARTICAOFAT.mount
sudo systemctl status dev-sdb3.swap
julio@srv01:~$ sudo systemctl status media-PARTICAONTFS.mount
● media-PARTICAONTFS.mount - media-PARTICAONTFS
   Loaded: loaded (/etc/systemd/system/media-PARTICAONTFS.mount; enabled; vendor preset: enabled)
   Active: active (mounted) since Sat 2020-05-16 14:17:15 -03; 18min ago
    Where: /media/PARTICAONTFS
     What: /dev/sdb2
    Tasks: 1 (limit: 1080)
   CGroup: /system.slice/media-PARTICAONTFS.mount
           └─869 /sbin/mount.ntfs /dev/sdb2 /media/PARTICAONTFS -o rw

mai 16 14:17:15 srv01 systemd[1]: Mounting media-PARTICAONTFS...
mai 16 14:17:15 srv01 systemd[1]: Mounted media-PARTICAONTFS.
mai 16 14:17:15 srv01 ntfs-3g[869]: Version 2017.3.23 integrated FUSE 28
mai 16 14:17:15 srv01 ntfs-3g[869]: Mounted /dev/sdb2 (Read-Write, label "PARTICAONTFS", NTFS 3.1)
mai 16 14:17:15 srv01 ntfs-3g[869]: Cmdline options: rw
mai 16 14:17:15 srv01 ntfs-3g[869]: Mount options: rw,allow_other,nonempty,relatime,fsname=/dev/sdb2,blkdev,blksize=4096
mai 16 14:17:15 srv01 ntfs-3g[869]: Ownership and permissions disabled, configuration type 7

julio@srv01:~$ sudo systemctl status media-PARTICAOEXT4.mount
● media-PARTICAOEXT4.mount - media-PARTICAOEXT4
   Loaded: loaded (/etc/systemd/system/media-PARTICAOEXT4.mount; enabled; vendor preset: enabled)
   Active: active (mounted) since Sat 2020-05-16 14:17:15 -03; 18min ago
    Where: /media/PARTICAOEXT4
     What: /dev/sdb4
    Tasks: 0 (limit: 1080)
   CGroup: /system.slice/media-PARTICAOEXT4.mount

mai 16 14:17:15 srv01 systemd[1]: Mounting media-PARTICAOEXT4...
mai 16 14:17:15 srv01 systemd[1]: Mounted media-PARTICAOEXT4.

julio@srv01:~$ sudo systemctl status media-PARTICAOFAT.mount
● media-PARTICAOFAT.mount
   Loaded: loaded (/etc/systemd/system/media-PARTICAOFAT.mount; enabled; vendor preset: enabled)
   Active: active (mounted) since Sat 2020-05-16 14:17:15 -03; 18min ago
    Where: /media/PARTICAOFAT
     What: /dev/sdb1
    Tasks: 0 (limit: 1080)
   CGroup: /system.slice/media-PARTICAOFAT.mount

mai 16 14:17:15 srv01 systemd[1]: Mounting media-PARTICAOFAT.mount...
mai 16 14:17:15 srv01 systemd[1]: Mounted media-PARTICAOFAT.mount.

julio@srv01:~$ sudo systemctl status dev-sdb3.swap
● dev-sdb3.swap
   Loaded: loaded (/etc/systemd/system/dev-sdb3.swap; enabled; vendor preset: enabled)
   Active: active since Sat 2020-05-16 14:27:56 -03; 7min ago
     What: /dev/sdb3
    Tasks: 0 (limit: 1080)
   CGroup: /system.slice/dev-sdb3.swap

mai 16 14:27:56 srv01 systemd[1]: Activating swap dev-sdb3.swap...
mai 16 14:27:56 srv01 systemd[1]: Activated swap dev-sdb3.swap.

Comando mount.

julio@srv01:~$ mount
/dev/sdb1 on /media/PARTICAOFAT type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro)
/dev/sdb4 on /media/PARTICAOEXT4 type ext4 (rw,relatime,data=ordered)
/dev/sdb2 on /media/PARTICAONTFS type fuseblk (rw,relatime,user_id=0,group_id=0,allow_other,blksize=4096)

swapon .

julio@srv01:~$ swapon
NAME      TYPE      SIZE USED PRIO
/swap.img file      1,9G   0B   -2
/dev/sdb3 partition   4G   0B   -3
julio@srv01:~$ systemctl list-units --type=mount
media-PARTICAOEXT4.mount      loaded active mounted media-PARTICAOEXT4                
media-PARTICAOFAT.mount       loaded active mounted media-PARTICAOFAT.mount           
media-PARTICAONTFS.mount      loaded active mounted media-PARTICAONTFS
julio@srv01:~$ systemctl list-units --type=swap
UNIT          LOAD   ACTIVE SUB    DESCRIPTION                                                                                                                                   
dev-sdb3.swap loaded active active dev-sdb3.swap                                                                                                                                 
swap.img.swap loaded active active /swap.img                                                                                                                                     

Você pode ver o conteúdo da unit.

systemctl cat dev-sdb3.swap
julio@srv01:~$ systemctl cat dev-sdb3.swap
# /etc/systemd/system/dev-sdb3.swap
[Unit]
Description=dev-sdb3.swap

[Swap]
What=/dev/sdb3

[Install]
WantedBy=multi-user.target
julio@srv01:~$ 

Deixe seu comentário.

Deixe uma resposta

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *