· 6 years ago · Mar 17, 2019, 03:26 PM
1Introduction
2Today we focus on a little user management, disk, volume, and partition management.
3Review the partitioning and filesystem slides1 partitions and filesystems that we covered in class. You might also want to read through this a useful reference to LVM by polluck2.
4Adding Users
5Our first task is to create several user accounts in our linux machine. Use the appropriate commands to do this. Once you have create the accounts, check /etc/passwd, /etc/shadow and /etc/groups to check if the accounts are created. Also check to see if their home directories are created. This should be under /home/*.Our next goal is to add a new hard disk, and make it as our new /home partition.
6Adding a home filesystem. Let's start by exploring. If you type
7adduser username
8Passwd username
9
10$ df
11
12
13at the shell, you'll see some filesystems mounted on your system. The ones that correspond to parts of your hard drive are
14/
15
16
17 (the root filesystem to which other filesystems are mounted) and
18/boot
19
20
21(the filesystem that contains grub and other booting-specific files).
22To see how things are allocated at a lower level, as root, run
23$ fdisk
24
25
26 (with the correct argument). See how your drive has one partition for booting, and one additional partition for LVM. Right now all space on your hard drive has been allocated. So our initial goal is to add a new hard disk on virtualbox. To do this, turn off your linux instance and go under settings to add a new harddisk with size 500MB. Now as root, you can mount the new /home filesystem if u wanted, but we will implement RAID over LVM on it first. Beforehand, run
27ls -l /home
28
29
30to see your home directory there. Notice you have some files here pertaining to the accounts you have created. Copy the files in /home using the command
31$ cp -i
32
33
34cp -i command into a free directory like /tmp or /root. Find out what the -i argument do for you.Then run
35$ umount /home
36
37
38(note you can't umount /home, if /home is not a partition. If this is the case, ignore the umount /home part)
39Software RAID
40Now that we have added the new disk. Your linux box will see this new disk as /dev/sdb device. Verify this in your /dev folder. Use fdisk to create a new partition on the hard disk. Create a new LVM partition. Make sure to change the partition type to LVM. Once done, you'll notice that you have /dev/sdb1 and fdisk -l will show the new partition.
41Now since we have a new partition, we need to add that partition we created as a LVM physical volume. To do this, run the pvdisplay command and see what type of physical volumes on LVM you currently have. Then create a new physical volume with
42$ pvcreate /dev/sdb1
43
44
45Run pvdisplay again and observe your physical volumes. We can now add this partition in the exisiting volume group. Use
46$ vgcreate vgcentos2 /dev/sdb1
47
48
49But if you run lvdisplay, you'll notice we only have 2 logical partitions, which we are currently using for the root / directory and swap.
50Now that we have available on the newly create volumegroup, we can create some more logical volumes that have some value. Let's start by exploring RAID. In particular, let's use software RAID3 to create a new filesystem. RAID 1 (mirroring) or RAID 5 (n+1) can be applied to a set of equal-sized drive partitions (below LVM) or logical volumes (above LVM). Let's build our RAID on top of LVM. The RAID-specific steps will be the same if we were using partitions across multiple drives. Some references on using LVM 4 as tutorials are available on the net.
51The first step is to create some new logical volumes, each 100MB in size. Our goal is to provide a 200MB RAID-5 volume for /home. Type
52$ lvcreate -L100M -n lv_home01 vgcentos2
53
54
55 (assuming the volume group on your machine is called vgcentos2). Do the same for two more (e.g., lv_home02, lv_home03). Use lvs to see that you have created three new logical volumes. (Note that you can use lvremove if you make a mistake in this process.)
56Now let's establish our software RAID device. Before you can do that, make sure you have mdadm installed. Use yum for this. Type
57mdadm --create /dev/md0 --auto=yes --level=raid5 -n 3 /dev/mapper/X1 /dev/mapper/X2 /dev/mapper/X3
58
59
60where you replace X1..X3 with the names of the newly created logical volumes (found in /dev/mapper). This tells mdadm to create a new raid 5 device at /dev/md0 with the three specific volumes as the underlying sources, and when finished, lets us access the combined device as /dev/md0.
61We can now place a filesystem on it, using mke2fs (or mkfs). Make sure you create an ext4 filesystem. Once that is in place, try mounting it at /mnt and running df. Look at /proc/mdstat to see the status of the RAID filesystem. If it shows useful information about RAID (e.g., active raid5, etc.), then it is functioning---the OS is performing software RAID and making it visible via /dev/md0. (Notice that you can use the filesystem even when the RAID devices are not yet consistent.) You can also use the command mdadm --detail /dev/md0. Now move all the /home content into this /mnt partition.
62RHEL/CentOS assumes that RAID is only started prior to LVM. In this case, we are creating a RAID that exists on top of LVM, so it must be started afterwards. You'll need to edit the /etc/rc.d/rc.sysinit file -- move the seven lines for lvm to be prior to the MD RAID block right above it. This is a good place to reboot to make sure that the RAID shows up functioning again (check /proc/mdstat after boot). If it does, you can mount it again to /mnt.
63Finally, to make this permanent unmount the filesystem from /mnt and mount it permanently on /home. To finish, add and remove the appropriate entries in /etc/fstab and reboot to make sure it works.
64Additional swap
65Check to see what is your initially set up of swap space. (Use top to see this.) Our task at this point is to create a new swap partition.
66Use mkswap to establish logical volume as a swap file (this is very fast). Now use top to see how much swap you have. Although the filesystem is ready, the OS hasn't yet been told to use it. Edit /etc/fstab to include the new swap partition, then use swapon -a to turn it on.
67Run top once again to see that you now have more of swap.
68Resizing a volume
69(Warning: this part is particularly dangerous to your filesystem. Double check your work before you do something that requires you to reinstall your OS from scratch!)
70At this point your /home filesystem is much smaller than when we started. (Again, you can use df and lvs to see the filesystems and logical volumes known to your system.) If you add the space used by the logical volumes shown by lvs and compare to the size of the partition used by LVM, you will find that there is quite a bit of space unused. However, if you use pvs to see the information about your physical volumes, you'll see that your LVM partition is listed as almost the whole drive. This means you cannot create new partitions (as there is no space on your drive). You can only create logical volumes to utilize available space within the Volume Group already defined.
71Sometimes it is useful to be able to create partitions directly, skipping LVM volumes entirely. Our final goal in this lab will be to shrink the physical volume so that you could create new partitions on your drive. Unfortunately, the pvresize command will not shrink a physical volume that has extents allocated in areas that will no longer be covered by a smaller volume.
72So your first step will be to find out whether the allocated extents of your physical volume are indeed allocated outside of the minimum needed. As you may recall from your readings about LVM, a physical volume is comprised of physical extents (portions of a physical volume that can be allocated to a particular volume group).
73As we saw above with pvs, there was lots of space free on your only volume group. Since LVM could use any extent (it is not required to use only the lowest-ordered extents), we must first find out where our logical volumes have been allocated (that is, which physical extents are being used).
74We can find out what LVM is doing by telling it to create a backup LVM volume group configuration. Run
75/sbin/vgcfgbackup
76
77
78and it will create the file /etc/lvm/backup/vg_sandbox with human-readable contents.
79Examine this file. The physical_volumes entry will show you the range of extents used (pe_start and pe_count). Each logical volume has a starting extent (specified by "pv0" within stripes) and an extent_count saying how large it is. What you want to see is that all of the physical extents used by your logical volumes are packed together at the beginning of the partition. If that is the case, it means that we can truncate this partition without harm.
80If we had physical extents in use in the area we wish to free (at the end) that correspond to volumes containing data, we would need to move that data to an earlier available extent. The command to do this is pvmove. Use it if needed to pack your physical extents all at the beginning of the partition. And then re-run vgcfgbackup to update your backup file, which you can examine to verify that it is now correct.
81Next is to actually change the size of the physical volume. While we could modify the volume backup file and tell the system about the revised file with /sbin/vgcfgrestore, lvs also has a command that does exactly what we want: pvresize. So, look at its short man page, and figure out how to use 100GB less storage from your drive.
82Once you've done so, LVM is now convinced that its storage allocation has changed but the disk partitioning does not yet reflect it. Run fdisk again to see that the partitions have not changed.
83What we need is to resize the /dev/sdb1 partition. We can still use parted to accomplish our goal. Run parted on the drive. At the parted prompt, type print to list the partitions that it sees. We are going to replace the existing sdb1 partition with a smaller one. Use the rm command to delete the 2nd partition. Then use mkpart primary fat32 (not mkpartfs) to create a new one.
84Note that mkpartfs would install a filesystem on the new partition (overwriting our existing filesystem!). Check the revised partition table with print, and notice that the new (smaller) partition does not have the right filesystem type. The one we removed had the lvm flag. Now use the set command to set the lvm flag. That's it! You can now quit. While parted does not require rebooting after these changes, you might want to do so to verify that everything is still functional.
85Wrapping Up
86Submit a youtube link to a video capture of what you have done in this lab In order to show your mastery in this subject. You can run the video as a tutorial or as a demonstration. But mainly you have to show that :
87show me /etc/passwd and /etc/groups for users you've created
88show me /etc/fstab and the output of /proc/mdstat and df to demonstrate the use of RAID
89show me top demonstrating the activation of your additional swap space
90show me the (correct) output of lvs, pvs, and fdisk
91show me that you've successfully transfered the old /home contents to the new lvm created partition after mounting it as the new /home
92
93http://italeem.iium.edu.my/notes/05-Disks.pdf ↩︎
94http://www.hccfl.edu/pollock/AUnix1/LVM.htm ↩︎
95http://italeem.iium.edu.my/notes/05-Disks.pdf ↩︎
96https://www.howtoforge.com/linux_lvm ↩︎
97Install mdadm