Disminuir el tamaño de una partición en Linux
Linux
Publicado el 20 de Diciembre del 2020 por Xve
6.052 visualizaciones desde el 20 de Diciembre del 2020
Aquí detallo los pasos que he tenido que realizar para disminuir una partición ext4 en un disco SSD para poner ahí la partición swap de la maquina.
Dispongo de un servidor Debian con un solo disco duro mecánico, y he tenido que añadir un segundo disco duro SSD para poner ahí una base de datos mongoDB de mas de 100Gb.
Algunas consultas en esta base de datos, necesitan el uso de memoria swap ya que la maquina no dispone de mucha memoria, pero al utilizar la memoria swap del disco mecánico, la velocidad disminuía bastante, por lo que hemos decidido modificar el disco SSD para añadir ahí la partición swap sin perder los datos.
Este es el disco inicialmente que queremos rebajarla unos 16Gb a la partición para poner ahí la swap:
Es importante fijarse en el inicio del disco, ya que lo necesitaremos al crear nuevamente la partición.
Se da por sentado que el tamaño a disminuir del disco es inferior al tamaña libre disponible.
En este punto, si montamos nuevamente la partición debería tener el mismo contenido pero con diferente tamaño!!!
Este es nuestro archivo original:
Este es nuestro archivo modificando el UUID de la partición swap (con el comando blkid /dev/sdb2 podemos obtener el UUID):
Dispongo de un servidor Debian con un solo disco duro mecánico, y he tenido que añadir un segundo disco duro SSD para poner ahí una base de datos mongoDB de mas de 100Gb.
Algunas consultas en esta base de datos, necesitan el uso de memoria swap ya que la maquina no dispone de mucha memoria, pero al utilizar la memoria swap del disco mecánico, la velocidad disminuía bastante, por lo que hemos decidido modificar el disco SSD para añadir ahí la partición swap sin perder los datos.
Este es el disco inicialmente que queremos rebajarla unos 16Gb a la partición para poner ahí la swap:
1
2
3
4
5
6
7
8
9
Disk /dev/sdb: 232.9 GiB, 250059350016 bytes, 488397168 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: dos
Disk identifier: 0x10cbc63f
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 488397167 488395120 232.9G 83 Linux
Es importante fijarse en el inicio del disco, ya que lo necesitaremos al crear nuevamente la partición.
Se da por sentado que el tamaño a disminuir del disco es inferior al tamaña libre disponible.
1.- Primeramente desmontamos el disco:
1
# umount /dev/sdb1
2.- Verificamos el sistema de ficheros:
1
2
3
4
5
6
7
8
# e2fsck -f /dev/sdb1
e2fsck 1.42.12 (29-Aug-2014)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdb1: 80/15269888 files (61.3% non-contiguous), 14669121/61049390 blocks
3.- Modificamos el tamaño del disco con resize2fs:
1
2
3
4
# resize2fs /dev/sdb1 216G
resize2fs 1.42.12 (29-Aug-2014)
Resizing the filesystem on /dev/sdb1 to 56623104 (4k) blocks.
The filesystem on /dev/sdb1 is now 56623104 (4k) blocks long.
4.- Modificamos la partición con fdisk. Hay que eliminar la paritción y volver a crearla con el mismo inicio y tamaño definido en el resize2fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.25.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): p <-- mostramos la partición actual
Disk /dev/sdb: 232.9 GiB, 250059350016 bytes, 488397168 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: dos
Disk identifier: 0x10cbc63f
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 488397167 488395120 232.9G 83 Linux
Command (m for help): d <-- eliminamos la particion
Selected partition 1
Partition 1 has been deleted.
Command (m for help): n <-- creamos nuevamente la partición
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-488397167, default 2048): <-- IMPORTANTE: tiene que tener el mismo punto de inicio
Last sector, +sectors or +size{K,M,G,T,P} (2048-488397167, default 488397167): +216G <-- IMPORTANTE: indicamos el mismo tamaño que en resize2fs
Created a new partition 1 of type 'Linux' and of size 216 GiB.
Command (m for help): p <-- mostramos la partición modificada
Disk /dev/sdb: 232.9 GiB, 250059350016 bytes, 488397168 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: dos
Disk identifier: 0x10cbc63f
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 452986879 452984832 216G 83 Linux
Command (m for help): w <-- guardamos los cambios
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
En este punto, si montamos nuevamente la partición debería tener el mismo contenido pero con diferente tamaño!!!
5.- Creamos la nueva partición con fdisk nuevamente
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.25.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): p <-- mostramos la particion
Disk /dev/sdb: 232.9 GiB, 250059350016 bytes, 488397168 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: dos
Disk identifier: 0x10cbc63f
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 452986879 452984832 216G 83 Linux
Command (m for help): n <-- Creamos una nueva particion
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2):
First sector (452986880-488397167, default 452986880): <-- No indicamos ni punto de inicio ni final, ya que deseamos el resto de disco
Last sector, +sectors or +size{K,M,G,T,P} (452986880-488397167, default 488397167):
Created a new partition 2 of type 'Linux' and of size 16.9 GiB.
Command (m for help): p <-- mostramos las particiones
Disk /dev/sdb: 232.9 GiB, 250059350016 bytes, 488397168 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: dos
Disk identifier: 0x10cbc63f
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 452986879 452984832 216G 83 Linux
/dev/sdb2 452986880 488397167 35410288 16.9G 83 Linux <-- aquí la nueva particion
Command (m for help): t <-- cambiamos el tipo de partición para indicar que es 82 (swap)
Partition number (1,2, default 2):
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'.
Command (m for help): p
Disk /dev/sdb: 232.9 GiB, 250059350016 bytes, 488397168 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: dos
Disk identifier: 0x10cbc63f
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 452986879 452984832 216G 83 Linux
/dev/sdb2 452986880 488397167 35410288 16.9G 82 Linux swap / Solaris <-- ya tenemos la particion swap
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
6.- le damos formato a la nueva partición
1
2
3
# mkswap /dev/sdb2
Setting up swapspace version 1, size = 17705140 KiB
no label, UUID=bff6fab0-b446-4ef3-b7d0-aec2c169ebe7
7.- modificamos el archivo /etc/fstab para Indicar cual es la partición swap que tiene que utilizar el sistema
Este es nuestro archivo original:
1
2
3
4
5
6
7
8
9
10
11
12
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda1 during installation
UUID=f9d90154-5bad-4469-bc46-130ac56fbdfd / ext4 errors=remount-ro 0 1
# swap was on /dev/sda5 during installation
UUID=242b0a6b-cbeb-4bd9-8be5-f7d054d6ecae none swap sw 0 0
UUID=280837a6-8607-4c8b-bd0b-cf0eb8322834 /mongoData ext4 errors=remount-ro 0 1
Este es nuestro archivo modificando el UUID de la partición swap (con el comando blkid /dev/sdb2 podemos obtener el UUID):
1
2
3
4
5
6
7
8
9
10
11
12
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda1 during installation
UUID=f9d90154-5bad-4469-bc46-130ac56fbdfd / ext4 errors=remount-ro 0 1
# swap was on /dev/sda5 during installation
UUID=bff6fab0-b446-4ef3-b7d0-aec2c169ebe7 none swap sw 0 0
UUID=280837a6-8607-4c8b-bd0b-cf0eb8322834 /mongoData ext4 errors=remount-ro 0 1
8.- Reiniciamos el ordenador y ya estaremos utilizando la nueva partición swap
1
# reboot
Comentarios... (0)
No hay comentarios