insert bootable partition table at start of lv
authorDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 19 Mar 2021 10:23:20 +0000 (11:23 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 19 Mar 2021 10:23:20 +0000 (11:23 +0100)
lv-insert-part.sh [new file with mode: 0755]

diff --git a/lv-insert-part.sh b/lv-insert-part.sh
new file mode 100755 (executable)
index 0000000..1911ed2
--- /dev/null
@@ -0,0 +1,32 @@
+#!/bin/sh -xe
+
+# insert bootable partition table at beginning of lv
+
+test -z "$1" || test ! -e "$1" && echo "Usage: $0 /path/to/lv" && exit 1
+
+lvs --noheadings -o vg_name,name,lv_size $1 | tee /dev/stderr | while read vg_name name lv_size ; do
+       lvcreate -s -L $lv_size -n ${name}.snap /dev/$vg_name/$name
+
+       # create partition
+       echo '2048,+,L,*' | sfdisk --no-reread /dev/$vg_name/$name
+
+       # FIXME label?
+       mkfs.ext4 -F -E offset=$(( 2048 * 512 )) /dev/$vg_name/$name
+
+       mkdir /tmp/$name
+       mount /dev/$vg_name/$name /tmp/$name -o offset=$(( 2048 * 512 ))
+
+
+       mkdir /tmp/${name}.snap
+       mount /dev/$vg_name/${name}.snap /tmp/${name}.snap
+
+       rsync -raHX --numeric-ids --sparse /tmp/${name}.snap/ /tmp/$name/
+
+       umount /tmp/${name}.snap
+       umount /tmp/$name
+
+       rmdir /tmp/${name}.snap
+       rmdir /tmp/${name}
+
+       lvremove -y /dev/$vg_name/${name}.snap
+done