Configure Local Storage Flashcards
/dev
All system devices are located in /dev
To create a new partition using fdisk
fdisk /dev/sdb
- use m -> for help
- use p -> to print
- use n -> to create a partition
- use L -> to view different file system we can use
- use w -> to write changes
partprobe command
If you don't see the partition that was just created, you can reboot the system or use the partprobe command # partprobe Which tells the kernel to re-read all partition
Parted command
parted
(parted) print all or type # parted -l
(parted) select /dev/sdb
(parted) mktable gpt create a gpt partition tables
- to create a primary partition with the ext4 type(from 1 mb to 400GB)
(parted) mkpart primary ext4 1MB 400GB
(parted) rm 2 remove a partition - to use all the remaining space, use -1 as end position
- If at a later stage, after creating a partition, you want to change the type of partition, don’t drop and recreate the partition. Format the partition as you want and parted will normally detect the new type
Example of a swap partition with 2GB
(parted) mkpart primary linux-swap 400GB 402GB
To set the first partition as bootable in parted
(parted) set 1 boot on
type set 1 boot off to remove the bootable flag
parted remove a partition
(parted) rm 2
LVM
Logical Volume Manager. It is the default volume management system
- Uses a collection of disks
- a single volue can span multiple disks
- not all disks need to be the same size
- each disk is referred to as a ‘Physical Volume’ (PV)
- physical volumes are collected in to ‘volume groups’ (VG)
- A volume group is split into ‘logical volumes’ (LV)
- logical volumes contain the file systems
- LVM allows for online resizing, reduces system downtime
- PV’s and LV’s are broken up into chunks of data, called extents
- Logical volumes can be grown or shrunk by either increasing or decreasing the extents used, or by increasing or decreasing the amount of disk space used (MG, GB, etc)
- LVM provides the ability to create volume backups through snapshots(no need for filesystem downtime)
- the /boot partition cannot be on a logical volume, grub cannot real LVM volumes
- Volume groups are located at /dev/mapper command line and GUI tools (System-config-lvm)
Creating Logical volume
First step is to convert any disk or partition into physical volumes
# fdisk /dev/sdb
- press the t key to change the partition’s type, then press enter
command(m for help): t
- next type (L) to view the different hex code, then press enter
command(m for help): L
- The one for LVM is 8e, so enter 8e and press enter
hex code(type L to list codes): 8e
-type w to save
command (m for help): w
-step 2 create a physical volume: # pvcreate /dev/sdb1 - to check on the physical volume: # pvs - to view more details: #pvdisplay
- step 3 lump volumes into a volume group # vgcreate vg_new /dev/sdb1 /dev/sdc1 - to check on the volume group # vgs # vgdisplay
- step 4 create logical volume
# lvcreate -L 800M -n lv_new vg_new - to verify
# lvs
To grow a logical volume
lvresize -L 900 MB /dev/vg_new/lv_new
to shrink the logical volume
# resize2fs /dev/vg_new/lv_new 800MB # lvresize -L 800MB /dev/vg_new/lv_new
to remove a volume from a volume group
pvremove /dev/sdb1
if the volume doesn’t exist, you can create it and add physical volumes in one shot
vgextend myvolgroup /dev/sdc
to assign a new physical volume to an existing volume
vgextend myvolgroup /dev/sdd
to remove a physical volume from a volume group
vgreduce myVolGroup /dev/sdc
to delete logical volumes
lvremove /dev/myVolGroup/vol0
you can scan for block devices that can be used as physical volumes
lvmdiskscan
pvscan command
scans all supported LVM block devices in the system for physical volumes. It shows all physical devices found #pvscan
The following command disallows the allocation of physical extents on /dev/sdc1
# pvchange -x n /dev/sdc1 you can use the -xy arguments of the pvchange command to allow allocation where it had previously been disallowed
to resize a physical volume
you should use the pvresize command
To change the maximum number of logical volumes of volume group vg00 to 128
vgchange -l 128 /dev/vg00
The following will deactivate the volume group my-volume-group
vgchange -a n my-volume-group volume group are activated by default
To remove a volume group that contains no logical volumes
vgremove officeVG
to split the physical volumes of a volume group and create a new volume group
use vgsplit command. Logical volumes cannot be split between volume groups # vgsplit bigvg smallvg /dev/ram15
to combine volume groups
vgmerge -v databases my-vs
renaming an existing volume group vg02 to my_volume_group
# vgrename /dev/vg02 /dev/my_volume_group # vgrename vg02 my_volume_group
Creating a striped logical volume across 2 physical volumes with a stripe of 64 KB
lvcreate -L 50G -i2 -I64 -n gfslv vg0 gfslv is the name of LV, while vg0 is the name of the VG
An example where the stripe will use sectors 0-50 of /dev/sda1 and sectors 50-100 of /dev/sdb1
lvcreate -l 100 -i2 -nstripelv testvg /dev/sda1:0-50 /dev/sdb1:50-100
to create a mirror volume
lvcreate -L 50G -ml -n mirrorlv vg0
steps to create a snapshot of a logical volume
mkdir /mnt snapshot directory to mount the snapshot
# lvcreate -L 100M -s -n lvmsnapshot /dev/vg-new/lv-new creates the snapshot
# lvs to view the snapshot created
to restore data from a snapshot, you need to first mount it
# mount -t ext4 /dev/vg-new/lvsnapshot /mnt/snapshot
Then you can copy the data back rom /mnt/snapshot
to remove a snapshot
# umount /mnt/snapshot # lvremove /dev/vg-new/mvsnapshot
GUI to manage LVM
System-config-lvm not installed by default
# yum install system-config-lvm
to access you can call from the terminal or from the GUI
System - Administration - logical volume Management
to find the UUID of a device
#blkid you can also use this command # dump2fs /dev/sdb1 | grep UUID
add UUID to /etc/fstab
# vi /etc/fstab add entry similar UUID=[strings of numbers & letters obtained] /mydata ext4 defaults 1 2
To label a filesystem
#e2label /dev/mapper/data luksdrive then you can add entry to /etc/fstab LABEL=luksdrive /mydata ext4 defaults 1 2
You can also assign a label to a disk with this command #tune2fs -L test /dev/sdb1
other commands
# mke2fs -L newlabel /dev/hdb1
# mkfs.ext3 -L newlabel /dev/hdb1
- mkswap -L swaplabel /dev/hdbxx
Mounting a specific logical volume by UUID
blkid | grep lv_vol»_space; /etc/fstab
- then edit /etc/fstab
the lvdisplay command also generates the UUID of a logical volume
# lvdisplay logicalVolumeName | grep UUID
manually mounting filesystems can also be done via label
mount -L labelname /mnt/point1
to display all partitions with their corresponding UUID and labels
# blkid # ls -l /dev/disk/by-uuid/ # tree /dev/disk
On the graphical end, you can install gparted # yum install gparted
-adding a partition using fdisk(summarize)
#fdisk /dev/sdb 2 - type p to print 3 - type 1 4 - hit enter twice 5 - type t 6 - type 82 7 - type w
adding a logical volume fdisk(summarize
#fdisk -cu /dev/sdd type p type n type 1 type t type L type 8e type w
to make a swap partition(summarize)
mkswap /dev/sdc1
to activate:
#swapon /dev/sdc1
to check it
#swapon -s
Add it to /etc/fstab #echo "/dev/sdc1 swap swap defaults 0 0" >> /etc/fstab
to remove swap logical volume # swapoff /dev/...
Create and configure LUKS-encrypted partitions and logical volumes to prompt for password and mount a decrypted file system at boot
Install the LUKS package #yum install -y cryptsetup-luks
Activate LUKS module #modprobe dm_crypt
Check the module is running #lsmod | grep dm_crypt
Create a logical volume(here called lv_vol with a size of 100MB in the vg volume group) #lvcreate --size 100M --name -v_vol vg
Convert the new logical volume to the LUKS format: #cryptsetup luksFormat /dev/vg/lv_vol
Open and give a name to the LUKS logical volume (here vol): #cryptsetup luksOpen /dev/vg/lv_vol vol
Create an EXT4 file system on the LUKS logical volume #mkfs.ext4 /dev/mapper/vol
Create the /etc/crypttab file and add the following line:
vol /dev/vg/lv_vol /root/luks.key
Note: if you put ‘none’ instead of ‘/root/luks.key’, you will be asked for the passphrase at each boot at the console
Create the /root/luks.key file for example to store the passphrase:
mypassphrase
Add the passphrase to the LUKS logical volume #cryptsetup luksAddKey /vdev/vg/lv_vol /root/luks.key
edit /etc/fstab file and add the following line (be careful when editing the /etc/fstab file or configure a virtual console):
/dev/mapper/vol /vol ext4 defaults 1 2
Create the mount point #mkdir /vol
Mount the LUKS logical volume #mount /vol
To unmount a LUKS-encrypted file system
Unmount a LUKS-encryted file system by its mounting point in the /etc/fstab file (here /vol), #unmount /vol
To unmount a LUKS-encrypted file system by its LUKS name (here vol), type: #umount /dev/mapper/vol
To remove a LUKS-encrypted file system, follow:
unmount the LUKS file system (here /vol):
#umount /vol
Close the LUKS logical volume: #cryptsetup luksClose /dev/mapper/vol
Remove the logical volume #lvremove /dev/vg/lv_vol
Remove the file where the passphrase is store #rm /root/luks.key
Edit the /etc/crypttab and /etc/fstab files and remove the entries associated with the LUKS logical volume
Configure Systems to mount file systems at boot by Universally Unique ID (UUID) or label
Find the UUID of a devide
#blkid
once found enter in /etc/fstab
UUID=f8b69….. /mynew_data ext4 defaults 1 2
Then mount #mount -a
To mount using a label, first you need to create the label #e2label /dev/mapper/mynew_data luksdrive
Then add to /etc/fstab
LABEL=luksdrive /mynew_data ext4 defaults 1 2