Skip to content

Instantly share code, notes, and snippets.

@SkymeFactor
Last active November 2, 2020 15:35
Show Gist options
  • Save SkymeFactor/728b5a2478f39f2b432385b3cccdfbde to your computer and use it in GitHub Desktop.
Save SkymeFactor/728b5a2478f39f2b432385b3cccdfbde to your computer and use it in GitHub Desktop.
#!/bin/bash
# Version with screenshots and results is available by link:
# https://docs.google.com/document/d/1kExXVpq4d7KO6Nc78bMv8ANoM96yvspQufao17pgTWE/edit?usp=sharing
get_device_id() {
udevadm trigger --subsystem-match=block; udevadm settle
udevadm info --query all $1 | grep by-id | head -1 | awk '{print $2}'
}
# 1.---------------------------------------------------------------------------
# Create an empty partition of size 300 Mb
fdisk /dev/sda << EOF
n # n - for creating a new partition
p
3
+300M
y
w
EOF
# Options in order of input:
#
# p - for primary partition
# 3 - for assigning the 3rd number like sda3 (1st is boot, 2nd is root)
# explicitly nothing - just enter to proceed the first empty block
# +300M - assign the size value of 300 Mb
# w - write changes on disk
# 2.---------------------------------------------------------------------------
# Create a new file with UUID of created partition
blkid --match-tag UUID /dev/sda3 | awk --field-separator '"' '{print $2}' > /root/UUID
# Make an alias to the disk. Contains "disk/by-uuid/UUID" string
device=$(get_device_id "/dev/sda3")
# 3.---------------------------------------------------------------------------
# Create ext4 file-system with 4096 block-size
mkfs --type ext4 -b 4096 /dev/${device} << EOF
y
EOF
# 4.---------------------------------------------------------------------------
# Show info stored within the superblock of created partition
dumpe2fs -h /dev/${device}
# 5.---------------------------------------------------------------------------
# Set up the fs check each second mount
tune2fs -c 2 /dev/${device}
# Set up the fs check every two month
tune2fs -i 2m /dev/${device}
# 6.---------------------------------------------------------------------------
# Create /mnt/newdisk/ and mount /dev/sda3 in there
mkdir /mnt/newdisk
mount --types ext4 /dev/${device} /mnt/newdisk/
# 7.---------------------------------------------------------------------------
# Create a link to mounted folder within /root/
ln --symbolic --directory /mnt/newdisk/ /root/link_to_sda3
# 8.---------------------------------------------------------------------------
# Create a random folder within the mounted fs
mkdir /root/link_to_sda3/random_name
# Don’t forget to put some random executable file into that folder
touch /mnt/newdisk/random_executible.sh
chmod a+x /mnt/newdisk/random_executible.sh
# 9.---------------------------------------------------------------------------
# Automatic mounting with no rights to execute and the last access
# writing disabled
# Put the following line at the end of /etc/fstab:
# /dev/sda3 /mnt/newdisk ext4 defaults,noatime,noexec 0 0
echo "/dev/${device} /mnt/newdisk ext4 defaults,noatime,noexec 0 0" >> /etc/fstab
# 10.--------------------------------------------------------------------------
# Remove the current partition that we’ve created earlier
fdisk /dev/sda << EOF
d
3
n
p
3
+350M
y
w
EOF
unset device
# Make an alias to the disk. Contains "disk/by-uuid/UUID" string
device=$(get_device_id "/dev/sda3")
# Options in order of input:
# d - to delete partition, 3 to specify number 3
# n - to create a new partition,
# p - make it primary,
# Enter - default size,
# +350M - make a larger size
# w - to write changes, n to not restructure the existing data
# Extend our partition to max size
resize2fs /dev/${device}
# 11.--------------------------------------------------------------------------
# Chek fs for errors, do not make any changes
e2fsck -n /dev/${device}
# 12.--------------------------------------------------------------------------
# Create a new partition of size 12Mb, process is identical to paragraph 1
fdisk /dev/sda << EOF
n
p
+12M
y
w
EOF
# Make an alias to the disk. Contains "disk/by-uuid/UUID" string
partprobe
device_slave=$(get_device_id "/dev/sda4")
# Creating journal fs on sda4
mke2fs -O journal_dev -b 4096 /dev/${device_slave} << EOF
y
EOF
# Umount sda3 (mandatory)
umount /dev/${device}
# Disable journal of sda3
tune2fs -O ^has_journal /dev/${device} << EOF
y
EOF
# Enable journaling of sda3 on sda4
tune2fs -o journal_data -j -J device=/dev/${device_slave} /dev/${device} << EOF
y
EOF
# 13.--------------------------------------------------------------------------
# Create two 100Mb partitions. (As far, as there must be at most 4 primary
# partitions, we will also delete the previous two).
fdisk --wipe always /dev/sda << EOF
d
3
d
4
w
EOF
fdisk --wipe-partitions always /dev/sda << EOF
n
p
3
+100M
y
n
p
+100M
y
w
EOF
unset device
unset device_slave
mkfs.ext4 /dev/sda4
device1=$(get_device_id "/dev/sda3")
device2=$(get_device_id "/dev/sda4")
# Actions are identical to steps 1 and 10 with the size correction
# 14.--------------------------------------------------------------------------
# Make previous volumes logical and create a logical group over them
# Create physical volumes
pvcreate /dev/${device1} /dev/${device2}
# Creating a group of them
vgcreate vol_supernewdisk /dev/${device1} /dev/${device2}
# Now a logical volume creation
lvcreate -L 182M -n logical_vol vol_supernewdisk
# Make a new mount directory
mkdir /mnt/supernewdisk
# Make fs on this device
mke2fs -t ext4 /dev/vol_supernewdisk/logical_vol
# Mount it
mount /dev/vol_supernewdisk/logical_vol /mnt/supernewdisk
unset device1
unset device2
# 15.--------------------------------------------------------------------------
# Create a new folder in /mnt
mkdir /mnt/share
# Connect an external folder (my parameters must probably be changed)
mount.cifs //192.168.0.10/public /mnt/share -o username=kalipi,password=****
# 16.--------------------------------------------------------------------------
# Create the config file /etc/.smbclient containing following lines:
# username=kalipi
# password=****
# domain=kalipi.local
cat > /etc/.smbclient << EOF
username=kalipi
password=****
domain=kalipi.local
EOF
# Put the next line into /etc/fstab
# //192.168.0.10/public /mnt/share cifs user,rw,credentials=/etc/.smbclient 0 0
echo "//192.168.0.10/public /mnt/share cifs user,rw,credentials=/etc/.smbclient 0 0" >> /etc/fstab
@SkymeFactor
Copy link
Author

@AlexTalker, что теперь делать?

@AlexTalker
Copy link

Раз уж Вы использовали partprobe, не было необходимости в udevadm trigger, но в udevadm settle как оптимистичным ожиданием(в том плане что событие по завершению partprobe уже должно было прийти в udev, но не факт что все правила отработали).

В вызовах fstab например применение оных путей по-прежнему упущено.
Также Вы по-прежнему игнорируете --root 😄

К защите изучите что будет если записать некоторые данные на блочное устройство с одной системы(VM), а затем в то же время прочитать их с другой и почему(диск должен быть подключен к двум VM единовременно в таком случае).

@SkymeFactor
Copy link
Author

@AlexTalker, я partprobe в итоге отключил же) а udevadm settle там используется, просто в той же строке написан через точку с запятой. А во сколько защита будет?

@AlexTalker
Copy link

Так то что прописан я вижу,
суть в том что НЕ НАДО триггерить событие которое и так придёт,
притом триггеря все остальные устройства, это не комильфо.

Защита будет на парах.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment