Diferencia entre revisiones de «File systems»
(→Automatic mounting at system startup =) |
|||
Línea 194: | Línea 194: | ||
− | == Automatic mounting at system startup | + | == Automatic mounting at system startup == |
Now we will see how to automate the mounting process every time the system is started, since it would be tedious to do it. | Now we will see how to automate the mounting process every time the system is started, since it would be tedious to do it. |
Revisión del 16:26 30 nov 2021
Contenido
- 1 Step 1: Adding a disk to the virtual machine
- 2 Step 2: View disks in Linux
- 3 Step 3: Creating Linux partitions
- 4 Step 4: Format partition: creating a file system
- 5 Step 5: Mounting and unmounting partitions in Linux
- 6 Step 6: Creation of NFS network file systems
- 7 Step 8: Managing volumes (Logic Volume Manager, LVM)
- 8 Step 9: Resize virtual disk size of Ubuntu cloud image already imported into libvirt
Step 1: Adding a disk to the virtual machine
We are going to use any ubuntu cloud virtual machine that we have used previously and we are going to add two previously and we are going to add two virtual disks for testing.
- We open the window of the virtual machine to use.
- Move to View -> Details, and click on the 'Add hardware' button.
- We select Storage, we give it a size of 5GB and in the device type we select 'disk device'. With these options, we click Finish, and we will have our disk created.
- Repeat the previous step and create another 4GB disk.
Step 2: View disks in Linux
Let's go back to the console view (View -> Console) and start the machine (Virtual machine -> Run).
To check the disks added in step 1 let's use the lsblk command, which will show us an output similar to the following:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 252:0 0 2.2G 0 disk
├─vda1 252:1 0 2.1G 0 part /
├─vda14 252:14 0 4M 0 part
└─vda15 252:15 0 106M 0 part /boot/efi
vdb 252:16 0 5G 0 disk
vdc 252:32 0 4G 0 disk
Here we see that we have 3 disks:
- vda: the current ubuntu disk we added when we created the machine.
- vdb: the disk we added in step 1 of 5GB.
- vdc: the disk that we added in step 1 of 4GB
We can also see that the vda disk has 3 partitions: vda1, vda14 and vda15
Step 3: Creating Linux partitions
We are now going to use our 5GB added disk, in the previous step we should have identified which one it is, in my case /dev/vdb, be sure which one is yours is yours and we will start partitioning the disk:
sudo fdisk /dev/vdb
If the disk is new and you haven't done anything previously, it usually comes without a partition table, so fdisk /dev/vdb </syntaxhighlight>. partition table, so fdisk takes care of creating one. message when we have executed the previous command. The partition table is a small part of the disk that is used to store the partition information. partition information, the format and whether a partition is executable or not. executable or not.
Once this is done, we will see that we are inside fdisk (software to partition a disk), and we will see that we are inside fdisk (software to partition a disk). partitioning a disk), and we will see that it has its own command line.
Now we will see the fdisk help and we will create a 3GB test partition
- Enter the letter 'm' and press Enter to get the list of possible fdisk commands.
- Enter 'n' and press Enter to create a new partition. You will be asked for several details:
- Partition type: press Enter and it will be assigned primary by default.
- Partition number: press Enter and it will be set to 1 by default.
- First sector: press Enter and it will be assigned by default.
- Last sector: we are going to create a 3GB partition, so we type +3G and press Enter.
- Our first partition is already created, although the changes have not yet been written to disk, for that, we will need to apply these changes, and we do it with the command 'w' and pressing Enter.
- Now we have our partition created. Let's check that the change is done, using lsblk for example, we should get something similar to the following:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 252:0 0 2.2G 0 disk
├─vda1 252:1 0 2.1G 0 part /
├─vda14 252:14 0 4M 0 part
└─vda15 252:15 0 106M 0 part /boot/efi
vdb 252:16 0 5G 0 disk
└─vdb1 252:17 0 3G 0 part
vdc 252:32 0 4G 0 disk
Let's now create on the 4GB disk (vdc in my case) two partitions of 2GB each, and the 3GB partition created earlier, let's delete it:
- sudo fdisk /dev/vdc
- command 'n' and Enter
- All data by default, except the last sector, where we will put '+2G'.
- Once the first partition is created, before writing to disk, we are going to create the second partition, we repeat the previous steps. We are going to have a problem and when we get to the last step, it will tell us that 2GB is not possible. If we have a 4GB disk, why doesn't it allow us to create two 2GB partitions? Exactly, because of the partition table. What we will do in the last step will be to leave the default value, which will be the entire spare disk.
- Let's check before writing the changes, that everything is OK, command 'p' and Enter should show us an output similar to:
Device Boot Start End Sectors Size Id Type
/dev/vdc1 2048 4196351 4194304 2G 83 Linux
/dev/vdc2 4196352 4196351 4194304 2G 83 Linux
- If everything is correct, press 'w' and Enter and apply the changes.
- Now we will enter with the /dev/vdb disk to delete the partition: sudo fdisk /dev/vdb
- Now we are going to eliminate the partition, we press 'd' and Enter. As we only have one partition, it deletes it directly, in case we have more than one, it will ask us which one we want to delete.
- We apply the changes: 'w' and Enter.
- We check that everything has been to our liking with the command lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 252:0 0 2.2G 0 disk
├─vda1 252:1 0 2.1G 0 part /
├─vda14 252:14 0 4M 0 part
└─vda15 252:15 0 106M 0 part /boot/efi
vdb 252:16 0 5G 0 disk
vdc 252:32 0 4G 0 disk
├─vdc1 252:33 0 2G 0 part
└─vdc2 252:34 0 2G 0 part
Step 4: Format partition: creating a file system
So far we have only defined in the partition table where each partition starts and ends. each partition begins and ends, but we will not be able to use it until we create a file system on the partition. file system on the partition. To create a file system we will use the command 'mkfs', and we will have several options when creating a file system one: ext2, ext3, ext4, btrfs, fat, ntfs, etc...
We create an ext4 system for /dev/vdc1 and a fat one for /dev/vdc2:
sudo mkfs -t ext4 /dev/vdc1
sudo mkfs -t fat /dev/vdc2
Once this is done, let's check that the changes have been applied properly:
sudo file -s /dev/vdc1
sudo file -s /dev/vdc2
In the output we should get that we have an ext4 file system and a FAT file system. another FAT.
Step 5: Mounting and unmounting partitions in Linux
After having created in the previous steps some partitions and formatting them, we are going to now we are going to mount them to be able to use them. In linux mounting a partition will mean that we will assign a system folder to a partition, and in it will be all the contents of the disk. will contain all the contents of the disk.
Mount and umount
We will start by using the mount and umount commands to mount and unmount partitions. partitions. We are going to mount the partitions /dev/vdc1 and /dev/vdc2, the first one we will mount in /home/ubunt1 and /dev/vdc2, the first one in /home/ubunt2. we will mount it in /home/ubuntu/part1 and the second one in /home/ubuntu/part2:
mkdir /home/ubuntu/part1 # We create the folder where we are going to mount the partition.
sudo mount /dev/vdc1 /home/ubuntu/part1 # mount partition
The same with /dev/vdc2
mkdir /home/ubuntu/part2
sudo mount /dev/vdc2 /home/ubuntu/part2
Let's see the contents of the mounted partitions:
ls /home/ubuntu/part1
ls /home/ubuntu/part2
We will see that we have a lost+found folder in the ext4 system, which is used for system errors. used for file system errors, when there is an error and we find an unreferenced file, it is we find an unreferenced file, it would be added inside this folder, and there would be the possibility to recover it.
Now we are going to create a new file inside our /dev/vdc1 partition, we are going to unmount it, and we are going to dismount it. we are going to unmount it, and we are going to mount it in a different directory:
sudo touch /home/ubuntu/part1/part1/newFile # we have to create it with sudo because we don't have permissions, then we will see this.
sudo umount /dev/vdc1
ls /home/ubuntu/part1 # the file is gone, the partition was unmounted
mkdir /home/ubuntu/part3
sudo mount /dev/vdc1 /home/ubuntu/part3
ls /home/ubuntu/part3
We will check that the file created in /home/ubuntu/part1 is now located in /home/ubuntu/part3, since actually, when we saved it, it was in the partition we mounted. partition we mounted.
We can also see that using the lsblk command, we will be able to observe where the partition is mounted. the partition is mounted.
Automatic mounting at system startup
Now we will see how to automate the mounting process every time the system is started, since it would be tedious to do it. system, since it would be tedious to have to mount all the partitions every time we turn off and turn on our machine. it would be tedious to have to mount all the partitions every time we turn off and on our machine.
The automated process is usually done inside the /etc/fstab file, let's look at. its contents:
cat /etc/fstab
Each row contains a mount of a partition, which contains:
- Partition ID: in this case a partition label is being used, but we can use anything that identifies the device, such as uuid or location (/dev/vdc1).
- Mount point: where the device will be mounted.
- File system: ext4, fat
- Options: here we will put the different mounting options, such as mount for read-only, give permissions to a user to use the partition, etc. Keep in mind that each system has its own options.
- backup: if it is set to zero, no backup will be made.
- check: if it is set to zero, no check is made at startup.
We are going to add some lines to automatically mount the partitions at startup:
Using the editor of your choice, add the following to the file /etc/fstab. Important to do it with sudo so that it allows us to write to the file:
/dev/vdc1 /home/ubuntu/part1 ext4 rw,user,exec 0 0
/dev/vdc2 /home/ubuntu/part2 vfat umask=000 0 0 0
Once the changes have been saved, let's apply them without rebooting, to test that it works correctly:
mount -a # apply the changes to fstab without reboot
lsblk # check that it is properly mounted.
Now let's check if our user has write permissions:
touch /home/ubuntu/part1
touch /home/ubuntu/part2
In the first one it will not let us and in the second one we will not have problems. This works this way because the ext4 systems, in order to have write permissions, we have to give it on the file system, while the fat system, in order to have write permissions, we have to give it on the file system, while the fat system has the umask option that already does the job, has the umask option that already does the job. To have write permissions with our user in the partition, we will have to give permission to the partition to be able to write to it, for example, the to be able to write to it, for example, using the chown command:
sudo chown ubuntu /home/ubuntu/part1
If we try it now, we will be able to write to the ext4 partition, and having given permissions, it will work every time:
touch /home/ubuntu/part1
Now, let's unmount everything and reboot the machine to check that it's all working.
sudo umount /dev/vdc1
sudo umount /home/ubuntu/part2 # This is another way to unmount, giving the mount point.
lsblk # check that they are not mounted.
We restart the machine (Virtual machine -> Shutdown -> Restart) and check:
lsblk
Step 6: Creation of NFS network file systems
In this step, we are going to create a shared network file system via NFS. For this we will need to create a server where we will start this system to which the clients will connect.
Step 6.1: Installation and configuration of the NFS server
To configure the server, we will need to open one of the virtual machines created earlier and install the necessary tool:
sudo apt install nfs-kernel-server
Once this is done, let's create a directory which will be the one we will share, for example:
sudo mkdir /home/shared
Now we are going to edit the nfs server, for this, we will have to edit the /etc/exports file, but first, we will see which is our IP, which we will need:
ip address
The output of this command will be similar to the following:
enp1s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.167 netmask 255.255.255.0 broadcast 192.168.0.255
inet6 fe80::ea6a:64ff:fe66:66a0 prefixlen 64 scopeid 0x20<link>
ether e8:6a:64:66:66:66:a0 txqueuelen 1000 (Ethernet)
RX packets 3821 bytes 2152349 (2.0 MiB)
RX errors 0 dropped 268 overruns 0 frame 0
TX packets 3386 bytes 266855 (260.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 16 memory 0xa4300000-a4320000
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 220506 bytes 16017419 (15.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 220506 bytes 16017419 (15.2 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
We look where it says inet, and look at the network that is NOT 'lo', in this case, the IP sought would be 192.168.0.167. Now yes, let's edit the /etc/exports:
/home/shared 192.168.0.*(rw,sync,subtree_check)
We explain the parameters:
- Folder to share.
- We give the IP with a * at the end to allow all networks to access, from 192.168.0.0.0 to 192.168.0.255.
- Here we have many options available:
- ro: read only
- rw: read and write
- subtree_check: check subdirectories to share them too
- sync: force to do the operations synchronously (the data is immediately written to the NFS server disk).
Once the file has been modified, let's restart the NFS service:
sudo service nfs-kernel-server restart
Once this is done, our NFS server is now configured.
To check that the NFS server is operational, we can use the ss command:
ss -lt
And a line referring to nfs should appear.
== Step 6.2: Installing and configuring the NFS client == Step 6.2: Installing and configuring the NFS client == Step 6.3.
To access the NFS file system created in the previous step, we will need to configure the client to mount the NFS file system. To access the NFS file system created in the previous step, we will need to configure the client to mount the shared file system. The client will be mounted on a different virtual machine.
We create and open this new machine and install a tool to be able to mount the NFS on our machine: == We create and open this new machine and install a tool to be able to mount the NFS on our machine:
sudo apt install nfs-common
With this we will be able to mount on our client the NFS. Let's remember the IP that we obtained in the previous step when we mounted the server, and we do the following following:
sudo mount 192.168.0.167:/home/shared /mnt
Now we have our network shared file system mounted, let's test that everything works. test that everything works.
1. On the machine where we installed the NFS server, we are going to create a folder and a file inside the shared folder, for them we will need to give permissions to our user previously:
sudo chown -R ubuntu /home/shared
mkdir /home/shared/folder
touch /home/shared/folder/folder/file
2. Let's check on the client, that these files are being displayed:
ls /mnt
ls /mnt/folder
3. Now we do it the other way around, we will write from the client, and we are going to see it on the server:
mkdir /mnt/folder2
touch /mnt/folder2/file
4. We log into the server and check:
ls /home/shared
ls /home/shared/shared/folder2
Step 8: Managing volumes (Logic Volume Manager, LVM)
Logic Volume Manager (LVM) is a software layer that allows you to create logical volumes and easily map them onto physical devices.
The installation of LVM is easy with the command:
sudo apt-get install lvm2
LVM management is based on three basic concepts:
- Physical Volumes (PV): represents a storage unit that provisions storage space for the logical volume we are going to create.
- Volume Group (VG): Represents a storage pool for LVM. A VG will be composed of several PVs, being able to have as many VGs as needed.
- Logical volume (LV): They represent logical units created from previously created VG. As many LVs can be created as necessary for a VG. The creation of an LVM generates a special file in /dev, in the form /dev/group_name/logical_volume_name. The space mapping from an LV to a PV is configurable and can be: Linear, RAID, Cache, ...
Step 8.1: Creating LVM physical volume (PV)
To list the available storage units in the system, we use the following command:
lsblk
In virtualbox we can create new storage units and add them to the virtual machine.
To create a physical volume on the /dev/sdb drive, we use the following command:
pvcreate /dev/sdb
Remember that the /dev/sdb drive must be unused.
To view the existing physical volumes, we use the command:
pvscan
For more information:
pvdisplay
To remove a PV, for example /dev/sdb:
pvremove /dev/sdb
Step 8.2: Creating LVM volume group (VG) == Step 8.2: Creating LVM volume group (VG) == Step 8.2: Creating LVM volume group (VG) ==
To create a group, we use the vgcreate command:
vgcreate vg_test /dev/sdb /dev/sdc.
This adds the sdb and sdc volumes to the 'vg_test' group, making the group capacity the aggregate capacity of the added PVs.
To remove a vgremove group:
vgremove vg_test
To extend a created group (e.g. vg_test) with more PVs (e.g. /dev/sde) we use the vgextend command:
vgextend vg_test vg_test /dev/sde.
To reduce the capacity of a created group (e.g. vg_test) just use the vgreduce command indicating the unit (PV) to remove, e.g. /dev/sde:
vgreduce vg_test /dev/sde
To display all existing volume groups
vgscan
Step 8.3: Creating a logical volume (LV)
To create a logical volume, we use the command:
lvcreate --name volume1 --size 100MB vg_test
From this point on there is a drive that appears as /dev/mapper/vg_test-volume1.
We can now format the logical volume:
mkfs.ext4 /dev/vg_test/vg_test/volume1
and mount it to store data:
mount /dev/vg_test/volume1 /mnt
You can extend a logical volume by 1 Gbyte more:
lvextend --size +1GB /dev/vg_test/volume1
Right after that you have to resize the file system:
resize2fs /dev/vg_test/test/volume1.
You can check with:
df -h
that the file system in volume1 now occupies the entire logical volume.
Reducing the size of a logical volume is a bit more complicated, you could lose data if not done correctly!
First, we unmount the volume:
umount /mnt/volume1
to reduce the size of the file system, we check the integrity of the file system:
e2fsck -f /dev/vg_test/volume1
And we resize it (reduce in size):
resize2fs /dev/vg_test/volume1 500M
Now, lastly, you can reduce the size of the volume:
lvreduce --size 500M /dev/vg_test/volume1
To check that everything went well, resize the file system again so that it occupies all the available space.
resize2fs /dev/vg_test/volume1
And you can mount the file system again:
mount /dev/vg_test/test/volume1 /mnt/volume1
with
df -h
to buy the new available size of only 500 MBytes.
Step 9: Resize virtual disk size of Ubuntu cloud image already imported into libvirt
With the virtual machine off, from the host:
qemu-img resize ubuntu-18.04-server-cloudimg-arm64.img +8G
To give you 8 GBytes more space.
Now, from the virtual machine, we increase the partition size:
sudo growpart /dev/sda 1
And then we resize the file system:
sudo resize2fs /dev/sda1 +8G